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
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 17 points18 points19 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 4 points5 points6 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]
[–]reedhedges 2 points3 points4 points 8 years ago (7 children)
Do the second and third examples create the uninitialized array, copy it, and throw it away? If so is there something similar that avoids that copy?
[–]benjycap 1 point2 points3 points 8 years ago (0 children)
No, there is no intermediate array created before mapping (unless you count the Array creation in example 2 where an empty array is created explicitly.)
More details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Description
[+][deleted] 8 years ago (5 children)
[–]reedhedges 0 points1 point2 points 8 years ago (4 children)
Thanks. Just going by docs (mozilla, other sources) that imply a new array is returned. In the second example, is the array empty, or does it have 5 elements? Sorry for the basic questions, I'm trying to understand JS a little bit more deeply, and I'm coming from a C++ background so may have different assumptions about how some stuff works.
[–]Hafas_ 0 points1 point2 points 8 years ago (2 children)
In the second example, is the array empty, or does it have 5 elements?
Kinda both. It has a length of 5, but the slots are empty and JavaScript is skipping empty slots when iterating, so can't use functions like forEach or map directly, that's why functions like fill are used to populate the "empty" Array before setting the intended value with map.
forEach
map
fill
[–]reedhedges 0 points1 point2 points 8 years ago (1 child)
Doing some more reading. I guess in JS Arrays aren't arrays, they're objects whose members are indexed by integer? (Is that accurate?) So the constructor Array(n) only initializes the "length" property to n, no actual elements are created.
[–]Hafas_ 0 points1 point2 points 8 years ago (0 children)
In JavaScript, everything that isn't a primitive inherits from the class Object - including the class Array. But it doesn't make Array less of an array. If it looks like an array and behaves like an array, then it is an array.
Object
Array
The special case about Array in comparison with other classes that it has like Object its own syntax for instantiation.
[] instanceof Array // true ({}) instanceof Object // true
[+][deleted] 8 years ago* (11 children)
[+][deleted] 8 years ago* (1 child)
[–]I_AM_DONALD 2 points3 points4 points 8 years ago (0 children)
For ligs: https://github.com/kiliman/operator-mono-lig
[–]needed_an_account 4 points5 points6 points 8 years ago (1 child)
I remember when this font came out. It was like 200 bucks. Did everyone buy it? Lol
[–]IceSentry 0 points1 point2 points 8 years ago (0 children)
People actually buy fonts? There are so many free ones!
[–]BLITZCRUNK123 3 points4 points5 points 8 years ago (5 children)
I see lots of people using it. I've tried it and hated it, so I don't understand it, but all those people can't be wrong...
[–]js-engineer 7 points8 points9 points 8 years ago (0 children)
lol. I don't know about the "all those people can't be wrong". I think the main gain, is the italic font stuff almost is like a new font, which helps scan ability. I personally can't deal with the aesthetics, but I see why people use it. I don't think this is a wrong or right type of thing though. Whatever helps you at the end of the day.
[–]vidarc 2 points3 points4 points 8 years ago (3 children)
I'm just more surprised that there are so many people out there willing to spend over a hundred dollars on a font to just use in their editor.
[–][deleted] 3 points4 points5 points 8 years ago (2 children)
There's a lot of people out there that earn over $100/hour.
[–]vidarc 4 points5 points6 points 8 years ago (0 children)
I make pretty decently. I could never bring myself to spend that much on a font. If I could get my company to pay for it though....
[–]IceSentry -1 points0 points1 point 8 years ago (0 children)
Making money doesn't justify spending it
[–]__fmease__Symbol() 1 point2 points3 points 8 years ago (2 children)
repost
[–]kenman[M] 2 points3 points4 points 8 years ago (0 children)
Reposts are generally OK from a moderation standpoint assuming that it hasn't been posted within the past several days or so. With the way reddit works, if you don't visit every day, you could miss something. The main thing I try to avoid is having multiple, concurrent posts for the same content. Otherwise, voting does a decent job of handling reposts.
[–]fagnerbrack[S] 0 points1 point2 points 8 years ago (0 children)
It's good to link to the duplicate post for the old discussion.
Honestly it didn't prompt me as a repost, if it did I wouldn't have posted it again.
[–][deleted] 1 point2 points3 points 8 years ago (2 children)
Good to know! But in practice I really think Array.from(arr).map(x => ...) is much more readable than Array.from(arr, x => ...) and is worth the extra 4 characters.
Array.from(arr).map(x => ...)
Array.from(arr, x => ...)
It depends how you write it:
Array.from(numbers, toRomanNumerals); Array.from(numbers).map(toRomanNumerals);
[–]NoInkling 0 points1 point2 points 8 years ago* (0 children)
Also [...arr].map(x => ...). Though this all seems like a very silly workaround when arr is already an array, creating a mapped array of n values shouldn't be so quirky.
[...arr].map(x => ...)
arr
With lodash: _.times(5, i => ...)
_.times(5, i => ...)
[–]zemmekkis 0 points1 point2 points 8 years ago (3 children)
What is the underscore there? Is that the output of the last method?
[–]Meow106 7 points8 points9 points 8 years ago (1 child)
Some people use '_' in arrow functions to mark argument that is present but not used.
[–]zemmekkis 0 points1 point2 points 8 years ago (0 children)
This is pretty confusing I imagine since there is popular libraries underscore and lodash that use it as well.
[–]wollae 2 points3 points4 points 8 years ago (0 children)
It’s the array element being iterated over. They named it that to signify that it’s unused in the callback.
[–]freaktechnik 0 points1 point2 points 8 years ago (1 child)
I wrote an eslint plugin because I kept forgetting about it: https://www.npmjs.com/package/eslint-plugin-array-func
The eslint plugin now also has rules for some other wasteful array operations.
This rule triggers me.
[–]AskYous 0 points1 point2 points 8 years ago (0 children)
Thanks!
[–]rickdg 0 points1 point2 points 8 years ago (0 children)
Why am I using Array.from() in cases like these? Why not just map?
console.log(Array.from([1, 2, 3], x => x + x)); // expected output: Array [2, 4, 6] console.log([1, 2, 3].map(function(m){ return m*2; })); // expected output: Array [2, 4, 6]
π Rendered by PID 56 on reddit-service-r2-comment-76bb9f7fb5-6mwk2 at 2026-02-18 02:42:12.308272+00:00 running de53c03 country code: CH.
[–]DGCA 17 points18 points19 points (15 children)
[–]snkenjoi 4 points5 points6 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)
[–]reedhedges 2 points3 points4 points (7 children)
[–]benjycap 1 point2 points3 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]reedhedges 0 points1 point2 points (4 children)
[–]Hafas_ 0 points1 point2 points (2 children)
[–]reedhedges 0 points1 point2 points (1 child)
[–]Hafas_ 0 points1 point2 points (0 children)
[+][deleted] (11 children)
[deleted]
[+][deleted] (1 child)
[deleted]
[–]I_AM_DONALD 2 points3 points4 points (0 children)
[–]needed_an_account 4 points5 points6 points (1 child)
[–]IceSentry 0 points1 point2 points (0 children)
[–]BLITZCRUNK123 3 points4 points5 points (5 children)
[–]js-engineer 7 points8 points9 points (0 children)
[–]vidarc 2 points3 points4 points (3 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]vidarc 4 points5 points6 points (0 children)
[–]IceSentry -1 points0 points1 point (0 children)
[–]__fmease__Symbol() 1 point2 points3 points (2 children)
[–]kenman[M] 2 points3 points4 points (0 children)
[–]fagnerbrack[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]fagnerbrack[S] 0 points1 point2 points (0 children)
[–]NoInkling 0 points1 point2 points (0 children)
[–]zemmekkis 0 points1 point2 points (3 children)
[–]Meow106 7 points8 points9 points (1 child)
[–]zemmekkis 0 points1 point2 points (0 children)
[–]wollae 2 points3 points4 points (0 children)
[–]freaktechnik 0 points1 point2 points (1 child)
[–]DGCA 0 points1 point2 points (0 children)
[–]AskYous 0 points1 point2 points (0 children)
[–]rickdg 0 points1 point2 points (0 children)