how slow is an array vs a lone variable

Benchmark created by gaby de wilde on


Setup

function foo1(x){return x+Math.random()}
  function foo2(x){return x[0]+Math.random()}
  window.bar1 = 0;
  window.bar2 = [0];

Test runner

Ready to run.

Testing in
TestOps/sec
var
for(var x=0;x<10000;x++){

window.bar1 = foo1(window.bar1);

}
console.log(window.bar1)
ready
array
for(var x=[0];x[0]<10000;x[0]++){

window.bar2[0] = foo2(window.bar2);

}
console.log(window.bar2[0])
ready
var without global
bar3 = 0;
for(var x=0;x<10000;x++){

bar3 = foo1(bar3);

}
console.log(bar3)
ready
var without global
bar4 = [0]
for(var x=[0];x[0]<10000;x[0]++){

bar4[0] = foo2(bar4);

}
console.log(bar4[0])
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by gaby de wilde on