loop vs. lookup (v3)

Revision 3 of this benchmark created by Ian Carter on


Setup

var a = ['vegan','soccer','baby-shop','cycling-and-triathlon','sporting-goods','girls-shoes','mens-jeans','pajamas','socks'];
    
    var o = {'vegan':0,'soccer':1,'baby-shop':2,'cycling-and-triathlon':3,'sporting-goods':4,'girls-shoes':5,'mens-jeans':6,'pajamas':7,'socks':8};
    
    //improved performance if array is sorted by most common requests
    function index_a(x)
    {var i=a.length;while(--i>-1)if(a[i]==x)return i;return -1;}
    
    function index_o(x)
    {if(o[x]) return o[x]; return -1;}
    
    function index_switch(x)
    {switch(x){case 'vegan':return 0;case 'soccer':return 1;case 'baby-shop':return 2;case 'cycling-and-triathlon':return 3;case 'sporting-goods':return 4;case 'girls-shoes':return 5;case 'mens-jeans':return 6;case 'pajamas':return 7;case 'socks':return 8;default:return -1;}}

Test runner

Ready to run.

Testing in
TestOps/sec
loop
index_a("notInThere")
ready
lookup
index_o("notInThere")
ready
switch
index_switch("notInThere")
ready

Revisions

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

  • Revision 1: published by ratkinson on
  • Revision 2: published by ratkinson on
  • Revision 3: published by Ian Carter on