question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prefer-object-from-entries

See original GitHub issue

Prefer Object.fromEntries() over Array#reduce() unicorn/prefer-object-from-entries

const demo= [{a: 1 b:3, c: 3},{a: 11 b:13, c: 13}]

const reduced = demo.reduce((acc, item) => {
  acc[item.a] = item.b

  return acc
}, {})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
fregantecommented, Sep 2, 2021

Better yet, no functions at all:

const demo= [{a: 1 b:3, c: 3},{a: 11 b:13, c: 13}]

const reduced = {};
for (const item of demo) {
	reduced[item.a] = item.b
}

.reduce() is mostly used as a generic loop. There’s nothing special in this case that justifies its awkward API.

Perf test
const array = Array.from({length: 1e2}, () => ['k', 'v'])

console.time('reduce');
for (let i = 0; i < 1e3; i++)
array.reduce((acc, item) => {
  acc[item[0]] = item[1]

  return acc
}, {})
console.timeEnd('reduce');

console.time('fromEntries');
for (let i = 0; i < 1e3; i++)
Object.fromEntries(
  array.map(item => [
    item[0], // key
    item[1], // value
  ]),
);
console.timeEnd('fromEntries');


console.time('for-of');
for (let i = 0; i < 1e3; i++) {
    const reduced = {};
    for (const item of array) {
        reduced[item[0]] = item[1]
    }
}
console.timeEnd('for-of');
VM237:10 reduce: 3.24609375 ms
VM237:20 fromEntries: 13.962158203125 ms
VM237:30 for-of: 5.65380859375 ms
0reactions
fiskercommented, Aug 6, 2021

I understand that there might be sight performance loss, but the second one is more readable to me.

I think this is acceptable.
const array = Array.from({length: 1e2}, () => ['k', 'v'])

console.time('reduce');
for (let i = 0; i < 1e3; i++)
array.reduce((acc, item) => {
  acc[item[0]] = item[1]

  return acc
}, {})
console.timeEnd('reduce');

console.time('fromEntries');
for (let i = 0; i < 1e3; i++)
Object.fromEntries(
  array.map(item => [
    item[0], // key
    item[1], // value
  ]),
);
console.timeEnd('fromEntries');
VM141:10 reduce: 4.244873046875 ms
VM141:20 fromEntries: 6.815185546875 ms

I don’t think there are not too many cases in real world that the array is already key-value pairs, most of them need to transform. Any real word cases can help us to understand why you don’t like it?

The one in the topic seems silly to me, if there are duplicated value of a property, it may result unexpected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-unicorn/prefer-object-from-entries.md at main
Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object. This rule is enabled in the ✓ recommended config.
Read more >
Object.fromEntries() - JavaScript - MDN Web Docs
The method returns an iterator object that produces two-element array-like objects. The first element is a value that will be used as a...
Read more >
JavaScript Object.fromEntries() | SamanthaMing.com
Object.fromEntries is the inverse of Object.entries. It will take key-value pairs and return a new object. You can use it on Arrays and...
Read more >
prefer-object-from-entries #1464 - Issuehunt
What is the problem? ... So, you are going to really suggest creating a new array just to have key/value pairs instead of...
Read more >
Benchmark: Object.fromEntries vs reduce - MeasureThat.net
fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) ... AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found