Key Or Array search

JavaScript performance comparison

Revision 3 of this test case created


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

Preparation code

Benchmark.prototype.setup = function() {
    var stringCount = 500;
    var testCount = 100;
    var i=0;
    var allStrings = [];
    for (i=0; i<stringCount; i++) {      
    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;

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 ;
in operator
for (i=0; i<testStrings.length; i++) { 
   res = testStrings[i] in stringObj;
object property check
for (i=0; i<testStrings.length; i++) { 
   res =  stringObj[testStrings[i]] || false;

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment