switch vs if-else

Benchmark created by John-David Dalton on


Setup

var type = typeof 1;

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (type) {
  case 'boolean':
    r = 'boolean';
    break;
  case 'function':
    r = 'function';
    break;
  case 'object':
    r = 'object';
    break;
  case 'string':
    r = 'string';
    break;
  case 'number':
    r = 'number';
    break;
}
 
ready
else-if
if (type === 'boolean') {
  r = 'boolean';
}
else if (type === 'function') {
  r = 'function';
}
else if (type === 'object') {
  r = 'object';
}
else if (type === 'string') {
  r = 'string';
}
else if (type === 'number') {
  r = 'number';
}
 
ready

Revisions

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