switch vs if-else (v33)

Revision 33 of this benchmark created 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
and or
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
and or expression
var type = a.type;
r = type === 'boolean' && 'boolean' || type === 'function' && 'function' || type === 'object' && 'object' || type === 'string' && 'string' || type === 'number' && 'number' || type === 'null' && 'null' || type === 'undefined' && 'undefined';
ready
switch 2
var t = a.type;
switch (t) {
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

Revisions

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