Array Performance (v153)

Revision 153 of this benchmark created by Andrey on


Description

Manual array lookups vs. holey arrays.

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

Setup

var a1 = [];
    var a2 = [];
    var o = {};

    for(var i=0;i<200;i++){
      var id = (29938+i).toString();
       a1[i] = {id: id , name: 'name1'};
       a2[id]={id: id , name: 'name1'};
       o[id]={id: id , name: 'name1'};
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup
var id = '30000';
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === id) result = a1[i];
}
 
ready
Holey Array by Index
var id = '30000';
var result = a2[id];
ready
Object by Key
var id = '30000';
var result = o[id];
 
ready
lodash
var id = '30000';
var result = _.find(a1, { 'id': id  });
 
ready
array->hash
var hash = {};
var id = '30000';
for (var i = 0; i < a1.length; i++) 
{
hash[a1[i].id] = a1[i];
}

var result = hash[id];
ready

Revisions

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