Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce round 2
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (creating temporary objects)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys());
Tests:
Object.fromEntries
Object.fromEntries(data.map((key) => [key, 'x']));
Reduce (creating temporary objects)
data.reduce((acc, k) => ({ ...acc, [k]: 'x' }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (creating temporary objects)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
21 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
2539.1 Ops/sec
Reduce (creating temporary objects)
109.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined as an object with several properties: * `Name`: The name of the benchmark, which in this case is "Object.fromEntries vs reduce round 2". * `Description`: An empty string, indicating that there is no description for this benchmark. * `Script Preparation Code` and `Html Preparation Code`: These are code snippets that are executed before running each test case. In this case, they both create an array of keys using `Array.from(Array(10000).keys())`. **Individual Test Cases** There are two test cases: 1. **Object.fromEntries** The benchmark definition is: `Object.fromEntries(data.map((key) => [key, 'x']));` This uses the `Object.fromEntries()` method to create an object from an array of key-value pairs. 2. **Reduce (creating temporary objects)** The benchmark definition is: `data.reduce((acc, k) => ({\r\n ...acc,\r\n [k]: 'x'\r\n}), {});` This uses the `Array.prototype.reduce()` method to create an object from an array of key-value pairs. The accumulator (`acc`) is an empty object that gets updated with new properties in each iteration. **Options Compared** These two test cases compare the performance of: * **Object.fromEntries()**: A modern JavaScript method for creating objects from arrays of key-value pairs. * **Reduce (creating temporary objects)**: An older method using `Array.prototype.reduce()` to create objects from arrays, which involves creating a temporary object in each iteration. **Pros and Cons** ### Object.fromEntries() Pros: * **Concise and expressive**: The syntax is simple and easy to read. * **Efficient**: It creates an object by copying the key-value pairs from the array, making it efficient for large datasets. Cons: * **Support**: Not all older browsers or environments support this method (it was introduced in ES6). * **Security**: It's possible to create objects with arbitrary keys and values using `Object.fromEntries()`, which can lead to security issues if not used carefully. ### Reduce (creating temporary objects) Pros: * **Wide browser support**: This method has been around for a long time, so most browsers and environments support it. * **No memory allocation overhead**: The accumulator is passed by reference, avoiding the need to allocate new memory in each iteration. Cons: * **Verbose syntax**: The code can be more cumbersome due to the need to use object literals and bracket notation. * **Performance**: Creating temporary objects in each iteration can lead to performance issues for large datasets. **Libraries and Special Features** There is no library being used in this benchmark. However, if you're interested in using a specific library or function that's not part of the standard JavaScript syntax, it might be related to other features like: * `Array.prototype.map()`: Used in both test cases to transform the data array. * `Object.assign()` or `Object.create()`: Alternative methods for creating objects from arrays. **Other Alternatives** If you're looking for alternative approaches, consider: * Using a library like Lodash or Ramda, which provide more concise and expressive functions for working with arrays and objects. * Utilizing modern JavaScript features like `for...of` loops or `Array.prototype.forEach()` to iterate over the data array. Keep in mind that these alternatives might come with trade-offs in terms of performance, browser support, or security.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries VS multiple reduce from list of data
Comments
Confirm delete:
Do you really want to delete benchmark?