Array Performance (v91)

Revision 91 of this benchmark created on


Description

Manual array lookups vs. holey arrays.

Setup

var a1 = ['name1', 'name2'];
    var o = {0:'name1', 1:'name2'};

Test runner

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup
var id = 1;
for (var i = 0; i < a1.length; i++) {
  if (i == id) {
    result = a1[i];
    break;
  }
}
ready
Object by Variable Key
var id = 1;
var result = o[id];
ready
Object by Static Key
var id = 1;
var result = o.id;
ready
Array by Variable Key
var id = 1;
var result = a1[id];
ready
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;
  }
}
ready
Array with forEach()
function findElement(element, index, array) {
  console.log('a[' + index + '] = ' + element);
}
a1.forEach(findElement);
ready
Array with every()
function findElement(element, index, array) {
  console.log('a[' + index + '] = ' + element);
  return true;
}
a1.every(findElement);
ready
Array with loop
var id = 1;
var len = a1.length;
for (var i = 0; i < len; i++) {
  console.log('a[' + i + '] = ' + a1[i]);
}
ready
Loop in object 1
for (prop in o) {
  if (!o.hasOwnProperty(prop)) {
    continue;
  }
  console.log('a[' + prop + '] = ' + o[prop]);
}
ready
Loop in object 2
for (var prop in o) {
  if (o.hasOwnProperty(prop)) {
    console.log('a[' + prop + '] = ' + o[prop]);
  }
}
ready
Operation per each elem 1
var a2 = new Array();

function addPerElement(element, index, array) {
  a2[index] = element + 1;
  return true;
}
a1.every(addPerElement);
ready
Operation per each elem 2
var a2 = [];

function addPerElement(element, index, array) {
  a2[index] = element + 1;
  return true;
}
a1.every(addPerElement);
ready
Operation per each elem 3
var o2 = {};

function addPerElement(element, index, array) {
  o2[index] = element + 1;
  return true;
}
a1.every(addPerElement);
ready
Operation per each elem 4
var a2 = [].map.call(o, function(obj) {
  return obj.value + 1;
});
ready
Operation per each elem 5
var a2 = [].map.call(a1, function(element) {
  return element + 1;
});
ready
Operation per each elem 6
var o2 = {};
[].map.call(o, function(obj) {
  o2[index] = obj.value + 1;
});
ready
Operation per each elem 7
var a2 = [];
[].map.call(a1, function(element, index) {
  a2[index] = element + 1;
});
ready
Operation per each elem 8
var a2 = new Array();

function addPerElement(element, index) {
  a2[index] = element + 1;
  return true;
}
a1.every(addPerElement);
ready
Operation per each elem 9
var a2 = [];

function addPerElement(element, index) {
  a2[index] = element + 1;
  return true;
}
a1.every(addPerElement);
ready
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;
}
ready
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;
}
ready
Operation per each elem 12
var o2 = {};
for (prop in o) {
  if (!o.hasOwnProperty(prop)) {
    continue;
  }
  o2[prop] = o[prop];
}
ready
Operation per each elem 13
var a2 = new Array();
for (var i = 0; i < a1.length; i++) {
  a2[i] = a1[i] + 1;
}
ready

Revisions

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