tc39 / proposal-flatMap

proposal for flatten and flatMap on arrays
https://tc39.github.io/proposal-flatMap
214 stars 19 forks source link

Checking existence of properties #29

Closed zloirock closed 7 years ago

zloirock commented 7 years ago

In the actual proposal, we check the existence of properties before getting: image It's a correct behaviour for ES5 array methods like Array#map: image 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: image So maybe this proposal should use modern behaviour?

ljharb commented 7 years ago

Good call; this proposal should indeed assume all arrays are dense.

michaelficarra commented 7 years ago

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.

ljharb commented 7 years ago

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.

michaelficarra commented 7 years ago

There are at least 3 possible results for [0, , 2].flatMap(x => [x]):

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.

ljharb commented 7 years ago

I vastly prefer either of the two alternatives over the current proposal.

zloirock commented 7 years ago

Looking at other ES6+ methods, the first alternative looks like the most logical solution.

bakkot commented 7 years ago

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.