Performance of Assigning variables in JavaScript (v16)

Revision 16 of this benchmark created on


Description

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

Replaced location with locatio for the test.

Setup

window.locatio = {};
    window.id = "yahoo"; //Declare global variable
    preMap = {
      camelCase: "http://www.thecamelcase.com",
      jsFiddle: "http://www.jsfiddle.net",
      cricInfo: "http://www.cricinfo.com",
      apple: "http://www.apple.com",
      yahoo: "http://www.yahoo.com"
    };
    redir = function(id) {
      window.locatio.href = preMap[id];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
if-elseif
 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 === "apple") {
   window.locatio.href = "http://apple.com/";
 } else 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 '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",
  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",
  yahoo: "http://www.yahoo.com"
}[id];
ready
Dirty if-elseif
window.locatio.href =
  id == "camelCase" ? "http://www.thecamelcase.com" :
  (id == "jsfiddle" ? "http://jsfiddle.net/" :
  (id == "cricInfo" ? "http://cricinfo.com/" :
    (id == "apple" ? "http://apple.com/" :
      (id == "yahoo" ? "http://yahoo.com/" :
        window.locatio.href))));
ready
predefined map
window.locatio.href = preMap[id];
ready
redirect function
redir(id);
ready

Revisions

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