Performance of Assigning variables in JavaScript

JavaScript performance comparison

Test case created by Rob W

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  window.locatio = {};
  window.id="yahoo"; //Declare global variable

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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/";
}   
pending…
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;

}
pending…
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];
pending…
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];
pending…

Compare results of other browsers

Revisions

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

0 Comments