Skip to content

Instantly share code, notes, and snippets.

@peterwmwong
Last active June 8, 2017 13:19
Show Gist options
  • Save peterwmwong/ba3eec2e4af59cf69ccb32b2493f2573 to your computer and use it in GitHub Desktop.
Save peterwmwong/ba3eec2e4af59cf69ccb32b2493f2573 to your computer and use it in GitHub Desktop.
> /p/google/v8/out.gn/x64.release/d8 test.js
testFuncExpr 1345 ms
testFuncDecl 347 ms
'use strict';
function testFuncExpr(arr){
let result = 0;
arr.forEach((c) => { if(c % 2) result++ });
return result;
}
function testFuncDecl(arr){
let result = 0;
const fn = (c) => { if(c % 2) result++ };
arr.forEach(fn);
return result;
}
var ARRAYS = [
[0,1,2,3,4,5],
[0,2,2,3,4,5],
[0,3,2,3,4,5],
[0,4,2,3,4,5],
[0,5,2,3,4,5],
[0,6,2,3,4,5]
];
function run(i, isFuncExpr){
let arr = ARRAYS[i % ARRAYS.length];
if (isFuncExpr) return testFuncExpr(arr);
else return testFuncDecl(arr);
}
function bench(isFuncExpr){
const start = Date.now();
let end = 0;
for (let i = 0; i < 1e7; ++i) {
run(i, isFuncExpr);
}
end = Date.now();
print(isFuncExpr ? 'testFuncExpr' : 'testFuncDecl', end - start, 'ms');
}
// Warmup
run(0, true);
run(0, false);
run(1, true);
run(1, false);
// Bench
bench(true);
bench(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment