Performance of Assigning variables in JavaScript (v26)

Revision 26 of this benchmark created by Matt McCray on


Description

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

Replaced location with locatio for the test.

Setup

window.locatio = {};
    window.id = "yahoo"; //Declare global variable
    
    window.cachedHrefMap = {
      camelCase: "http://www.thecamelcase.com",
      jsFiddle: "http://www.jsfiddle.net"
    };

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/";
 }
ready
Switch
switch (id) {
  case 'camelCase':
    window.locatio.href = "http://www.thecamelcase.com";
    break;
  case 'jsFiddle':
    window.locatio.href = "http://www.jsfiddle.net";
    break;
}
ready
hrefMap
var hrefMap = {
  camelCase: "http://www.thecamelcase.com",
  jsFiddle: "http://www.jsfiddle.net"
};
window.locatio.href = hrefMap[id];
ready
Direct location.href
window.locatio.href = {
  camelCase: "http://www.thecamelcase.com",
  jsFiddle: "http://www.jsfiddle.net"
}[id];
ready
Cached HrefMap
window.locatio.href = cachedHrefMap[id];
ready

Revisions

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