Created
July 10, 2017 06:49
-
-
Save lukeed/2629834a2d91a85885678ab561b3a202 to your computer and use it in GitHub Desktop.
cached vs non-cached perf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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