Skip to content

Instantly share code, notes, and snippets.

@corymartin
Created November 10, 2014 04:24
Show Gist options
  • Save corymartin/e17531466f5a72bb7e8d to your computer and use it in GitHub Desktop.
Save corymartin/e17531466f5a72bb7e8d to your computer and use it in GitHub Desktop.
Simple test wrapper for testing code speed.
/**
* Test code speed.
*
* timeTest('#forEach() test', 10000, function(count) {
* someArray.forEach(function(){});
* });
*/
function timedTest(name, count, test) {
if (count !== +count) throw new TypeError('count must be a number');
// Make sure we have a uint to avoid infinite loop.
if (count !== (count >>> 0)) count = ~count + 1;
var finalResult;
console.group(name);
console.log('Testing %d times', count);
console.time(name);
while (count--) finalResult = test(count);
console.timeEnd(name);
console.log('Result: %o', finalResult);
console.groupEnd(name);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment