array loop vs filter (v6)

Revision 6 of this benchmark created by c on


Setup

var obj = [{ id: 0, num: 20}, { id: 1, num: 20}, { id: 2, num: 20}, { id: 3, num: 20}, { id: 4, num: 20}, { id: 5, num: 20}, { id: 6, num: 20}, { id: 7, num: 20}, { id: 8, num: 20}, { id: 9, num: 20}, { id: 10, num: 20}, { id: 11, num: 20}, { id: 12, num: 20}, { id: 13, num: 20}, { id: 14, num: 20}, { id: 15, num: 20}, { id: 16, num: 20}, { id: 17, num: 20}, { id: 18, num: 20}, { id: 19, num: 20}, { id: 20, num: 50}, { id: 21, num: 50}, { id: 22, num: 50}, { id: 23, num: 50}, { id: 24, num: 50}, { id: 25, num: 50}, { id: 26, num: 50}, { id: 27, num: 50}, { id: 28, num: 50}, { id: 29, num: 50}, { id: 30, num: 50}, { id: 31, num: 50}, { id: 32, num: 50}, { id: 33, num: 50}, { id: 34, num: 50}, { id: 35, num: 50}, { id: 36, num: 50}, { id: 37, num: 50}, { id: 38, num: 50}, { id: 39, num: 50}, { id: 40, num: 50}, { id: 41, num: 50}, { id: 42, num: 50}, { id: 43, num: 50}, { id: 44, num: 50}, { id: 45, num: 50}, { id: 46, num: 50}, { id: 47, num: 50}, { id: 48, num: 50}, { id: 49, num: 50}, { id: 50, num: 80}, { id: 51, num: 80}, { id: 52, num: 80}, { id: 53, num: 80}, { id: 54, num: 80}, { id: 55, num: 80}, { id: 56, num: 80}, { id: 57, num: 80}, { id: 58, num: 80}, { id: 59, num: 80}, { id: 60, num: 80}, { id: 61, num: 80}, { id: 62, num: 80}, { id: 63, num: 80}, { id: 64, num: 80}, { id: 65, num: 80}, { id: 66, num: 80}, { id: 67, num: 80}, { id: 68, num: 80}, { id: 69, num: 80}, { id: 70, num: 80}, { id: 71, num: 80}, { id: 72, num: 80}, { id: 73, num: 80}, { id: 74, num: 80}, { id: 75, num: 80}, { id: 76, num: 80}, { id: 77, num: 80}, { id: 78, num: 80}, { id: 79, num: 80}, { id: 80, num: 100}, { id: 81, num: 100}, { id: 82, num: 100}, { id: 83, num: 100}, { id: 84, num: 100}, { id: 85, num: 100}, { id: 86, num: 100}, { id: 87, num: 100}, { id: 88, num: 100}, { id: 89, num: 100}, { id: 90, num: 100}, { id: 91, num: 100}, { id: 92, num: 100}, { id: 93, num: 100}, { id: 94, num: 100}, { id: 95, num: 100}, { id: 96, num: 100}, { id: 97, num: 100}, { id: 98, num: 100}, { id: 99, num: 100}]
  
  var f = {id: 20, num: 100}

Test runner

Ready to run.

Testing in
TestOps/sec
filter
var keys = Object.keys(f)
return obj.filter(function (el) {
  for (var i = 0; i < keys.length; i ++){
    return el.num === keys[i];
  }
})
ready
loop
var res = []
var keys = Object.keys(f)
var filter_length = keys.length

for (var i = 0, j = obj.length; i !== j; i++) {
    for (var ix = 0; ix < filter_length; ix ++){
      if (obj[i][keys[ix]] === keys[ix]) res.push(obj[i])
  }
}

return res
ready
loop with length saved
var res = []
var keys = Object.keys(f)
var length = obj.length
var filter_length = keys.length

for (var i = 0, j = length; i !== j; i++) {
    for (var ix = 0; ix < filter_length; ix ++){
      if (obj[i][keys[ix]] === keys[ix]) res.push(obj[i])
  }
}

return res
ready
var res = []
  
var item_length = obj.length
var filter = Object.keys(f)
var filter_length = filter.length

function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = new Array();

  while( ai < a.length && bi < b.length )
  {
     if      (a[ai] < b[bi] ){ ai++; }
     else if (a[ai] > b[bi] ){ bi++; }
     else /* they're equal */
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }
    //console.log(result)
  if (result.length > 0) return result;
}

for (var i = 0; i < item_length; i++) {
    var intersection = intersect_safe(Object.keys(obj[i]), filter)
    // console.log(intersect_safe(Object.keys(obj[i]), filter))
    if (intersection) res.push(intersection)
  }
  
ready

Revisions

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