Why do we right code like this?
if (s[i] != s[s.length - 1 - i]) {
This allocates 2 1-character strings, and performs a string comparison (not a character comparison, there is no character datatype in Javascript).
If efficiency is our criteria, then we would write
if (s.charCodeAt(i) !== s.charCodeAt(s.length - 1 - i)) {
That is great, how does that perform compared to what I wrote?
That is, of course, the right question.
In Node 12, it seems to make no difference at all. Maybe they optimize to the same thing.
Results may vary in other runtimes.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Why do we right code like this?
This allocates 2 1-character strings, and performs a string comparison (not a character comparison, there is no character datatype in Javascript).
If efficiency is our criteria, then we would write
That is great, how does that perform compared to what I wrote?
That is, of course, the right question.
In Node 12, it seems to make no difference at all. Maybe they optimize to the same thing.
Results may vary in other runtimes.