Manual array map vs loop

Benchmark created by Brent on


Setup

var item = {"_id":"5850461c4ed903736e5b6ab0","active":true,"account_id":"58dasdasdsadsa5355fea99aa8","name":"Fake product name fake product title","sku":"3412312322232","upc":"3341234123123233","tags":[],"serial_numbers":[],"barcodes":[],"custom_fields":[],"supplies":[],"listings":[{"_id":"5850502e4ed903736e5d1138","variant_id":null,"item_id":"d6012b627c66d592c8121dde7ee3b5c7","store_id":"585030a114d72001d0e5b8d7"},{"_id":"58505bc24ed903736e5d4798","variant_id":null,"item_id":"d6012b627c66d592c8121dde7ee3b5c7","store_id":"5850471373ddda6a344c8b2c"},{"_id":"58b7d39f297b9e569bc7af5b","variant_id":null,"item_id":"d6012b627c66d592c8121dde7ee3b5c7","store_id":"58b590aeea11b611fa7619e8"}],"images":[],"last_updated":"2017-03-02T08:11:11.710Z","length":null,"width":null,"height":null,"fulfillment_sku":null,"notes":null,"unit":null,"cost":null,"wholesale":null,"retail":null,"image":null,"weight_unit":null,"weight":null,"dimensions_unit":null,"description":null};
  
  var items = [];
  var item_skus = [];
  
  for (i=0;i<50000;i++) {
  items.push(JSON.parse(JSON.stringify(item)));
  item_skus.push(item.sku);
  }
  
  item_skus[25000] = 'validsku';
  items[25000].sku = 'validsku';
  
  var search_sku = 'validsku';
  
  var globals = { items: items };

Test runner

Ready to run.

Testing in
TestOps/sec
Find
globals.items.find(function (item) {
return item.sku === search_sku;
});
ready
Use items length
for(i=0;i<globals.items.length;i++) {
if(globals.items[i].sku === search_sku) {
return items[i];
}
}
ready
Search array, fetch by index
var result = item_skus.indexOf(search_sku);
if(result !== -1) {
return globals.items[result];
}
ready

Revisions

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