Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created December 4, 2011 23:26
Show Gist options
  • Save srikumarks/1431640 to your computer and use it in GitHub Desktop.
Save srikumarks/1431640 to your computer and use it in GitHub Desktop.
V8 Javascript Speed comparison for "const" and "var" usage.
function timeit(f, N, S) {
var start, timeTaken;
var stats = {min: 1e50, max: 0, N: 0, sum: 0, sqsum: 0};
var i;
for (i = 0; i < S; ++i) {
start = Date.now();
f(N);
timeTaken = Date.now() - start;
stats.min = Math.min(timeTaken, stats.min);
stats.max = Math.max(timeTaken, stats.max);
stats.sum += timeTaken;
stats.sqsum += timeTaken * timeTaken;
stats.N++
}
var mean = stats.sum / stats.N;
var sqmean = stats.sqsum / stats.N;
return {min: stats.min, max: stats.max, mean: mean, spread: Math.sqrt(sqmean - mean * mean)};
}
var variable1 = 10;
var variable2 = 10;
var variable3 = 10;
var variable4 = 10;
var variable5 = 10;
var variable6 = 10;
var variable7 = 10;
var variable8 = 10;
var variable9 = 10;
var variable10 = 10;
function varAccess(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += variable1;
sum += variable2;
sum += variable3;
sum += variable4;
sum += variable5;
sum += variable6;
sum += variable7;
sum += variable8;
sum += variable9;
sum += variable10;
}
return sum;
}
const constant1 = 10;
const constant2 = 10;
const constant3 = 10;
const constant4 = 10;
const constant5 = 10;
const constant6 = 10;
const constant7 = 10;
const constant8 = 10;
const constant9 = 10;
const constant10 = 10;
function constAccess(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += constant1;
sum += constant2;
sum += constant3;
sum += constant4;
sum += constant5;
sum += constant6;
sum += constant7;
sum += constant8;
sum += constant9;
sum += constant10;
}
return sum;
}
function control(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
}
return sum;
}
console.log("ctl = " + JSON.stringify(timeit(control, 10000000, 50)));
console.log("con = " + JSON.stringify(timeit(constAccess, 10000000, 50)));
console.log("var = " + JSON.stringify(timeit(varAccess, 10000000, 50)));
@srikumarks
Copy link
Author

My measurements using node.js v0.6.3 on MacOSX -

ctl = {"min":182,"max":187,"mean":183.8,"spread":0.98}
con = {"min":770,"max":789,"mean":774.92,"spread":3.16}
var = {"min":196,"max":253,"mean":224.42,"spread":5.96}

(The spread rounded to two decimal places)

@19h
Copy link

19h commented Jan 24, 2015

tl;dr DONT JUDGE USING THE PREVIOUS RESULTS.

V8 changed completely since this was published and you should not accept any benchmarks that are older than 3 months.

ctl = {"min":124,"max":243,"mean":133.12,"spread":16.138946681862468}
con = {"min":123,"max":259,"mean":137.9,"spread":23.7126548492572}
var = {"min":126,"max":282,"mean":139.14,"spread":24.459771053711915}


New results with Node 5:

ctl = {"min":228,"max":260,"mean":230.76,"spread":5.213674328149501}
con = {"min":228,"max":276,"mean":239.14,"spread":10.897724533131056}
var = {"min":261,"max":323,"mean":282.46,"spread":7.756829249121327}

@akyoto
Copy link

akyoto commented Sep 24, 2016

Node 6.6.0:

ctl = {"min":92,"max":96,"mean":93.34,"spread":0.7901898506047685}
con = {"min":92,"max":95,"mean":93.38,"spread":0.7180529228410895}
var = {"min":92,"max":103,"mean":93.78,"spread":1.9420607611499223}

@antoine-duchenet
Copy link

antoine-duchenet commented Dec 29, 2016

Node 6.9.2 (Physical machine)

ctl = {"min":87,"max":132,"mean":88.84,"spread":6.313034135817678}
con = {"min":87,"max":89,"mean":87.74,"spread":0.48207883172856697}
var = {"min":87,"max":94,"mean":88.4,"spread":1.6970562748472102}

Node 6.9.2 (Docker container)

ctl = {"min":87,"max":131,"mean":88.78,"spread":6.162110028228955}
con = {"min":87,"max":90,"mean":87.74,"spread":0.7432361670428108}
var = {"min":87,"max":90,"mean":87.72,"spread":0.6013318551350664}

Node 7.3.0 (Docker container)

ctl = {"min":87,"max":134,"mean":88.84,"spread":6.60411992622783}
con = {"min":87,"max":464,"mean":321.32,"spread":123.30684328130376}
var = {"min":87,"max":89,"mean":87.68,"spread":0.5810335618524364}

con.spread heavily increased in 7.3.0, dockerized environnement does not seems to be responsible.

@antoine-duchenet
Copy link

antoine-duchenet commented Dec 30, 2016

nodejs/node#10516

Be aware that all the sum += return NaN due to the uninitialized sum.
Benchmarks results seems deeply affected by this unexpected behavior.

@episage
Copy link

episage commented Feb 22, 2020

➜   ✗ node temp.js 
ctl = {"min":6,"max":109,"mean":10.26,"spread":20.06071783361702}
con = {"min":6,"max":111,"mean":10.34,"spread":20.44857941276117}
var = {"min":6,"max":113,"mean":10.18,"spread":20.28663599515701}
➜   ✗ node -v
v12.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment