Reverse String

JavaScript performance comparison

Test case created by deemaxx

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const rev = 'ffeJ si eman ym iH'

};
</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
String For Loop
function forLoopStringReversal(str) {
  let newStr = '';
  
  for (let i = 0; i < str.length; i++) {
    newStr = `${str[i]}${newStr}`;
  }
  return newStr;
}

forLoopStringReversal(rev);
pending…
Array For Loop
function stringReverse(str) {
  let newArr = [];
  
  for (let i = str.length - 1; i >= 0; i--) {
    newArr.push(str[i]);
  }
  return newArr.join('');
}

stringReverse(rev);
pending…
Reverse method
function stringReversal(str) {
  return str.split('').reverse().join('');
}

stringReversal(rev);
pending…
ES6+ built-ins
const es6ReversalMethod = str => [...str].reverse().join('');

es6ReversalMethod(rev);
pending…
Recursive
function recursiveStringReversal(str) {
  let newStr = '';
  
  if (!str.length) return '';

  newStr = recursiveStringReversal(str.slice(1)) + str[0]; 
  return newStr;
}

recursiveStringReversal(rev);
pending…

Revisions

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

0 Comments