Performance of Assigning variables in JavaScript (v21)

Revision 21 of this benchmark created by Billy on


Description

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

Replaced location with locatio for the test.

Extended cases

Inside a for loop

Setup

window.locatio = {};
    window.id = "yahoo"; //Declare global variable

Test runner

Ready to run.

Testing in
TestOps/sec
if
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 === "nope1") {}
if (id === "nope2") {}
if (id === "nope3") {}
if (id === "nope4") {}
if (id === "nope5") {}
if (id === "nope6") {}
if (id === "nope7") {}
if (id === "nope8") {} 
if (id === "nope9") {}
if (id === "nope0") {}
if (id === "apple") {
  window.locatio.href = "http://apple.com/";
}
if (id === "yahoo") {
  window.locatio.href = "http://yahoo.com/";
}
 
ready
Switch
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":
  break;
case "nope2":
  break;
case "nope3":
  break;
case "nope4":
  break;
case "nope5":
  break;
case "nope6":
  break;
case "nope7":
  break;
case "nope8":
  break;
case "nope9":
  break;
case "nope0":
  break;
case 'yahoo':
  window.locatio.href = "http://www.yahoo.com";
  break;
}
ready
hrefMap
var hrefMap = {
  camelCase: "http://www.thecamelcase.com",
  jsFiddle: "http://www.jsfiddle.net",
  cricInfo: "http://www.cricinfo.com",
  apple: "http://www.apple.com",
  nope1: "",
  nope2: "",
  nope3: "",
  nope4: "",
  nope5: "",
  nope6: "",
  nope7: "",
  nope8: "",
  nope9: "",
  nope0: "",
  yahoo: "http://www.yahoo.com"
};
window.locatio.href = hrefMap[id];
 
ready
Direct location.href
window.locatio.href = {
  camelCase: "http://www.thecamelcase.com",
  jsFiddle: "http://www.jsfiddle.net",
  cricInfo: "http://www.cricinfo.com",
  apple: "http://www.apple.com",
  nope1: "",
  nope2: "",
  nope3: "",
  nope4: "",
  nope5: "",
  nope6: "",
  nope7: "",
  nope8: "",
  nope9: "",
  nope0: "",
  yahoo: "http://www.yahoo.com"
}[id];
ready
indexof
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(['nope1','nope2','nope3','nope4','nope5','nope6','nope7','nope8',      
   'nope9','nope0'].indexOf(id)>-1) {}

if (id === "apple") {
  window.locatio.href = "http://apple.com/";
}
if (id === "yahoo") {
  window.locatio.href = "http://yahoo.com/";
}
 
ready
if-else if
if (id === "camelCase") {
  window.locatio.href = "http://www.thecamelcase.com";
} else if (id === "jsFiddle") {
  window.locatio.href = "http://jsfiddle.net/";
} else if (id === "cricInfo") {
  window.locatio.href = "http://cricinfo.com/";
} else if (id === "nope1") {}
else if (id === "nope2") {}
else if (id === "nope3") {}
else if (id === "nope4") {}
else if (id === "nope5") {}
else if (id === "nope6") {}
else if (id === "nope7") {}
else if (id === "nope8") {} 
else if (id === "nope9") {}
else if (id === "nope0") {}
else if (id === "apple") {
  window.locatio.href = "http://apple.com/";
}
else if (id === "yahoo") {
  window.locatio.href = "http://yahoo.com/";
}
 
ready
if-else if and indexOf
if (id === "camelCase") {
  window.locatio.href = "http://www.thecamelcase.com";
} else if (id === "jsFiddle") {
  window.locatio.href = "http://jsfiddle.net/";
} else if (id === "cricInfo") {
  window.locatio.href = "http://cricinfo.com/";
} else if(['nope1','nope2','nope3','nope4','nope5','nope6','nope7','nope8',      
   'nope9','nope0'].indexOf(id)>-1) {}
else if (id === "apple") {
  window.locatio.href = "http://apple.com/";
} else if (id === "yahoo") {
  window.locatio.href = "http://yahoo.com/";
}
ready
switch2
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":
case "nope2":
case "nope3":
case "nope4":
case "nope5":
case "nope6":
case "nope7":
case "nope8":
case "nope9":
case "nope0":
  break;
case 'yahoo':
  window.locatio.href = "http://www.yahoo.com";
  break;
}
ready

Revisions

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