나는 @musicfreaks 대답 의이 간단한 구현을 사용하고 있습니다. 기능은 없지만 사용하기 정말 쉽습니다. 이것은 bench(function(){return 1/2;}, 10000, [], this)
1/2 10,000 번을 계산할 것입니다.
/**
* Figure out how long it takes for a method to execute.
*
* @param {Function} method to test
* @param {number} iterations number of executions.
* @param {Array} args to pass in.
* @param {T} context the context to call the method in.
* @return {number} the time it took, in milliseconds to execute.
*/
var bench = function (method, iterations, args, context) {
var time = 0;
var timer = function (action) {
var d = Date.now();
if (time < 1 || action === 'start') {
time = d;
return 0;
} else if (action === 'stop') {
var t = d - time;
time = 0;
return t;
} else {
return d - time;
}
};
var result = [];
var i = 0;
timer('start');
while (i < iterations) {
result.push(method.apply(context, args));
i++;
}
var execTime = timer('stop');
if ( typeof console === "object") {
console.log("Mean execution time was: ", execTime / iterations);
console.log("Sum execution time was: ", execTime);
console.log("Result of the method call was:", result[0]);
}
return execTime;
};