switch vs if-else (v54)

Revision 54 of this benchmark created by Hien Nguyen on


Description

Going to exclude "typeof" performance looses

Setup

var a = {
      type: typeof HHHH
    };
    var r = '';

Test runner

Ready to run.

Testing in
TestOps/sec
switch
switch (a.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;
case 'null':
  r = 'null';
  break;
case 'undefined':
  r = 'undefined';
  break;
}
ready
else-if
var type = a.type;
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';
} else if (type === 'null') {
  r = 'null';
} else if (type === 'undefined') {
  r = 'undefined';
}
ready
and
var type = a.type;
type === 'boolean' && (r = 'boolean');
type === 'function' && (r = 'function');
type === 'object' && (r = 'object');
type === 'string' && (r = 'string');
type === 'number' && (r = 'number');
type === 'null' && (r = 'null');
type === 'undefined' && (r = 'undefined');
ready
if
var type = a.type;
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';
}
if (type === 'null') {
  r = 'null';
}
if (type === 'undefined') {
  r = 'undefined';
}
ready

Revisions

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