DEV Community

Discussion on: Stop abusing .map()!

Collapse
 
totally_chase profile image
Phantz • Edited

Actually, I was referring to the newer .entries methods. You know, Array.prototype.entries, Set.prototype.entries, Map.prototype.entries etc. They all return iterators and will not be slow.

Meanwhile the old and outdated Object.entries goes over the entire collection to build and return the array, which will then be iterated over by the user again. A truly unfortunate state. But hey, at least they are on the right path on the modern .entries impls!

Since we're on the topic, here's an efficient Object.entries impl instead-

function* objEntries<T>(x: Record<string, T>): IterableIterator<[string, T]> {
  for (const k in x) {
    yield [k, x[k]];
  }
}
Enter fullscreen mode Exit fullscreen mode

playground

Thread Thread
 
pitops profile image
Petros Kyriakou

yes i thought you were talking about Object.entries