Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread operator vs. Array.map and lodash.set
(version: 3)
A comparison of the performance of the spread operator, [ ...array ], vs. Array.map(i => ...) and
Comparing performance of:
Spread operator vs Spread operator v2
Created:
8 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
permutations = new Array(10000).map(() => ({ pagination: 'blah blah blah' })); function updatePagination(action, prev) { return action; } action = 'something else'; state = { data: { permutations: [] } }; index = 5;
Tests:
Spread operator
const updated = Object.assign([...permutations], { [index]: Object.assign({}, permutations[index], { pagination: updatePagination(action, permutations[index]) }) }) return { ...state, data: { ...state.data, permutations: updated } };
Spread operator v2
const updated = Object.assign([...permutations], { [index]: { ...permutations[index], pagination: updatePagination(action, permutations[index]) } }) return { ...state, data: { ...state.data, permutations: updated } };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread operator
Spread operator v2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread operator
75015.3 Ops/sec
Spread operator v2
79680.3 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares different approaches to updating an object within a larger array. Let's break down the scenarios: * **Scenario:** You have an array of objects (`permutations`) and need to modify a specific object at a given index (`index`). The modification involves changing the `pagination` property. **Test Cases:** 1. **"Spread operator"**: This test case uses the spread syntax (`...`) to create a new array. It assigns a newly constructed object (with updated `pagination`) at the specified index within this new array. The result is then merged back into the original state object. * **Pros:** Can be concise and readable. 2. **"Spread operator v2"**: This test case also uses the spread syntax but assigns a modified version of the existing object directly to the new array. * **Pros:** Similar conciseness and readability as the first approach. **Libraries Used:** * **Lodash:** While not directly used in these tests, lodash is mentioned in the HTML preparation code. Lodash is a library commonly used for utility functions in JavaScript, including those related to object manipulation. It's possible the original benchmark intent included comparing lodash's `set` method against the spread syntax approaches. **Considerations:** * **Performance:** The benchmark aims to measure the execution speed of these different techniques. Spread syntax is generally efficient for array manipulations, but its performance compared to other methods (like `map`) can vary depending on the size and complexity of your data. * **Readability:** Both spread syntax approaches are considered fairly readable. The choice between them might come down to personal preference or the specific context of your code. **Alternatives:** 1. **`Array.map()`**: * You could iterate through the array using `map()` and update the object at the desired index within the mapped array. This approach often provides good control over the modification process. 2. **Original Object Modification (if mutable):** * If your objects are mutable, you might directly modify the `pagination` property of the existing object within the array. However, this can have implications for immutability and potential side effects in larger applications. Let me know if you'd like a deeper dive into any specific aspect!
Related benchmarks:
Array.prototype.map vs Lodash.map on large data
Spread Operator vs Lodash with not so many items
lodash range vs Array.from vs keys() + spread + fill
Array.slice() vs. Spread operator
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?