Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce creation x
(version: 0)
Comparing performance of:
spread insert vs assign insert vs map vs mutation vs tuple
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()))
mutation
data.reduce((acc, val) => acc[val] = val, {})
tuple
Object.fromEntries(data.map((v) => [v, v]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
spread insert
assign insert
map
mutation
tuple
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread insert
117.7 Ops/sec
assign insert
256.4 Ops/sec
map
1362.3 Ops/sec
mutation
1096.1 Ops/sec
tuple
2782.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. The benchmark is testing different approaches for inserting values into an object using JavaScript's `reduce` method. The test cases vary in how they use `reduce` to create or modify an object, including: 1. **Spread insert**: Using the spread operator (`{...}`) to create a new object and assign values. 2. **Assign insert**: Directly assigning values to the accumulator object using `Object.assign()`. 3. **Map**: Converting the accumulated data into a map (using `Object.fromEntries()`). 4. **Mutation**: Modifying the existing accumulator object directly by assigning new values. 5. **Tuple**: Creating an array of key-value pairs and converting it into an object using `Object.fromEntries()`. Let's analyze each approach: * **Spread insert**: * Pros: Creates a new object, which can be more efficient for large datasets since it avoids mutating the original object. * Cons: May require additional memory allocation, depending on the size of the data. * **Assign insert**: * Pros: Modifies the existing accumulator object directly, reducing memory allocation. * Cons: Can lead to unexpected behavior if the accumulator is not a suitable object type for assignment (e.g., not an object with own enumerable properties). * **Map**: * Pros: More functional programming style, can be easier to reason about and debug. * Cons: May require additional memory allocation, depending on the size of the data. * **Mutation**: * Pros: Directly modifies the existing accumulator object, which can reduce memory allocation. * Cons: Can lead to unexpected behavior if not used carefully. * **Tuple**: * Pros: Creates an array of key-value pairs and converts it into an object using `Object.fromEntries()`, providing a clear separation between data storage and access. * Cons: May require additional memory allocation, depending on the size of the data. Other considerations: * The test results show that **tuple** has the highest execution frequency (2782.295654296875), followed closely by **map** (1362.27099609375). * **Spread insert** and **mutation** have lower execution frequencies (256.4129333496094 and 117.69482421875, respectively). * **Assign insert** has the lowest execution frequency (1096.096435546875). Now, let's look at the library being used: There is no explicit mention of a specific library in the provided JSON. However, the use of `Object.fromEntries()` and `Array.from()` suggests that the test environment may be using modern JavaScript features that don't require external libraries. Special JS features or syntax: * The benchmark uses modern JavaScript features such as spread operator (`{...}`), arrow functions (`(acc, val) => {...}`, `(v) => [v, v]`), and `Object.fromEntries()`. Alternatives to MeasureThat.net: Some alternatives for testing JavaScript microbenchmarks include: 1. **jsPerf**: A JavaScript benchmarking tool that provides a simple way to measure the performance of different code snippets. 2. **Benchmark.js**: A lightweight benchmarking library that allows you to write and run benchmarks in your own projects. 3. **Jest**: A testing framework that can be used for benchmarking purposes, providing a more comprehensive set of features than MeasureThat.net. These alternatives offer varying degrees of complexity and functionality compared to MeasureThat.net, but they all aim to provide a reliable way to measure the performance of JavaScript code snippets.
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
Array.fill vs Array.from with dyamnic data
Comments
Confirm delete:
Do you really want to delete benchmark?