Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce, using arrays
(version: 0)
Comparing performance of:
Object.fromEntries vs reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var total = 10000; var ids = Array.from(Array(total).keys()); var data = ids.map(id => ({ id, foo: 'bar', name: 'John' }));
Tests:
Object.fromEntries
Object.fromEntries(data.map(({ id, ...others }) => [id, { ...others, v: 1, works: true }]))
reduce
data.reduce((acc, { id, ...others }) => { acc[id] = { ...others, v: 1, works: true }; return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
reduce
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 and explain what's being tested. **Benchmark Definition** The benchmark is testing two approaches to create an object from an array of key-value pairs: 1. `Object.fromEntries(data.map(({ id, ...others }) => [id, { ...others, v: 1, works: true }]))` 2. `data.reduce((acc, { id, ...others }) => {\r\n acc[id] = { ...others, v: 1, works: true };\r\n return acc;\r\n}, {})` **Options Compared** The two approaches are being compared in terms of performance (measured by the number of executions per second). **Pros and Cons** * `Object.fromEntries`: + Pros: - More concise and expressive code. - Less error-prone, as it uses a standardized method for creating objects from arrays. + Cons: - May be slower due to the overhead of the JavaScript engine's built-in methods. * `reduce`: + Pros: - Can be faster, as it's implemented in native code and can take advantage of loop optimizations. + Cons: - Requires more manual memory management (assigning values to the accumulator object) and error handling. **Library** There is no specific library being used in this benchmark. However, `Array.from` and `Object.fromEntries` are both part of the modern JavaScript API. **Special JS Feature or Syntax** The benchmark uses a feature introduced in ECMAScript 2017 (ES7): `Object.fromEntries`. This method was added to provide a concise way to create objects from arrays of key-value pairs. **Other Considerations** * The benchmark is running on Chrome 113, which may have some performance optimizations or differences compared to other browsers. * The test case uses a relatively small dataset (`total` variable is set to 10000), which might not accurately represent real-world scenarios where datasets can be much larger. * There's no error handling in the benchmark code; if any errors occur during execution, they will not be caught or reported. **Alternatives** If you want to write similar benchmarks for other languages or libraries, consider the following: * For Java 8+: Use `Stream.collect()` instead of `reduce()`. * For Python: Use a library like `pandas` or `numpy` to create dataframes from arrays. * For C++: Use `std::map` or `std::unordered_map` to create maps from arrays. Keep in mind that the specific implementation and syntax may vary depending on the language and library being used.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce - convert Array to Object
Object.fromEntries on array vs reduce on array
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?