Switch VS Array (v4)

Revision 4 of this benchmark created on


Description

Checks whats faster between a switch case and an array

Setup

str = 'dog'
    
    function switchTest(){
    
        switch(str){
                case 'cat':
                        return true;
                break;
        
                case 'mouse':
                        return true;
                break;
        
                case 'parrot':
                        return true;
                break;
        
                case 'monkey':
                        return true;
                break;
        
                case 'dog':
                        return true;
                break;
        
                default: break; return false ;
        }
    
    }
    a = {
                'cat': true,
                'mouse': true,
                'parrot': true,
                'monkey': true,
                'dog': true
        }
    
    function arrayTest(){
        return a[str] || false;
    }
      var b = ['cat','mouse','parrot','monkey','dog'];
    function indexOfTest(){
        return ['true','true','true','true','true'][b.indexOf(str)];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array Lookups
arrayTest('dog')
ready
Switch Case Loops
switchTest('dog')
 
ready
indexOf Loops
indexOfTest('dog')
ready

Revisions

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