Skip to content

Instantly share code, notes, and snippets.

@lukeed
Created July 10, 2017 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeed/2629834a2d91a85885678ab561b3a202 to your computer and use it in GitHub Desktop.
Save lukeed/2629834a2d91a85885678ab561b3a202 to your computer and use it in GitHub Desktop.
cached vs non-cached perf
const LOOPS = 1e6;
const LEN = 1e3;
function test(fn) {
let i = 0;
const data = new Array(LEN);
const start = Date.now();
for (; i < LOOPS; i++) {
fn(data);
}
const end = Date.now() - start;
console.log('elapsed: ', end);
console.log('op/s ', LOOPS * LEN * 1000 / end);
}
function foo(data) {
var i=0, len=data.length, res=[];
for (; i < len; i++) {
res[i] = true;
}
return res;
}
function bar(data) {
var res=[];
for (var i=0; i < data.length; i++) {
res[i] = true;
}
return res;
}
console.log('cached loop---');
test(foo);
console.log('non-cached loop---');
test(bar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment