Pre-Compute Once Only

Benchmark created by BretCameron on


Setup

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';
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Without a Closure
whichSideOfTheForce1('Luke');
whichSideOfTheForce1('Vader');
whichSideOfTheForce1('Anakin');
ready
With a Closure
whichSideOfTheForce2('Luke');
whichSideOfTheForce2('Vader');
whichSideOfTheForce2('Anakin');
ready

Revisions

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

  • Revision 1: published by BretCameron on