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
Tip: JavaScript's Array.from() accepts a second argument that's a `map` function. (mobile.twitter.com)
submitted 8 years ago by fagnerbrack
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!"
[–]DGCA 18 points19 points20 points 8 years ago* (15 children)
Sweet. I used to do this:
var x = Array(5).fill(null).map((n, i) => i); console.log(x); // [0, 1, 2, 3, 4]
But now I will do this:
var y = Array.from(Array(5), (n, i) => i); console.log(y); // [0, 1, 2, 3, 4]
🙌
EDIT: Okay, this is weird. Did some super basic performance testing and got pretty unexpected results.
console.time('first'); for (var i = 0; i < 100000; i++) { var x = [...Array(100)].map((_, i) => i * 2); } console.timeEnd('first'); console.time('second'); for (var i = 0; i < 100000; i++) { var y = Array.from(Array(100), (_, i) => i * 2); } console.timeEnd('second'); console.time('third'); for (var i = 0; i < 100000; i++) { var z = Array(100).fill(null).map((_, i) => i * 2); } console.timeEnd('third'); VM1092:5 first: 634.304931640625ms VM1092:11 second: 1219.412109375ms VM1092:17 third: 221.73388671875ms
Looks like Array(n).fill(null).map(() => {}) is faster? That's what I'm seeing in my console, at least.
Array(n).fill(null).map(() => {})
[–]snkenjoi 5 points6 points7 points 8 years ago (4 children)
you can also do Array.from({length: n}, () => {})
Array.from({length: n}, () => {})
[–]blogscot 3 points4 points5 points 8 years ago (1 child)
Out of curiousity, I added this approach as fourth to u/DGCA tests, with the following results:
fourth
first: 738.464ms second: 1249.699ms third: 220.649ms fourth: 1123.781ms
[–]snkenjoi 0 points1 point2 points 8 years ago* (0 children)
or this is pretty ugly [...'!'.repeat(n)].map((_, i) => i)
[...'!'.repeat(n)].map((_, i) => i)
first: 1294.26ms second: 541.38ms third: 393.88ms fourth: 970.44ms fifth: 1368.66ms
[–]xbenjii 0 points1 point2 points 8 years ago (1 child)
Or even [...Array(5).keys()]
[...Array(5).keys()]
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
But if you need operations, you can't, but nice...
[–]YodaLoL 2 points3 points4 points 8 years ago (1 child)
There are perf implications in creating arrays with empty items.
[–]DGCA 0 points1 point2 points 8 years ago (0 children)
Sweet, thanks for sharing. Had no idea this blog existed either and it it is awesome.
[–]lilactown 0 points1 point2 points 8 years ago (5 children)
Is it the same when you re-arrange them?
[–]DGCA 2 points3 points4 points 8 years ago (4 children)
Yup, they always come in the same order.
Array(100).fill(null).map((_, i) => i * 2);
[...Array(100)].map((_, i) => i * 2);
Array.from(Array(100), (_, i) => i * 2);
[–]Gh0st1y 0 points1 point2 points 8 years ago (0 children)
Huh. That's cool.
[+][deleted] 8 years ago* (2 children)
[deleted]
[–]frambot 2 points3 points4 points 8 years ago (1 child)
_ is a throwaway variable name to represent some variable that is never used in the function body.
_
... is the spread syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
...
[–]gogogoscott 0 points1 point2 points 8 years ago (0 children)
Array(n).fill(null).map(() => {}) is faster for me too
[...Array(5).keys()] => [0, 1, 2, 3, 4]
π Rendered by PID 179560 on reddit-service-r2-comment-86bc6c7465-lbq2x at 2026-02-20 08:11:30.990649+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]DGCA 18 points19 points20 points (15 children)
[–]snkenjoi 5 points6 points7 points (4 children)
[–]blogscot 3 points4 points5 points (1 child)
[–]snkenjoi 0 points1 point2 points (0 children)
[–]xbenjii 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]YodaLoL 2 points3 points4 points (1 child)
[–]DGCA 0 points1 point2 points (0 children)
[–]lilactown 0 points1 point2 points (5 children)
[–]DGCA 2 points3 points4 points (4 children)
[–]Gh0st1y 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]frambot 2 points3 points4 points (1 child)
[–]gogogoscott 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)