Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
create temp object vs Object.fromEntries #2
(version: 0)
Comparing performance of:
Object.fromEntries vs creating temporary objects
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from(Array(10000).keys()).map(i => { var length = Math.round(Math.random()*100) var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; })
Tests:
Object.fromEntries
Object.fromEntries(array.map(value => [value, true]));
creating temporary objects
const data = {} array.forEach(value => data[value] = true);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
creating temporary objects
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
1586.3 Ops/sec
creating temporary objects
3229.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition and Script Preparation Code** The benchmark is designed to measure the performance difference between two approaches: 1. Creating temporary objects using `Array.forEach()`. 2. Using `Object.fromEntries()` with an array created from another array. The script preparation code generates an array of 10,000 random strings (each string has a length between 1 and 100) and maps over this array to create the following values: * `result`: an empty string * `characters`: a fixed string containing all uppercase and lowercase letters, numbers, and special characters * `charactersLength`: the length of the `characters` string This script preparation code is executed before running each benchmark. **Benchmark Preparation Code** The individual test cases define two benchmarks: 1. **Object.fromEntries()**: creates an object from an array using `Object.fromEntries()` with a mapping function that pairs each value in the array with a boolean value (`true`). 2. **Creating temporary objects**: uses `Array.forEach()` to create temporary objects by assigning `true` values to properties on an empty object. **Options Compared** The two approaches differ in how they handle data: 1. **Object.fromEntries()**: * Creates a new object with the specified entries. * Returns the new object. * Does not modify the original array or its elements. 2. **Creating temporary objects**: * Uses an empty object as a container. * Iterates over the array and assigns values to properties on this object. * Modifies the original array by consuming it. **Pros and Cons** * **Object.fromEntries()**: + Pros: creates a new object, easy to use, efficient in terms of memory allocation. + Cons: may involve additional overhead due to the creation of an intermediate object (depending on the JavaScript engine). * **Creating temporary objects**: + Pros: allows for direct manipulation of array elements, potentially faster for large datasets. + Cons: creates an empty object and iterates over the original array, which can be slower than `Object.fromEntries()`. **Library Usage** Neither approach uses a specific library. However, some JavaScript engines may optimize or provide better performance for these operations due to their built-in implementation or caching mechanisms (e.g., V8 in Chrome). **Special JS Feature/Syntax** The benchmark does not use any special JavaScript features or syntax that would require an in-depth explanation. **Other Alternatives** If you're interested in exploring other approaches, here are some alternatives: 1. **Using `reduce()`**: Instead of using `Array.forEach()`, you could use the `reduce()` method to create a new object from the array. 2. **Using `map()` with an object literal**: Similar to `Object.fromEntries()`, but without the explicit mapping function. Here's an example of how you could modify the second test case to use `reduce()`: ```javascript const data = {}; array.reduce((acc, value) => acc[value] = true, {}); ``` And here's an example using `map()` with an object literal: ```javascript const data = {}; array.map(value => ({ [value]: true })).reduce((acc, obj) => Object.assign(acc, obj), {}); ``` These alternatives may offer different performance profiles or trade-offs in terms of code readability and maintainability.
Related benchmarks:
Object vs Map
create temp object vs Object.fromEntries
comparing Map and object
Object.create(null) vs {} vs Map() key access (heavy)
Comments
Confirm delete:
Do you really want to delete benchmark?