Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce creation
(version: 0)
Comparing performance of:
spread insert vs assign insert vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from({ length: 10000 }).map((_, idx) => idx)
Tests:
spread insert
data.reduce((acc, val) => ({ ...acc, [val]: val }), {})
assign insert
data.reduce((acc, val) => Object.assign(acc, {[val]: val}))
map
Object.fromEntries(data.reduce((acc, val) => acc.set(val, val), new Map()))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread insert
assign insert
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread insert
141.1 Ops/sec
assign insert
276.3 Ops/sec
map
1481.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark data and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The test measures the performance of three different approaches to inserting values into an object using the `reduce` method in JavaScript. The benchmarks compare: 1. Using the spread operator (`{...acc, [val]: val}`) for insertion. 2. Using `Object.assign()` for insertion. 3. Creating a new Map and setting values on it using the `set()` method. **Benchmark Definitions** The benchmark definitions are represented as JSON objects that describe the code to be executed. Each definition includes: * A JavaScript expression that uses the `reduce` method to process an array of numbers (`data`) and create an object. * The test name, which indicates the approach being tested (spread insert, assign insert, or map). Here's a brief explanation of each benchmark definition: 1. `data.reduce((acc, val) => ({ ...acc, [val]: val }), {})`: This code uses the spread operator to create a new object and inserts values into it. 2. `data.reduce((acc, val) => Object.assign(acc, {[val]: val}))`: This code uses `Object.assign()` to merge an existing object with a new object created using the spread operator. 3. `Object.fromEntries(data.reduce((acc, val) => acc.set(val, val), new Map()))`: This code creates a new Map and inserts values into it using the `set()` method. **Library Usage** In this benchmark, no external libraries are used. However, some built-in JavaScript features are utilized: * `Object.assign()`: A standard JavaScript method for merging objects. * `Map`: A built-in JavaScript object that maintains key-value pairs. * Spread operator (`{...}`): A syntax feature introduced in ECMAScript 2015. **Special JS Features or Syntax** None of the benchmark code uses special JavaScript features like async/await, Promises, or modern syntax (e.g., template literals). **Other Alternatives** For comparison purposes, other approaches to inserting values into an object could be: * Using a custom function with loops and objects. * Utilizing a library like Lodash (`_.assign()`). * Creating a new object using the `Object.create()` method. Keep in mind that these alternatives might not be as concise or efficient as the benchmarked approaches. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **Spread operator (`{...}`)**: * Pros: Concise, readable code. * Cons: May incur overhead due to object creation. 2. **`Object.assign()`**: * Pros: Efficient and optimized for merging objects. * Cons: Can be slower than the spread operator due to function calls. 3. **Map (`set()`) approach**: * Pros: Optimized for large datasets, as Map operations are faster. * Cons: May require more memory allocation. The `Map` approach is likely the most efficient way to insert values into an object in this benchmark, followed by the spread operator and then `Object.assign()`. I hope this explanation helps you understand what's being tested in the MeasureThat.net benchmark!
Related benchmarks:
Object spread vs New map
Object spread vs New map with string keys
Object.keys().length vs Map.size
Object spread vs New map entries
Map convert
Comments
Confirm delete:
Do you really want to delete benchmark?