2009/07/02 Update:
I have created a far better version, which you can find here: [LINK]
"use strict"; var getTime = function (func, length) { var i, start, end; length = length || 1; start = new Date(); for (i = 0; i < length; i += 1) { func(); } end = new Date(); return end - start; };
JavaScript does not have a built-in way to easily measure how long it takes to run code; however, it supplies the necessary building-blocks to create such a way.
You use getTime
like this:
var time = getTime(function () { /* code goes here! */ }, 100000);
The variable time
now contains how many milliseconds it took to run the function 100000
times.
- The first parameter should be a function. The time it takes to run this function is returned.
- The second parameter is how many times you want to run the function. The default is
1
.
Using this function, you can easily see how long it takes to run chunks of code, which is useful in benchmarking.
No comments:
Post a Comment