4 ms·
Can save more bits by doing: Array.from({ length: 10000 }, button.click);
by halfmatthalfcat 6y ago
Can save more bits by doing:
Array.from({ length: 10000 }, button.click);
- phogster 6y agoNever heard of a "from" loop before.
- franky47 6y agoArray.from lets you build an array from an iterable source (often used in conjunction with `new Set` to eliminate duplicates), or in this case, of a fixed length.
- deleted 6y ago[deleted]
- klyrs 6y agoNot JavaScript, but... https://en.m.wikipedia.org/wiki/COMEFROM https://en.m.wikipedia.org/wiki/COMEFROM
- franky47 6y agoI did not know this variant, thanks. Having to fill the array with anything (even `undefined`) to iterate on it seemed such a weird concept (but then again, JS is weird).
- bawolff 6y agonew Array(1000); ?
- franky47 6y agoTry iterating on it, it won't work. This creates 1000 "empty slots".
- jholman 6y agoIn addition to franky47's more important point, I'll add that `new Array()` and `Array()` are equivalent.
- The_rationalist 6y agoIn java it would be default initialized but it isn't in js
- a1371 6y agoThe spread operator does not have the same quirk. I use it but it's still a bit weird tough [...new Array(1000)].map(something)
- ficklepickle 6y agoYou can drop the new [...Array(1000)] I think that is the least bits. Would love to be proven wrong :D
- franky47 6y agoIt may be shorter, but I like the spread syntax best used sparingly, it's harder to grasp why it was necessary there at a glance. That's what I like in the elegance of Array.from({ length: 1000 }): it's a hack, but it reads naturally.
- antonvs 6y ago"Harder to grasp" only lasts until you're mildly familiar with the idiom.
- trehalose 6y ago[...Array(1e3)]
- warent 6y agoI this awesome... I've always done Array.from(new Array But your approach makes perfect sense and is so much cleaner. Thank you! If anyone is wondering why it works, it's because everything in JavaScript is an object. So, an array is an object with a length prop. Apparently Array.from just needs any object with a length prop to work
- kvirani 6y agoSomething about ducks?
- chpmrc 6y ago"If it walks like a duck, and it quacks like a duck, then it must be an object"
- erhk 6y agoOf it has feathers, then as far as I'm concerned it is a duck. Similarly, when I pick up a feathered pen in real life I become a duck
- DonHopkins 6y ago"If it walks like a duck, and it quacks like a duck, then it must be created by a QuackEnumeratorWalkManangerRepositoryProxyFactoryFactoryAdapterBroker." -Java
- chmod775 6y agoduckduckduck.duck is still available, by the way. Well, .duck isn't a TLD yet and it may not be publicly available... https://icannwiki.org/.duck https://icannwiki.org/.duck
- Freak_NL 6y agoNow gogo.duck and go.duck would be nice aliases for DuckDuckGo. (There is ddg.gg of course.)
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]