This article was originally published on my blog
The reverse a string problem is a common algorithm problem. In this article, we will consider fou...
For further actions, you may consider blocking this person and/or reporting abuse
Actually, you could take the recursion along a bit further and make it a prototype of String, viz:
Someone'll beat me to a blog post, likely.
It's generally recommended that you should not extend native prototypes.
developers.google.com/web/updates/...
shakes fist at MooTools
Wouldn't solution #2 (and maybe some of the others) cause an O(n2 ) problem?
If str has N elements, and you do character + reverseString each time you get n*(n+1)/2 characters copied (ie. O(n2 )).
Whereas option#1 presumably uses only O(n) functions so it should be much faster as data grows and less stress on the GC.
I think it would be best to compare these with benchmarks (with large examples). Because I've found that the one place where code can get super slow is with successive concatenation in a for loop (as in option#2).
Thanks for the suggestion and the analysis. In subsequent posts, I try to compare the solutions based on their performance.
What about an old classic way:
This is a good improvement to the solution 2. It reduces the loop time by half. Thanks for sharing.
.split('')
can be shortened:Also good because split will fail badly for some utf8 things like emojis
I'm not sure what you'd call this solution, but it does reverse the string:
That's the first time I've written code that uses .call(). Won't be the last time!
Short, sweet:
I'd forgotten about that third parameter into map. Short, sweet, superb!
So after doing .call(), let's do .apply(), viz
Okay, maybe it's time to do some real work now.
Going nuts here. More ideas, and for something as mundane as a reverse function. Monomania?
Your mileage may vary on flip4's
.fill()
call. I'm having to use polyfills (long story) and if I don't fill the Array,.map()
assumes that there's nothing there to work on.I do have a couple of ideas yet to exorcise.
This recursion thing is pretty cool. Here's another approach both as a String.prototype and as a standalone function:
The approach is to take the first character and put it last, take the last character and put it first, and do that to the stuff in the middle.
It's been through the Closure Compiler, thus the
1 >= this.length
.Another solution using for loop, but only iterates half the time.
already had this topic
What is this crazy black magic I'm looking at?
first thing i thought of was
reduceRight
(works in the opposite direction toreduce
), with nosplit
orjoin
:)Nice idea for blog posts! It is helpful for people wanting to learn and helpful for you to practice the algos! 💯
Thank you, Martin.
certainly not very fast, but I'd like destructuring assignment and recursion :)
Just don't use any emoji with skin color modifier though...
"🤷🏻♂️" split on '' becomes 7 "strings"
"🤷🏻♂️".split('').reverse().map(c => c.charCodeAt(0).toString(16))
// ["fe0f", "2642", "200d", "dffb", "d83c", "dd37", "d83e"]
That reversed looks... weird.
"️♂�🄷�"
Would be great to see a performance comparison between then all.
Yes, I'll consider adding that in subsequent posts. Thanks for the suggestion.
You can find code snippets here. I did the same a couple of days ago
github.com/vyasriday/js-snippets/b...