Skip to content

Instantly share code, notes, and snippets.

@hyperlogic
Created June 20, 2016 19:59
Show Gist options
  • Select an option

  • Save hyperlogic/1881148fe1f0a0353dc3fb6e71fa7029 to your computer and use it in GitHub Desktop.

Select an option

Save hyperlogic/1881148fe1f0a0353dc3fb6e71fa7029 to your computer and use it in GitHub Desktop.
function someOperation(x) { }
function looped(array, op) {
var i, length = array.length;
for (i = 0; i < length; i++) {
op(array[i]);
}
}
function mapped(array, op) {
array.forEach(op);
}
function timeIt(n, m, crawler, op) {
var array = new Array(m), i, start, elapsed;
// jit the crawler
crawler(array, op);
start = Date.now();
for (i = 0; i < n; i++) {
crawler(array, someOperation);
}
elapsed = Date.now() - start;
print('time:', elapsed);
}
timeIt(100000, 0, looped, someOperation);
timeIt(100000, 0, mapped, someOperation);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment