Last active
January 25, 2017 13:33
-
-
Save vsemozhetbyt/396eb4b7713accf60ead2fe164e47306 to your computer and use it in GitHub Desktop.
Benchmark cycles: var / let / const
This file contains 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
/******************************************************************************/ | |
'use strict'; | |
/******************************************************************************/ | |
// via https://twitter.com/bmeurer/status/821804688454680576 | |
console.log(` | |
// v8 ${process.versions.v8} (Node.js ${process.versions.node}) | |
`); | |
function forVar_______(a) { | |
var x = 0; | |
for (var i = 0, l = a.length; i < l; ++i) x += a[i]; | |
return x; | |
} | |
function forLet_______(a) { | |
let x = 0; | |
for (let i = 0, l = a.length; i < l; ++i) x += a[i]; | |
return x; | |
} | |
function forOfVar_____(a) { | |
var x = 0; | |
for (var y of a) x += y; | |
return x; | |
} | |
function forOfLetConst(a) { | |
let x = 0; | |
for (const y of a) x += y; | |
return x; | |
} | |
function forEachVar___(a) { | |
var x = 0; | |
a.forEach((y) => { x += y; }); | |
return x; | |
} | |
function forEachLet___(a) { | |
let x = 0; | |
a.forEach((y) => { x += y; }); | |
return x; | |
} | |
const a = []; | |
for (let i = 0; i < 1000; ++i) a[i] = i; | |
function bench(f) { | |
for (let i = 0; i < 1000; ++i) f.call(null, a); | |
} | |
for (let i = 0; i < 10; ++i) { | |
bench(forVar_______); | |
bench(forLet_______); | |
bench(forOfVar_____); | |
bench(forOfLetConst); | |
bench(forEachVar___); | |
bench(forEachLet___); | |
} | |
console.time(forVar_______.name); | |
bench(forVar_______); | |
console.timeEnd(forVar_______.name); | |
console.time(forLet_______.name); | |
bench(forLet_______); | |
console.timeEnd(forLet_______.name); | |
console.time(forOfVar_____.name); | |
bench(forOfVar_____); | |
console.timeEnd(forOfVar_____.name); | |
console.time(forOfLetConst.name); | |
bench(forOfLetConst); | |
console.timeEnd(forOfLetConst.name); | |
console.time(forEachVar___.name); | |
bench(forEachVar___); | |
console.timeEnd(forEachVar___.name); | |
console.time(forEachLet___.name); | |
bench(forEachLet___); | |
console.timeEnd(forEachLet___.name); |
Author
vsemozhetbyt
commented
Jan 19, 2017
•
From https://twitter.com/chicoxyzzy/status/821890622806257664:
Now using node v4.7.2 (npm v2.15.11)
// v8 4.5.103.43 (Node.js 4.7.2)
forVar_______: 2ms
forLet_______: 12ms
forOfVar_____: 66ms
forOfLetConst: 66ms
forEachVar___: 16ms
forEachLet___: 22ms
Now using node v6.9.4 (npm v3.10.10)
// v8 5.1.281.89 (Node.js 6.9.4)
forVar_______: 0.871ms
forLet_______: 4.920ms
forOfVar_____: 27.866ms
forOfLetConst: 30.833ms
forEachVar___: 14.314ms
forEachLet___: 14.328ms
Now using node v7.4.0 (npm v4.1.1)
// v8 5.4.500.45 (Node.js 7.4.0)
forVar_______: 1.085ms
forLet_______: 15.625ms
forOfVar_____: 17.448ms
forOfLetConst: 15.705ms
forEachVar___: 18.096ms
forEachLet___: 23.480ms
// v8 5.7.114 (Node.js 8.0.0-pre)
forVar_______: 1.070ms
forLet_______: 16.417ms
forOfVar_____: 2.467ms
forOfLetConst: 2.366ms
forEachVar___: 15.918ms
forEachLet___: 23.759ms
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment