DEV Community

Discussion on: 20 Killer JavaScript One Liners ☝️

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

I made no claim to it being faster, but in this case the reduce method is almost twice as fast - jsbench.me/3tkttupth4/1 - at least on Chrome. On Firefox though, the situation is reversed. Different JS engines, different optimisations I guess

Collapse
 
webreflection profile image
Andrea Giammarchi

Faster !== Correct ... his string reverse breaks with code points, while [...str] doesn't.

Try it: OP reverse breaks the emoji with reverse('some 💩'); while this reduce suggestion doesn't.

Thread Thread
 
webreflection profile image
Andrea Giammarchi • Edited

P.S.

// this works fast and correct at the same time
const reverse = str => [...str].reverse().join('');
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
eioluseyi profile image
Emmanuel Imolorhe

In a bid to be obnoxious 😏 you could have shortened your code further by 2 characters 🌚

const reverse = str => [...str].reverse().join``;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lluismf profile image
Lluís Josep Martínez

But who has the need in the real world / real apps to reverse a string? Nobody.