DEV Community

Cover image for 1 line of code: How to reverse a string
Martin Krause
Martin Krause

Posted on

1 line of code: How to reverse a string

const reverseString = str => [...str].reverse().join("");
Enter fullscreen mode Exit fullscreen mode

Returns the reversed string.


The repository & npm package

You can find the all the utility functions from this series at github.com/martinkr/onelinecode
The library is also published to npm as @onelinecode for your convenience.

The code and the npm package will be updated every time I publish a new article.


Follow me on Twitter: @martinkr and consider to buy me a coffee

Photo by zoo_monkey on Unsplash


Subscribe to the weekly modern frontend development newsletter


Top comments (8)

Collapse
 
martinkr profile image
Martin Krause

Hi everyone,

thanks to everyone for the efortand the thoughtful contributions.
I setup a benchmark with all one-line contributions. For me, on chrome, the article's function is the fastest with 400 ops/s.
Interested to see which results you guys get

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

I prefer to hard code my reversed strings πŸ₯

Collapse
 
frankwisniewski profile image
Frank Wisniewski • Edited

the fastest solution in one line:

const reverseString=([firstChar,...str])=>firstChar?reverseString(str)+firstChar:''  
Enter fullscreen mode Exit fullscreen mode

It's a nice game to write functions in one line. But nothing more...

const areverseString=([firstChar,...str])=>firstChar?areverseString(str)+firstChar:''
//duration of 1.000.000 calls = 478 ms 

const breverseString = str => [...str].reverse().join("");
//duration of 1.000.000 calls = 1210 ms 

const creverseString = str => [...str].reduce((a,c) => c.concat(a));
//duration of 1.000.000 calls = 943 ms 

//now the classical way....
const reverseString = function (str) {
  let length = str.length
  let reverse = ''
  for (let i = 0; i<length; i++ ){
    reverse = str[i] + reverse
  }
  return reverse
}
//duration of 1.000.000 calls = 210 ms 

let start = performance.now()  
for (let y=1; y<1000000 ;y++){
  reverseString('Sauerkrauttopf');
}
let duration = performance.now() - start;
console.log(duration)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lexlohr profile image
Alex Lohr

If this was about speed, it would probably be faster not to use an array at all:

const reverseString = str => { for (let r = '', i = str.lenght; i >= 0;) r += str[--i]; return r }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
frankwisniewski profile image
Frank Wisniewski

Uncaught ReferenceError: r is not defined...

Thread Thread
 
lexlohr profile image
Alex Lohr

My bad, typed that on the cellphone.

const reverseString = str => { let r = ''; for (let i = str.lenght; i >= 0;) r += str[--i]; return r }
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
frankwisniewski profile image
Frank Wisniewski

The function doesn't return anything...it seems to be an old turntable phone..;-)

Thread Thread
 
lexlohr profile image
Alex Lohr • Edited

I'll try it later when I have my PC.

Found the issue, but it is indeed slower.