4 ms·
It is not necessary to create any extraneous data structures to do OP's one-liner. This creates n+1 new arrays that must be garbage-collected.
by keville 5y ago
It is not necessary to create any extraneous data structures to do OP's one-liner. This creates n+1 new arrays that must be garbage-collected.
- runarberg 5y agoObject.fromEntries takes iterable, so if you have a map from a handy iterator library you could get rid of the parent array returned from `bar.map`. import { map } from "my-iter-lib"; Object.fromEntries(map((v) => [v.id, v], bar)); Or with the proposed iterator-helpers: Object.fromEntries( Iterator.from(bar).map((v) => [v.id, v]), ) However the inner arrays are harder to get rid of... In fact even OP’s oneliner defines the inner arrays. I would hope there were some engine optimizations though which could minimize their footprint.
- keville 5y agoPlease see my other comment in this thread for the `reduce`-based solution that requires no extra data structures. They're not that hard to get rid of!