Case Objects

JavaScript performance comparison

Test case created by dinocarl

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var fizzCases = {
    true: 'fizz',
    false: '',
  };
  
  function syntaxA(n) {
    if (n % 3 === 0) return 'fizz';
    return '';
  }
  
  function syntaxB(n) {
    return n % 3 === 0
      ? 'fizz'
      : '';
  }
  
  function syntaxC(n) {
    let x = n % 3 === 0;
    switch(x) {
      case true: {
        return 'fizz';
      }
      default: {
        return '';
      }  
    }
  }
  
  function syntaxD(n) {
    return fizzCases[`${n % 3 === 0}`];
  }
  

};
</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
If Syntax
syntaxA(3)
pending…
Ternary Syntax
syntaxB(3)
pending…
Switch Syntax
syntaxC(3)
pending…
Case Obj Syntax
syntaxD(3)
pending…

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

0 Comments