Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries VS multiple reduce from list of data
(version: 1)
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) from list of data
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects)
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(5000).keys());
Tests:
Object.fromEntries
Object.fromEntries(data.map((key) => [key, key]));
Reduce (reuse object)
Object.entries(data).reduce((acc, key) => { acc[key] = key.toString(); return acc; }, {});
Reduce (creating temporary objects)
Object.entries(data).reduce((acc, key) => ({ ...acc, [key]: key.toString() }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and test cases. **Benchmark Definition** The benchmark compares three approaches to create an object from a list of key-value pairs: 1. `Object.fromEntries(data.map((key) => [key, key]));` 2. `Object.entries(data).reduce((acc, key) => {\r\n acc[key] = key.toString();\r\n return acc;\r\n}, {});` 3. `Object.entries(data).reduce((acc, key) => ({\r\n ...acc,\r\n [key]: key.toString()\r\n}), {});` These approaches are used to create an object where each key is a unique value from the input list. **Options Compared** The benchmark compares three options: 1. `Object.fromEntries`: A modern JavaScript method that creates an object from an array of key-value pairs. 2. `Reduce (reuse object)`: An approach that uses the `reduce` method to create an object, reusing the existing accumulator object and appending new properties to it. 3. `Reduce (creating temporary objects)`: An approach that uses the `reduce` method to create a new object for each iteration, without reusing any object. **Pros and Cons** 1. **Object.fromEntries**: This approach is concise and easy to read. It's also likely to be optimized by modern JavaScript engines. * Pros: Concise, efficient * Cons: May not be supported in older browsers or environments 2. **Reduce (reuse object)**: This approach reuses the existing accumulator object, which can lead to better performance due to reduced object creation overhead. * Pros: Efficient, reduces object creation overhead * Cons: Can be less readable and more error-prone due to manual property management 3. **Reduce (creating temporary objects)**: This approach creates a new object for each iteration, which may lead to increased memory allocation and deallocation overhead. * Pros: Easy to understand and implement * Cons: Inefficient due to excessive object creation **Library and Purpose** None of the test cases rely on any external libraries. They use built-in JavaScript methods and syntax. **Special JS Features or Syntax** There is no special JavaScript feature or syntax used in these test cases. However, it's worth noting that `Object.fromEntries` was introduced in ECMAScript 2015 (ES6) and may not be supported in older browsers or environments. **Other Alternatives** If you need to create an object from a list of key-value pairs, you can also use the spread operator (`{...data.map((key) => [key, key])}`) or the `Object.create()` method with an initial object as a third argument. These alternatives may offer different trade-offs in terms of readability, performance, and compatibility. For example: * Using the spread operator: `{...data.map((key) => [key, key])}` * Using `Object.create()`: `Object.create(null).reduce((acc, key) => {\r\n acc[key] = key.toString();\r\n return acc;\r\n}, {})`
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries on array vs reduce on array vs reduce(reuse object) - 100 products
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?