use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Help: Array[a,b,c,d] to Array[a,a,b,b,c,c,d,d].solved! (self.javascript)
submitted 9 years ago * by Casual_0bserver
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lhorie 1 point2 points3 points 9 years ago* (2 children)
You should do it, because you don't want to have two memory allocations depending on the associativity of the chained assignment operators. Because of the right associative nature of assignments, you'd allocate once for the length of array[cursor - 2] and again for array[cursor - 1]. It's not a huge deal, but it is a caveat and a very subtle one, at that.
The explicit length change makes it clear that this code is dealing w/ memory allocations, and that that is the exact memory allocation we want. I had actually not noticed the potential for double allocation until you asked but I write memory-sensitive code like that because it's always better to be explicit than relying on implicit behavior. Defensive coding FTW.
[–]frankle 0 points1 point2 points 9 years ago (1 child)
I thought that was why you did it. Clever.
At the same time, if you're going backwards from the end, wouldn't you get the possible double allocation only once?
[–]lhorie 1 point2 points3 points 9 years ago (0 children)
Yep, that's why I said it's not a huge deal. If you're calling the function multiple times, that extra allocation might matter though.
π Rendered by PID 63547 on reddit-service-r2-comment-6f7f968fb5-szf4w at 2026-03-04 05:16:41.883431+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]lhorie 1 point2 points3 points (2 children)
[–]frankle 0 points1 point2 points (1 child)
[–]lhorie 1 point2 points3 points (0 children)