Case Objects

Benchmark created by dinocarl on


Setup

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}`];
  }

Test runner

Ready to run.

Testing in
TestOps/sec
If Syntax
syntaxA(3)
ready
Ternary Syntax
syntaxB(3)
ready
Switch Syntax
syntaxC(3)
ready
Case Obj Syntax
syntaxD(3)
ready

Revisions

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