another switch vs indexOf

JavaScript performance comparison

Test case created by Faustino Forcén

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function bySwitch(input) {
  	var strClass = 'type-0';
  
    // force the input to NOT be treated as a constant by the JIT
  	switch(input.toString()) {
  		case 'Payment Credit Card':
  			strClass = 'type-1';
  			break;
  		case 'Cash Withdrawal':
  			strClass = 'type-2';
  			break;
  		case 'Bill Payment':
  			strClass = 'type-3';
  			break;
  		case 'Salary':
  			strClass = 'type-4';
  			break;
  		case 'Online Transfer':
  			strClass = 'type-5';
  			break;
  	}
  	return strClass;
  }
  
  function byArray(input) {	
  	return 'type-' + 
  			(['Payment Credit Card',
  			'Cash Withdrawal',
  			'Bill Payment',
  			'Salary',
  			'Online Transfer'].indexOf(input) + 1);
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Using Switch
bySwitch('Cash Withdrawal');
pending…
Using Switch not found
bySwitch('empty');
pending…
Using indexOf
byArray('Cash Withdrawal');
pending…
Using indexOf not found
byArray('empty');
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.

0 Comments