Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created February 5, 2015 04:39
Show Gist options
  • Select an option

  • Save Alex1990/f29b874ef5e33119cf2e to your computer and use it in GitHub Desktop.

Select an option

Save Alex1990/f29b874ef5e33119cf2e to your computer and use it in GitHub Desktop.
A simple function to test performance.
/**
* A simple function to test performance.
*/
function benchmark() {
var tests = Array.prototype.slice.call(arguments);
var length = tests.length;
var startTimes = Array(length);
var result = Array(length);
var times = typeof benchmark.times === 'number' ? benchmark.times/2 : 5000;
for (var i = 0; i < length; i++) {
startTimes[i] = new Date();
for (var j = 0; j < times; j++) {
tests[i]();
}
result[i] = new Date() - startTimes[i];
}
for (i = length - 1; i >=0; i--) {
startTimes[i] = new Date();
for (var j = 0; j < times; j++) {
tests[i]();
}
result[i] += new Date() - startTimes[i];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment