Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Sparse arrays case #12

Open
medikoo opened this issue Feb 14, 2018 · 6 comments
Open

Sparse arrays case #12

medikoo opened this issue Feb 14, 2018 · 6 comments

Comments

@medikoo
Copy link

medikoo commented Feb 14, 2018

Should it ignore sparse array characteristics?

e.g. if I read current spec, it'll work as:

var arr = new Array(3);
arr[0] = 'foo';
arr[1] = 'bar';
arr.last(); // undefined not 'bar';

Similarly with:

var arr = [0, 1, 3];
arr.length = 100;
arr.last(); // undefined

Is that expected?

@ljharb
Copy link
Member

ljharb commented Feb 14, 2018

Very good question.

Most ES5 methods, like forEach etc, seem to respect sparsensss, whole most ES6+ methods seem to ignore it. Concptually, while one might expect a sparse array’s “last” item to be the last item it actually contains, i think “length minus one” is probably a simpler and more consistent meaning for it.

@jridgewell
Copy link
Member

Which ES6 features? All I can think of is destructuring.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2018

All the ES6+ array iteration methods will interact with every index, present or not, eg:

  • Array.from
  • find
  • findIndex
  • includes
  • fill

Also flatten and flatMap will do the same.

@zloirock
Copy link
Contributor

zloirock commented Feb 14, 2018

@jridgewell Array.from, Array#{find, findIndex, fill, includes, keys, values, entries, @@iterator}, destructuring.

@jridgewell
Copy link
Member

Huh, I had no idea those were different (minus iterator).

@medikoo
Copy link
Author

medikoo commented Feb 15, 2018

I take that seeing it as ES2015+ feature, it should stay as it is, and it was officially stated that spareness should be ignored as a design side effect and not a real feature (hence not supported in all new methods).

I rely on util which respects sparseness, but it was implemented that way only to confirm well to ES5 state of things. I don't remember single use case where I would rely on that behavior

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants