Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster Reverse String Algorithm for JavaScript?

So I'm looking for the fastest possible Reverse String function.

Here are my function and all the functions that I found on the internet and their perfromance tests:

https://jsperf.com/javascript-reversing-string-performance

It looks like the fastest one (and the prettiest in my opinion) is this:

function reverseString(str) {
  return str.split().reverse().join("");
}

But maybe there is even more efficient, faster way to do this?

like image 552
Konrad Błachowicz Avatar asked Apr 25 '26 05:04

Konrad Błachowicz


1 Answers

This one seems to be even faster:

function reverseString(str) {
  let reversed = "";
  const l = str.length;
  for(let i = l-1; i >= 0; i--) {
    reversed = `${reversed}${str[i]}`;
  }
  return reversed;
}
like image 181
Max Podriezov Avatar answered Apr 26 '26 17:04

Max Podriezov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!