switch-vs-objeto-literal

JavaScript performance comparison

Test case created by suissa

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const type = 'coke'

};
</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
switch
// RUIM
let drink
switch( type ) {
case 'pepsi':
  drink = 'Pepsi'
  break
case 'teste1':
  drink = 'teste1'
  break
case 'teste2':
  drink = 'teste2'
  break
case 'teste3':
  drink = 'teste3'
  break
case 'teste4':
  drink = 'teste4'
  break
case 'teste5':
  drink = 'teste5'
  break
case 'teste6':
  drink = 'teste6'
  break
case 'teste7':
  drink = 'teste7'
  break
case 'teste8':
  drink = 'teste8'
  break
case 'coke':
  drink = 'Coke'
  break
default:
  drink = 'Unknown drink!'
}

console.log( drink ) // 'Coke'
pending…
objeto literal
// MELHOR
const getDrink = ( type ) => {
  const drinks = {
    'pepsi': 'Pepsi',
    'lemonade': 'Lemonade',
    'teste1': 'teste1',
    'teste2': 'teste2',
    'teste3': 'teste3',
    'teste4': 'teste4',
    'teste5': 'teste5',
    'teste6': 'teste6',
    'teste7': 'teste7',
    'teste8': 'teste8',
    'coke': 'Coke'
  }
return drinks[ type ]
}

console.log( getDrink(type) ) // 'Coke'
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