Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Adding new key in object
(version: 0)
Comparing performance of:
reduce with same obj vs creating new object vs fromEntries
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = Object.entries({ ...Array.from(Array(10000).keys()) });
Tests:
reduce with same obj
data.reduce((map, [price, vol]) => { map[price] = vol; return map; }, {});
creating new object
data.reduce((map, [price, vol]) => { return { ...map, [price]: vol, }; }, {});
fromEntries
Object.fromEntries(data);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce with same obj
creating new object
fromEntries
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 provided benchmark. **Overview** The benchmark measures the performance of three different approaches for adding a new key to an object in JavaScript: 1. `reduce` with the same object (map existing values) 2. Creating a new object and then assigning the new key-value pair 3. Using `Object.fromEntries` These benchmarks are run on Chrome 107, a desktop browser, running on macOS 10.15.7. **Options compared** The three options compared in this benchmark: 1. **`reduce` with same object**: This approach uses the `reduce` method to iterate over an array of key-value pairs and add new values to an existing object. 2. **Creating a new object**: In this approach, a new object is created explicitly and then the new key-value pair is assigned to it. 3. **`Object.fromEntries`**: This approach uses the `fromEntries` method to create a new object from an array of key-value pairs. **Pros and Cons** 1. **`reduce` with same object**: * Pros: Efficient use of memory, as no extra object creation is needed. * Cons: Can be slower for large datasets due to the overhead of iterating over the entire array, even if only new values are being added. 2. **Creating a new object**: * Pros: Simple and easy to understand, with clear intent of creating a new object. * Cons: Creates an extra object in memory, which can be wasteful for large datasets. 3. **`Object.fromEntries`**: * Pros: Modern and concise syntax, reducing the need for explicit object creation. * Cons: May have additional overhead due to the complexity of creating a new object from entries. **Library and purpose** None of the benchmarked approaches use any external libraries or dependencies. **Special JS feature or syntax** The benchmark uses modern JavaScript features: 1. **`Object.entries()`**: Returns an array of key-value pairs from an object. 2. **`Array.from()`**: Creates a new array from an iterable (in this case, `Array(10000).keys()`). 3. **`reduce()`**: A method that applies a function to each element in an array and returns the accumulated value. 4. **`Object.fromEntries()`**: A static method that creates a new object from an array of key-value pairs. **Other alternatives** If you want to benchmark similar approaches, consider: 1. Using `Object.assign()` or the spread operator (`{ ... }`) instead of `reduce`. 2. Creating objects using other methods, such as `Object.create()` or `class` declarations. 3. Comparing different data structures, like arrays or sets, for adding new elements. Keep in mind that these alternatives might not be representative of real-world use cases and may have varying performance characteristics depending on the specific scenario.
Related benchmarks:
Object keys vs Array map v2
Map vs Object with Number Keys
entries vs keys lookup
Map convert
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?