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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With