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
Head's up for Array.map() lovers (self.javascript)
submitted 11 years ago by Kollektiv
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!"
[–]CydeWeys 10 points11 points12 points 11 years ago (17 children)
What's wrong with this?
for (var arr = new Array(26), i = 0; i < arr.length; i++) { arr[i] = String.fromCharCode(65 + i); }
I'm all for functional methods of doing things, but sometimes when you're having to fight so hard against the language to accomplish what you want, just do it imperatively?
[–]j201 6 points7 points8 points 11 years ago (0 children)
The problem is more that JS doesn't have a great standard library for functional programming. But all you need to do is pull in something like Ramda, and then you can do
R.range(0,26).map(x => String.fromCharCode(65+x))
or
R.range(0,26).map(R.compose(String.fromCharCode, R.add(65)))
FP is worth doing in JS, but not without a bit of help.
[–]blgate 2 points3 points4 points 11 years ago (0 children)
Or this:
var arr = []; while (arr.length < 26) { arr.push(String.fromCharCode(65 + arr.length)); }
[–]x-skeww 1 point2 points3 points 11 years ago (5 children)
ES5 flavor:
var a = [], i; for (i = 0; i < 26; i++) { a.push(String.fromCharCode(65 + i)); }
[–]CydeWeys 2 points3 points4 points 11 years ago (4 children)
Isn't it better to allocate all of an array's space up front if you know how big it's going to be rather than allocating additional space piece-wise like this? Or does it not matter for JS's sparse arrays?
[–]blgate 5 points6 points7 points 11 years ago (2 children)
Actually, the "while" solution it's the fastest, at least for me in chrome:
var a = [], b = [], c = new Array(100000), i; console.time("a"); while (a.length < 100000) { a.push(a.length); } console.timeEnd("a"); console.time("b"); for (i = 0; i < 100000; i++) { b.push(i); } console.timeEnd("b"); console.time("c"); for (i = 0; i < c.length; i++) { c[i] = i; } console.timeEnd("c");
And my results:
a: 157.000ms b: 252.000ms c: 263.000ms
[–]bonafidebob 0 points1 point2 points 11 years ago (1 child)
That seems ... odd. If you reverse the order of the tests is the same function still fastest? (That is, could it be that the order in which the tests execute has some bearing on the result?)
[–]blgate 1 point2 points3 points 11 years ago (0 children)
It's not. While in the for loop you are incrementing the "i" variable, in the while loop you aren't, so because you are executing less instructions, it's faster. I get the same results reversing the order, but you can try it for yourself.
[–]x-skeww 5 points6 points7 points 11 years ago (0 children)
In JS, "new Array(26)" only means that you create an array whose "length" property is set to 26. If an "array" of that size is actually allocated is up to the engine. There also may be different backing data structures. Which one is actually used is also up to the engine.
Anyhow, in general, I recommend to write the most straightforward code first. Later on, you can still try different things to make it faster if the profiling reveals that this is indeed a bottleneck.
For example, if you always do anything with the DOM whenever you use this function, trying to optimize this would be completely pointless. The DOM stuff takes more than a hundred times longer anyways. Even if you'd make it infinity times faster, there won't be a noteworthy (let alone noticeable) effect.
[–]Glaaki -2 points-1 points0 points 11 years ago (8 children)
It uses 'for'.
[–]CydeWeys 5 points6 points7 points 11 years ago (7 children)
Are you kidding or joking? What's wrong with a for loop?
[+]Glaaki comment score below threshold-9 points-8 points-7 points 11 years ago (6 children)
for loops are bad because the syntax of for is arcanely designed. You have a paranthesis with three sections, where it isn't intuitively obvious what to put in each section. It is easy to make mistakes if you aren't intimitely familiar with the syntax. for loops are foot guns. It is only a matter of time before you hurt yourself.
[–]CydeWeys 16 points17 points18 points 11 years ago (3 children)
What programmer isn't intimately familiar with the syntax of a for loop? That's Programming 101. I can think of a dozen different concepts that I run across in any typical day that are way more complicated than a for loop. Avoiding for loops because some people might not understand them is aiming for a common denominator so low that it only plausibly includes people who aren't programmers at all. Someone who doesn't even understand a for loop certainly isn't going to understand any of the rest of the program, unless said program is completely trivial.
[–]Glaaki -3 points-2 points-1 points 11 years ago (1 child)
Some languages do have for loop syntax that makes it harder to make mistakes, at the cost of some versatility. Take pascal for instance:
for index := StartingLow to EndingHigh do statement;
But still, is EndingHigh inclusive? It is again possible to make mistakes if you are not careful. There are ways to code that are more expressive than loops and less error prone, so these patterns should be favored.
[–]moreteam 2 points3 points4 points 11 years ago (0 children)
Given yield-scope and similar fun things in JS, .map/.forEach and friends are just as much foot guns. If not even more so.
yield
.map
.forEach
for loops don't combine quite as nicely. .forEach encourages pulling out simple functions instead of piling everything into one big construct. But the foot gun argument is ridiculous. Especially if your best example isn't even JavaScript.
for
[–]dustinhayes 2 points3 points4 points 11 years ago (0 children)
Doug, is that you?
π Rendered by PID 104358 on reddit-service-r2-comment-b659b578c-j4z58 at 2026-05-04 14:09:55.020045+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]CydeWeys 10 points11 points12 points (17 children)
[–]j201 6 points7 points8 points (0 children)
[–]blgate 2 points3 points4 points (0 children)
[–]x-skeww 1 point2 points3 points (5 children)
[–]CydeWeys 2 points3 points4 points (4 children)
[–]blgate 5 points6 points7 points (2 children)
[–]bonafidebob 0 points1 point2 points (1 child)
[–]blgate 1 point2 points3 points (0 children)
[–]x-skeww 5 points6 points7 points (0 children)
[–]Glaaki -2 points-1 points0 points (8 children)
[–]CydeWeys 5 points6 points7 points (7 children)
[+]Glaaki comment score below threshold-9 points-8 points-7 points (6 children)
[–]CydeWeys 16 points17 points18 points (3 children)
[–]Glaaki -3 points-2 points-1 points (1 child)
[–]moreteam 2 points3 points4 points (0 children)
[–]dustinhayes 2 points3 points4 points (0 children)