String Reversal Agorithm

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const sampleText = 'abcdefghijklmnopqrstuvwxyz'
  
  function reverseStringChaining(text) {
       return [...text].reverse().join('');
  }
  function reverseStringForLoop(text) {
      let result = "";
      for (let char of text) {
          result = char + result
      }
      return result;
  }
  function reverseStringRecursive(text) {
      if (text === "") {
          return ""
      } else {
          return reverseStringRecursive(text.substr(1)) + text[0]
      }
  }
  function reverseStringReduce(text) {
      return text.split("").reduce((acc, char) => char + acc);
  }

};
</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
Chaining Built In Methods
reverseStringChaining(sampleText)
pending…
Using For Loop
reverseStringForLoop(sampleText)
pending…
Using Recursion
reverseStringRecursive(sampleText)
pending…
Using .reduce()
reverseStringReduce(sampleText)
pending…

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

Compare results of other browsers

0 Comments