Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created December 30, 2015 02:20
Show Gist options
  • Save pachacamac/9f26efca66dd584190e1 to your computer and use it in GitHub Desktop.
Save pachacamac/9f26efca66dd584190e1 to your computer and use it in GitHub Desktop.
js benchmark wrapper function
var benchmarks = {};
function benchmark(f){
return function(){
var time, result, name = (''+f).split(/\W/,2)[1];
time = performance.now();
result = f.apply(this, arguments);
time = performance.now() - time;
if(!benchmarks[name]){
benchmarks[name] = {
min: time,
max: time,
avg: time,
cnt: 1
}
}else{
if(benchmarks[name].min > time) benchmarks[name].min = time;
if(benchmarks[name].max < time) benchmarks[name].max = time;
benchmarks[name].avg = (benchmarks[name].avg + time) * 0.5;
benchmarks[name].cnt++;
}
return result;
}
}
function fooTest(x){
var r=0;
for(var i=0; i<x; i++){ r += Math.sqrt(i); }
return r;
}
fooTest(10000000);
benchmark(fooTest)(10000000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment