switch vs if-else (v10)

Revision 10 of this benchmark created 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
and
type === 'boolean' && (r = 'boolean');
type === 'function' && (r = 'function');
type === 'object' && (r = 'object');
type === 'string' && (r = 'string');
type === 'number' && (r = 'number');
ready
if
if (type === 'boolean') {
  r = 'boolean';
}
if (type === 'function') {
  r = 'function';
}
if (type === 'object') {
  r = 'object';
}
if (type === 'string') {
  r = 'string';
}
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.