Object index vs Array search (v23)

Revision 23 of this benchmark created by test_simo596 on


Setup

var obj = {'test1': {'value': 1},
    'test2': {'value': 2},
    'test3': {'value': 3},
    'test4': {'value': 4},
    'test5': {'value': 5},
    'test6': {'value': 6},
    'test7': {'value': 7},
    'test8': {'value': 8},
    'test9': {'value': 9},
    'test10': {'value': 10},
    };
    var arr = [{'name': 'test1', 'value': 1},
    {'name': 'test2', 'value': 2},
    {'name': 'test3', 'value': 3},
    {'name': 'test4', 'value': 4},
    {'name': 'test5', 'value': 5},
    {'name': 'test6', 'value': 6},
    {'name': 'test7', 'value': 7},
    {'name': 'test8', 'value': 8},
    {'name': 'test9', 'value': 9},
    {'name': 'test10', 'value': 10}
    ];
    
    function searchInArray(search){
      for(var i=0; i<arr.length; i++){
        var val = arr[i];
        if(val.name==search){
          return val.value;
        }
      }
    }
    function searchInArraySome(search){
          var val = arr.some(function (obj) {
                    return obj.name == search;
                });
           return val.value ;
    }
    function searchInObject(search){
      var res = obj[search];
      if(res){
        return res.value;
      }
    }
    function searchInObject2(search){
      if(search in obj)return obj[search];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Object (exists last)
searchInArraySome('test10');
ready
Array (exists last)
searchInArray('test10');

 
ready
Object (exists first)
searchInArraySome('test1');
ready
Array (exists first)
searchInArray('test1');
ready
Object (doesn't exist)
searchInArraySome('testx');
ready
Array (doesn't exist)
searchInArray('testx');
ready
Object(exist last) in operator
searchInObject2('test10');
ready
Object (doesn't exist) in operator
searchInObject2('testx');
ready

Revisions

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

  • Revision 1: published by Davsket on
  • Revision 2: published by Zdeněk Mlčoch on
  • Revision 23: published by test_simo596 on