ECMA TC39 proposal for making mapping over Objects more concise
HTML
94
19 commits
updated Oct 30, 2020
After concerns were raised at the February 2020 meeting, this proposal is being withdrawn.
An ECMA TC39 proposal to improve the experience & performance of iterating and mapping over Objects
Objects are commonly used as the defacto dictionary type in Javascript. But Objects are not iterable by default, so many of the valuable collection methods on Iterables are not available for Objects without first transforming the Object into an Array1.
Iterator on an Object// Collect result back into an Object
Object.fromEntries(
// Utility method to get an iterator on an Object
Object.iterateEntries(obj)
// Standard `map` function that operates on iterables
.map(([key, value]) => [transform(key), transform(value)])
);
Object to provide in-place iteration on data stored therein:
Object.iterateEntries: iterates through the Object, providing key-value pairs much like Map.prototype.entriesObject.iterateKeys: iterates through the Object, providing just the keys like Map.prototype.keysObject.iterateValues: iterates through the Object, providing just the values like Map.prototype.valuesObject.iterateEntriesconst obj = { foo: 'bar', baz: 'blah' };
const entriesIterator = Object.iterateEntries(obj);
for (const [key, value] of entriesIterator) {
console.log(`${key} -> ${value}`);
}
Object.iterateKeysconst obj = { foo: 'bar', baz: 'blah' };
const keysIterator = Object.iterateKeys(obj);
for (const key of keysIterator) {
console.log(key);
}
Object.iterateValuesconst obj = { foo: 'bar', baz: 'blah' };
const valuesIterator = Object.iterateValues(obj);
for (const value of valuesIterator) {
console.log(value);
}
An unofficial polyfill is available in the core-js library.
This proposal has morphed a bit since original presentation.
The initial proposal was Object.map -- providing a static API method on Object to transform the keys and/or values
and return the transformed Object.
Slides: https://1drv.ms/p/s!As13Waij_jkUqeV6IHXsJBMDkNIgXw
There was general support for improving the experience and optimizability of mapping over Objects, but folks were
concerned about the slippery slope that adding a map method would create.
Instead, we agreed that the proposal could change to be an exploration on improving mapping over Objects and agreed on Stage 1.
After the October meeting, the champions & authors of this proposal and the Iterator Helpers proposal met and
discussed how the two proposals are related. It was determined that augmenting Iterator.from for this purpose
would not work, since it would be ambiguous for Objects that extend Iterator.prototype.
Since Iterator Helpers provides the needed map mechanism, this proposal could be adjusted to focus on getting
an Iterator from an Object.
Presented the change from "Object mapping" to "Object iteration"
Slides: https://1drv.ms/p/s!As13Waij_jkUqe0X3QmI7R9FfKahkw
Proposed spec text: https://tc39.es/proposal-object-iteration/
Alternate spec text: https://tc39.es/proposal-object-iteration/alt.html
Multiple implementors voiced concerns over the usefulness of this API. Folks believe that the existing Object.keys,
values, and entries are just fine and can be optimized easily enough. It's straightforward enough for an engine
to have a "fast" path that can just optimize away the array instantiation in the common case, with a slower path for
edge cases like when the Array prototype is polluted.
There was also concern over the second spec text option allowing modification of the underlying object during iteration.
This is consistent with existing collections such as Array, Set, and Map. But it would mean that moving from using keys
to iterateKeys would have a real semantic difference that could be a footgun.
Some delegates believe that it would be better to try to direct folks to more appropriate "dictionary" collections like
Map, which already provides the in-place iteration provided in the proposal as presented. There was agreement that
getting Maps from JSON was not ideal, so the next steps for this proposal may be to investigate improving revivers for
this purpose.
More investigation to follow.
1 Specifically, Object.entries, Object.keys, and Object.values
18 commits
1 commits
HTML
100.0%
ECMA TC39 proposal for making mapping over Objects more concise
HTML
94
19 commits
updated Oct 30, 2020
After concerns were raised at the February 2020 meeting, this proposal is being withdrawn.
An ECMA TC39 proposal to improve the experience & performance of iterating and mapping over Objects
Objects are commonly used as the defacto dictionary type in Javascript. But Objects are not iterable by default, so many of the valuable collection methods on Iterables are not available for Objects without first transforming the Object into an Array1.
Iterator on an Object// Collect result back into an Object
Object.fromEntries(
// Utility method to get an iterator on an Object
Object.iterateEntries(obj)
// Standard `map` function that operates on iterables
.map(([key, value]) => [transform(key), transform(value)])
);
Object to provide in-place iteration on data stored therein:
Object.iterateEntries: iterates through the Object, providing key-value pairs much like Map.prototype.entriesObject.iterateKeys: iterates through the Object, providing just the keys like Map.prototype.keysObject.iterateValues: iterates through the Object, providing just the values like Map.prototype.valuesObject.iterateEntriesconst obj = { foo: 'bar', baz: 'blah' };
const entriesIterator = Object.iterateEntries(obj);
for (const [key, value] of entriesIterator) {
console.log(`${key} -> ${value}`);
}
Object.iterateKeysconst obj = { foo: 'bar', baz: 'blah' };
const keysIterator = Object.iterateKeys(obj);
for (const key of keysIterator) {
console.log(key);
}
Object.iterateValuesconst obj = { foo: 'bar', baz: 'blah' };
const valuesIterator = Object.iterateValues(obj);
for (const value of valuesIterator) {
console.log(value);
}
An unofficial polyfill is available in the core-js library.
This proposal has morphed a bit since original presentation.
The initial proposal was Object.map -- providing a static API method on Object to transform the keys and/or values
and return the transformed Object.
Slides: https://1drv.ms/p/s!As13Waij_jkUqeV6IHXsJBMDkNIgXw
There was general support for improving the experience and optimizability of mapping over Objects, but folks were
concerned about the slippery slope that adding a map method would create.
Instead, we agreed that the proposal could change to be an exploration on improving mapping over Objects and agreed on Stage 1.
After the October meeting, the champions & authors of this proposal and the Iterator Helpers proposal met and
discussed how the two proposals are related. It was determined that augmenting Iterator.from for this purpose
would not work, since it would be ambiguous for Objects that extend Iterator.prototype.
Since Iterator Helpers provides the needed map mechanism, this proposal could be adjusted to focus on getting
an Iterator from an Object.
Presented the change from "Object mapping" to "Object iteration"
Slides: https://1drv.ms/p/s!As13Waij_jkUqe0X3QmI7R9FfKahkw
Proposed spec text: https://tc39.es/proposal-object-iteration/
Alternate spec text: https://tc39.es/proposal-object-iteration/alt.html
Multiple implementors voiced concerns over the usefulness of this API. Folks believe that the existing Object.keys,
values, and entries are just fine and can be optimized easily enough. It's straightforward enough for an engine
to have a "fast" path that can just optimize away the array instantiation in the common case, with a slower path for
edge cases like when the Array prototype is polluted.
There was also concern over the second spec text option allowing modification of the underlying object during iteration.
This is consistent with existing collections such as Array, Set, and Map. But it would mean that moving from using keys
to iterateKeys would have a real semantic difference that could be a footgun.
Some delegates believe that it would be better to try to direct folks to more appropriate "dictionary" collections like
Map, which already provides the in-place iteration provided in the proposal as presented. There was agreement that
getting Maps from JSON was not ideal, so the next steps for this proposal may be to investigate improving revivers for
this purpose.
More investigation to follow.
1 Specifically, Object.entries, Object.keys, and Object.values
18 commits
1 commits
HTML
100.0%