Key Or Array search

JavaScript performance comparison

Revision 2 of this test case created by vincent piel

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 i=0;
   
    var allStrings = ['apple', 'orange', 'tomato', 'lemon', 'banana'];
   
    var stringObj = {} ;
    for (var i=0; i<allStrings.length; i++) stringObj[allStrings[i]]=true;
   
    var testStrings = ['orange', 'car', 'lemon', 'banana', 'x', 'lemon', 'void'];
   
    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