Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt
Last active January 25, 2017 13:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsemozhetbyt/396eb4b7713accf60ead2fe164e47306 to your computer and use it in GitHub Desktop.
Save vsemozhetbyt/396eb4b7713accf60ead2fe164e47306 to your computer and use it in GitHub Desktop.
Benchmark cycles: var / let / const
/******************************************************************************/
'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);
@vsemozhetbyt
Copy link
Author

vsemozhetbyt commented Jan 19, 2017

  // v8 4.5.103.43 (Node.js 4.7.2)

forVar_______:  0ms
forLet_______: 31ms
forOfVar_____: 94ms
forOfLetConst: 94ms
forEachVar___: 15ms
forEachLet___: 31ms

  // v8 4.6.85.32 (Node.js 5.12.0)

forVar_______:  2.735ms
forLet_______: 23.017ms
forOfVar_____: 89.047ms
forOfLetConst: 83.709ms
forEachVar___: 26.844ms
forEachLet___: 32.260ms

  // v8 5.1.281.89 (Node.js 6.9.4)

forVar_______:  2.085ms
forLet_______:  7.976ms
forOfVar_____: 49.098ms
forOfLetConst: 48.186ms
forEachVar___: 23.597ms
forEachLet___: 25.034ms

  // v8 5.4.500.45 (Node.js 7.4.0)

forVar_______:  2.164ms
forLet_______: 26.332ms
forOfVar_____: 26.022ms
forOfLetConst: 25.614ms
forEachVar___: 28.428ms
forEachLet___: 37.018ms

  // v8 5.4.500.46 (Node.js 8.0.0-nightly20170118622b43997f)

forVar_______:  2.170ms
forLet_______: 26.253ms
forOfVar_____: 25.680ms
forOfLetConst: 25.958ms
forEachVar___: 28.475ms
forEachLet___: 36.671ms

@vsemozhetbyt
Copy link
Author

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