Closed zloirock closed 7 years ago
Good call; this proposal should indeed assume all arrays are dense.
It would make sense to me that if you flatMap
an array with holes, the holes map to holes. .flatMap(x => [x])
should be the identity function for any array, but this change would make it eliminate holes.
I thought post-ES6 that array methods were pretending that holes don't exist; in other words, that undefined
maps to "own undefined
", whether it's originally own or not.
There are at least 3 possible results for [0, , 2].flatMap(x => [x])
:
[0, , 2]
[0, undefined, 2]
[0, 2]
The PR I opened (#31) was for alternative 2. I've added this question to my slides and will ask for committee feedback. As of right now, I still feel the current state of the proposal is best.
I vastly prefer either of the two alternatives over the current proposal.
Looking at other ES6+ methods, the first alternative looks like the most logical solution.
There's another disadvantage of "holes === own undefined", which is that it breaks the flatMap = map + flatten
symmetry.
I still prefer it, but it bears consideration.
In the actual proposal, we check the existence of properties before getting: It's a correct behaviour for ES5 array methods like
Array#map
: But this behaviour was changed for all new ES6+ methods because of additional overhead and performance problem with arrays with holes, for example,Array#find
: So maybe this proposal should use modern behaviour?