DEV Community

[Comment from a deleted post]
Collapse
 
jdelgit profile image
jdelgit

Have you looked at the performance impact of each method?

Collapse
 
thomasjunkos profile image
Thomas Junkツ

Performace of reversing »Hello«?

Collapse
 
nektro profile image
Meghan (she/her)

the first is the most straightforward, while the second is going to be the fastest by far compared to #1 and #3

Collapse
 
skovorodan profile image
Никита Сковорода

the second is going to be the fastest by far compared to #1 and #3

No, it isn't. Symbol-by-symbol string concatenation comes with significant penalties outside of microbenchmarks that immediately throw the result out.

 
nektro profile image
Meghan (she/her)

Perhaps, but reduce is more than a power of magnitude faster than split reverse join. It’s on jsperf

 
skovorodan profile image
Никита Сковорода

That microbench is taken out of context, «faster» there does not actually mean anything.

In an actual application where you would actually use the result instead of throwing it out immediately, char-by-char concatenation would consume up to an order of magnitude more memory and slow everything down.

My code to repoduce that is in a comment below, in another thread here.

Collapse
 
aspittel profile image
Ali Spittel • Edited

I think something like this would actually be the most performant:

function reverseStr(str){
  let newStr = ""
  for (let i = str.length-1; i >= 0; i--){
    newStr += str[i]
   }
   return newStr
}
Collapse
 
trueneu profile image
Pavel Gurkov

Wouldn't it still take quadratic time?

Collapse
 
theodesp profile image
Theofanis Despoudis

My attempt:

function reverseStr(str) {
    let newStr = str.split('');

    for (let j = str.length - 1, i = 0; i < j; i++, j--) {
        let temp = newStr[i];
        newStr[i] = newStr[j];
        newStr[j] = temp;
    }

    return newStr.join('');
}
 
skovorodan profile image
Никита Сковорода • Edited

That basically reimplements Array#reverse() from JS land at the second step.

Array#reverse() operates in-place, so there is no benefit here — you are doing exactly what .reverse() already does.

This approach is unlikely to be more performant than the built-in method, and just takes more code to write and maintain.

 
titi profile image
Thibault ROHMER

Yep. Also remember your usage scenario.
Are you in a browser ?
How large is the string ?
How many strings ?

Because if you're only reversing a 100 chars string, even 1000 chars, the implementation (amongst thoses proposed in this thread) doesn't matter much performance wise (memory, duration, cpu cycles)...
For clarity and code comprehension, it's something else sure ^

 
theodesp profile image
Theofanis Despoudis

This is a showcase, not a contest. Also, there is no point using reverse() to do a reverse operation. It's the same as trying to explain some term using the same term in a sentence.

 
nektro profile image
Meghan (she/her)

This would be best solution. Yes it’s a re implementation of array reverse but it skips the split and join and only uses 2 bytes of memory and n/2 time

 
nektro profile image
Meghan (she/her)

Oh it’s not what I thought it was

Collapse
 
caseycole589 profile image
Casey Cole

regular for loop will beat all these methods by huge margin
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}