Created
November 8, 2009 19:46
-
-
Save fearphage/229452 to your computer and use it in GitHub Desktop.
Very basic benchmarking function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Benchmarker(count, args, that) { | |
if (Object.prototype.toString.call(args) != '[object Array]') args = [args]; | |
function toArray() { | |
var name, results = this.results, array = []; | |
for (name in results) { | |
array.push(name + ': ' + results[name]); | |
} | |
return array; | |
} | |
function toString() { | |
var array = this.toArray(); | |
return array.length ? array.join('ms\n') + 'ms' : 'Please run some tests firsts'; | |
} | |
function test(name, fn) { | |
var i = count, ignore, start = new Date; | |
while (i--) ignore = fn.apply(that, args); | |
this.results[name] = (new Date) - start; | |
} | |
return { | |
results: {} | |
,toString: toString | |
,toArray: toArray | |
,test: test | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment