Key Or Array search

JavaScript performance comparison

Revision 3 of this test case created

Info

The purpose of this test is to test fastest way to check wether a string belongs to a set of strings.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var stringCount = 500;
    var testCount = 100;
    var i=0;
   
    var allStrings = [];
   
    for (i=0; i<stringCount; i++) {      
          allStrings[i]=Math.floor(Math.random()*1000)+'a';
    }
   
    var stringObj = {} ;
    for (var i=0; i<allStrings.length; i++) stringObj[allStrings[i]]=true;
   
    var testStrings = [];
    for (i=0; i<testCount; i++) {      
          testStrings[i]= Math.random() >0.5 ?
                            ( 0 | (1000*Math.random())) + 'b' :
                            allStrings[0 | (Math.floor()*allStrings.length)]  ;
    }
   
   
    var res=false;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
array search
for (i=0; i<testStrings.length; i++) { 
   res = allStrings.indexOf(testStrings[i])!=-1 ;
}
pending…
in operator
for (i=0; i<testStrings.length; i++) { 
   res = testStrings[i] in stringObj;
}
pending…
object property check
for (i=0; i<testStrings.length; i++) { 
   res =  stringObj[testStrings[i]] || false;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment