switch vs Anonymous Function ({object})[value] (v2)

Revision 2 of this benchmark created by Mike McCaughan on


Description

Used correctly (i.e., creating the object before the lookup), anonymous objects can be much, much faster than switch statements.

Setup

var key = 'a',
        values = {
        "a": "aaa",
        "b": "bbb",
        "c": "ccc",
        "d": "ddd",
        "e": "eee"
        };

Test runner

Ready to run.

Testing in
TestOps/sec
switch
var value;

switch (key) {
case "a":
  value = "aaa";
  break;

case "b":
  value = "bbb";
  break;

case "c":
  value = "ccc";
  break;

case "d":
  value = "ddd";
  break;

case "e":
  value = "fff";
  break;
}
ready
anonymous function
var value = values[key];
ready

Revisions

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

  • Revision 1: published by heeju on
  • Revision 2: published by Mike McCaughan on