Performance of Assigning variables in JavaScript (v14)

Revision 14 of this benchmark created by anonymous on


Description

http://stackoverflow.com/q/8624939/

Replaced location with locatio for the test.

Extended cases

Inside a for loop

random choose the id, and construct the map only once in test 3.

Setup

window.locatio = {};
    var ids = ['camelCase', 'jsFiddle', 'cricInfo', 'apple', 'nope1', 'nope2', 'nope3', 'nope4', 'nope5', 'nope6', 'nope7', 'nope8', 'nope9', 'nope0', 'yahoo'];
    var hrefMap = {
      camelCase: "http://www.thecamelcase.com",
      jsFiddle: "http://www.jsfiddle.net",
      cricInfo: "http://www.cricinfo.com",
      apple: "http://www.apple.com",
      nope1: "http://nope1z.cricinfo.com/",
      nope2: "http://nope2nn.cricinfo.com/",
      nope3: "http://nope3aaa.cricinfo.com/",
      nope4: "http://nope4kkkk.cricinfo.com/",
      nope5: "http://nope5mmmmmm.cricinfo.com/",
      nope6: "http://nope6tootot.cricinfo.com/",
      nope7: "http://nope7nonemen.cricinfo.com/",
      nope8: "http://nope8meingikl.cricinfo.com/",
      nope9: "http://nope9meignimls.cricinfo.com/",
      nope0: "http://nope0.cricinfo.com/",
      yahoo: "http://www.yahoo.com"
    };
    window.id = ids[Math.floor(15 * Math.random())]; //Declare global variable

Test runner

Ready to run.

Testing in
TestOps/sec
if-elseif
for (var i = 0; i < 1500; i++) {
  if (id === "camelCase") {
    window.locatio.href = "http://www.thecamelcase.com";
  }
  if (id === "jsFiddle") {
    window.locatio.href = "http://jsfiddle.net/";
  }
  if (id === "cricInfo") {
    window.locatio.href = "http://cricinfo.com/";
  }
  if (id === "apple") {
    window.locatio.href = "http://apple.com/";
  }
  if (id === "nope1") {
    window.locatio.href = "http://nope1z.cricinfo.com/";
  }
  if (id === "nope2") {
    window.locatio.href = "http://nope2nn.cricinfo.com/";
  }
  if (id === "nope3") {
    window.locatio.href = "http://nope3aaa.cricinfo.com/";
  }
  if (id === "nope4") {
    window.locatio.href = "http://nope4kkkk.cricinfo.com/";
  }
  if (id === "nope5") {
    window.locatio.href = "http://nope5mmmmmm.cricinfo.com/";
  }
  if (id === "nope6") {
    window.locatio.href = "http://nope6tootot.cricinfo.com/";
  }
  if (id === "nope7") {
    window.locatio.href = "http://nope7nonemen.cricinfo.com/";
  }
  if (id === "nope8") {
    window.locatio.href = "http://nope8meingikl.cricinfo.com/";
  }
  if (id === "nope9") {
    window.locatio.href = "http://nope9meignimls.cricinfo.com/";
  }
  if (id === "nope0") {
    window.locatio.href = "http://nope0.cricinfo.com/";
  }
  if (id === "yahoo") {
    window.locatio.href = "http://yahoo.com/";
  }
}
ready
Switch
for (var i = 0; i < 1500; i++) {
  switch (id) {
    case 'camelCase':
      window.locatio.href = "http://www.thecamelcase.com";
      break;
    case 'jsFiddle':
      window.locatio.href = "http://www.jsfiddle.net";
      break;
    case 'cricInfo':
      window.locatio.href = "http://www.cricinfo.com";
      break;
    case 'apple':
      window.locatio.href = "http://www.apple.com";
      break;
    case "nope1":
      window.locatio.href = "http://nope1z.cricinfo.com/";
      break;
    case "nope2":
      window.locatio.href = "http://nope2nn.cricinfo.com/";
      break;
    case "nope3":
      window.locatio.href = "http://nope3aaa.cricinfo.com/";
      break;
    case "nope4":
      window.locatio.href = "http://nope4kkkk.cricinfo.com/";
      break;
    case "nope5":
      window.locatio.href = "http://nope5mmmmmm.cricinfo.com/";
      break;
    case "nope6":
      window.locatio.href = "http://nope6tootot.cricinfo.com/";
      break;
    case "nope7":
      window.locatio.href = "http://nope7nonemen.cricinfo.com/";
      break;
    case "nope8":
      window.locatio.href = "http://nope8meingikl.cricinfo.com/";
      break;
    case "nope9":
      window.locatio.href = "http://nope9meignimls.cricinfo.com/";
      break;
    case "nope0":
      window.locatio.href = "http://nope0.cricinfo.com/";
      break;
    case 'yahoo':
      window.locatio.href = "http://www.yahoo.com";
      break;
  }
}
ready
hrefMap
for (var i = 0; i < 1500; i++) {
  window.locatio.href = hrefMap[id];
}
ready
Direct location.href
for (var i = 0; i < 1500; i++) {
  window.locatio.href = {
    camelCase: "http://www.thecamelcase.com",
    jsFiddle: "http://www.jsfiddle.net",
    cricInfo: "http://www.cricinfo.com",
    apple: "http://www.apple.com",
    nope1: "http://nope1z.cricinfo.com/",
    nope2: "http://nope2nn.cricinfo.com/",
    nope3: "http://nope3aaa.cricinfo.com/",
    nope4: "http://nope4kkkk.cricinfo.com/",
    nope5: "http://nope5mmmmmm.cricinfo.com/",
    nope6: "http://nope6tootot.cricinfo.com/",
    nope7: "http://nope7nonemen.cricinfo.com/",
    nope8: "http://nope8meingikl.cricinfo.com/",
    nope9: "http://nope9meignimls.cricinfo.com/",
    nope0: "http://nope0.cricinfo.com/",
    yahoo: "http://www.yahoo.com"
  }[id];
}
ready

Revisions

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