Array Performance

JavaScript performance comparison

Revision 89 of this test case created by Claudiu Ghica

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var a1 = ['name1', 'name2'];
    var o = {0:'name1', 1:'name2'};
  };
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Operation per each elem 11
var a2 = new Array();
var len = a1.length;
for (var i = 0; i < len; i=i+1) {  
    a2[i] = a1[i]+1;
}
pending…
Operation per each elem 12
var o2 = {};
for (prop in o) {
    if (!o.hasOwnProperty(prop)) {
        continue;
    }
    o2[prop] = o[prop];
}
pending…
Operation per each elem 13
var a2 = new Array();
for (var i = 0; i < a1.length; i++) {  
    a2[i] = a1[i]+1;
}
pending…
Operation per each elem 9
var a2 = [];
function addPerElement(element, index) {
   a2[index] = element+1;
   return true;
}
a1.every(addPerElement);
pending…
Operation per each elem 10
var a2 = new Array();
var len = a1.length;
for (var i = 0; i < len; i++) {  
    a2[i] = a1[i]+1;
}
pending…
Operation per each elem 7
var a2 = [];
[].map.call(a1, function(element, index) {
  a2[index] = element+1;
});
pending…
Operation per each elem 8
var a2 = new Array();
function addPerElement(element, index) {
   a2[index] = element+1;
   return true;
}
a1.every(addPerElement);
pending…
Array with loop
var id = 1;
var len = a1.length;
for (var i = 0; i < len; i++) {  
    console.log('a[' + i + '] = ' + a1[i]);
}
pending…
Loop in object 1
for (prop in o) {
    if (!o.hasOwnProperty(prop)) {
        continue;
    }
    console.log('a[' + prop + '] = ' + o[prop]);
}
pending…
Loop in object 2
for (var prop in o) {
  if (o.hasOwnProperty(prop)) {
    console.log('a[' + prop + '] = ' + o[prop]);
  }
}
pending…
Operation per each elem 1
var a2 = new Array();
function addPerElement(element, index, array) {
   a2[index] = element+1;
   return true;
}
a1.every(addPerElement);
pending…
Operation per each elem 2
var a2 = [];
function addPerElement(element, index, array) {
   a2[index] = element+1;
   return true;
}
a1.every(addPerElement);
pending…
Operation per each elem 3
var o2 = {};
function addPerElement(element, index, array) {
   o2[index] = element+1;
   return true;
}
a1.every(addPerElement);
pending…
Operation per each elem 4
var a2 = [].map.call(o, function(obj) {
  return obj.value+1;
});
pending…
Operation per each elem 5
var a2 = [].map.call(a1, function(element) {
  return element+1;
});
pending…
Operation per each elem 6
var o2 = {};
[].map.call(o, function(obj) {
  o2[index] = obj.value+1;
});
pending…
Manual Array Lookup
var id = 1;
for (var i = 0; i < a1.length; i++) {  
    if(i == id){
       result = a1[i];
       break;
    }
}
pending…
Object by Variable Key
var id = 1;
var result = o[id];
pending…
Object by Static Key
var id = 1;
var result = o.id;
pending…
Array by Variable Key
var id = 1;
var result = a1[id];
pending…
Manual Array Lookup 2
var id = 1;
var len = a1.length;
for (var i = 0; i < len; i++) {  
    if(i == id){
       result = a1[i];
       break;
    }
}
pending…
Array with forEach()
function findElement(element, index, array) {
  console.log('a[' + index + '] = ' + element);
}
a1.forEach(findElement);
pending…
Array with every()
function findElement(element, index, array) {
  console.log('a[' + index + '] = ' + element);
  return true;
}
a1.every(findElement);
pending…

Compare results of other browsers

Revisions

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

0 Comments