Gematria Performance

JavaScript performance comparison

Test case created by patrickroberts

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function gematria_original(text){
    var newText = text.toLowerCase(); // handle capital letters
    var startValue=0; //initialize value to zero
    for(let i=0 ; i<newText.length; i++){ // loop through every letter on the text
      let currentCharValue = newText.charCodeAt(i) - 96; // get the distance from the 'a'
      startValue +=currentCharValue;
    }
    return startValue
  }
  
  function gematria_nina(text) {
      return [...text].reduce((s, c) => s + parseInt(c, 36) - 9, 0);
  }
  
  const lut = Array.from(
    Array(128).keys(),
    index => String.fromCharCode(index).toLowerCase().charCodeAt(0) - 96
  );
  
  function gematria_patrick(text) {
    let startValue = 0;
  
    for (let i = 0; i < text.length; ++i) {
      startValue += lut[text.charCodeAt(i)];
    }
  
    return startValue;
  }
  
  const gematria_jonas = text => [...text].reduce((s, c) => s + c.toLowerCase().charCodeAt(0) - 96, 0);

};
</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
Original implementation
gematria_original("someText");
pending…
Nina's implementation
gematria_nina("someText");
pending…
Patrick's implementation
gematria_patrick("someText");
pending…
Jonas' implementation
gematria_jonas("someText");
pending…

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

0 Comments