Pre-Compute Once Only

JavaScript performance comparison

Test case created by BretCameron

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function whichSideOfTheForce1(name) {
    const light = ['Luke', 'Obi-Wan', 'Yoda']; 
    const dark = ['Vader', 'Palpatine'];
    
    return light.includes(name) ? 'light' : 
      dark.includes(name) ? 'dark' : 'unknown';
  };
  
  function whichSideOfTheForce2(name) {
    const light = ['Luke', 'Obi-Wan', 'Yoda'];
    const dark = ['Vader', 'Palpatine'];
    return name => light.includes(name) ? 'light' :
      dark.includes(name) ? 'dark' : 'unknown';
  };
  

};
</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
Without a Closure
whichSideOfTheForce1('Luke');
whichSideOfTheForce1('Vader');
whichSideOfTheForce1('Anakin');
pending…
With a Closure
whichSideOfTheForce2('Luke');
whichSideOfTheForce2('Vader');
whichSideOfTheForce2('Anakin');
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