Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs Immer vs Spread Reducer With Filled State
(version: 0)
Comparing performance of:
Object.assign vs Mutate vs Spread vs immer pull up
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/immer@3.1.3/dist/immer.umd.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function getItems(count) { let id = 1; return _.times(count, () => ({ name: "city" + id++, visited: true })) } data = getItems(1000).reduce((acc, curr) => ({ ...acc, [curr.name]: curr.visited }), {})
Tests:
Object.assign
const reducer = (state, curr) => { return Object.assign(state, {curr }) }; reducer(data, 'yolo')
Mutate
const reducer = (draft, curr) => { draft[curr] = curr; return draft; }; reducer(data, 'yolo')
Spread
const reducer = (draft, curr) => ({ ...draft, [curr]: curr }); reducer(data, 'yolo')
immer pull up
const reducer = immer.produce((draft, curr) => { draft[curr] = curr; }); reducer(data, 'yolo')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.assign
Mutate
Spread
immer pull up
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):
The provided benchmark definition and test cases measure the performance of different approaches to update an object in JavaScript. **What is being tested?** * Four test cases are compared: + `Object.assign`: Using `Object.assign` to merge two objects, where one object contains new properties, and the other object contains existing properties. + `Mutate`: Mutating a shallow copy of an array (specifically, an object with same-name properties) by assigning values directly to it. + `Spread`: Using the spread operator (`{ ... }`) to create a new object that includes all key-value pairs from another object and adds new ones. + `immer pull up`: Using Immer's `produce` function to create a new object with merged values, while preserving existing properties. **Options compared** * The four test cases compare the performance of different approaches to update an object. Each approach has its own pros and cons: **Object.assign** Pros: Simple, widely supported, and efficient. Cons: Can be slower for large objects due to its recursive nature. **Mutate** Pros: Fast and simple, as it modifies a shallow copy of an array/object directly. Cons: May not preserve existing properties or handle complex data structures well. **Spread** Pros: Creates a new object with merged values, preserving existing properties and handling complex data structures well. Cons: Can be slower due to the creation of a new object and potential deep copying. **immer pull up** Pros: Preserves existing properties, handles complex data structures efficiently, and is designed for safety in state management. Cons: May require more overhead due to the use of Immer's `produce` function. **Other considerations** * The benchmark assumes that the input object (`data`) has a nested structure with same-name properties. This makes `Object.assign` less suitable than the other approaches. * The test cases do not consider deeper object hierarchies or more complex data structures, which may affect performance differently. **Library explanation** * Immer is a JavaScript library for managing state changes in React applications and other applications that require predictable updates to objects. It provides functions like `produce`, `draft`, and others for safely updating objects without mutations. * Lodash (lodash) is a utility library that includes the `times` function used in the script preparation code. **Special JS feature/syntax** None of these benchmark cases specifically use special JavaScript features or syntax, such as async/await, Promises, or modern language features like arrow functions, destructuring, or classes.
Related benchmarks:
Immer vs Spread Reducer With Filled State
Immer vs Spread Only Reducer With Filled State
Immer vs Spread
Immer vs Spread Immer
Comments
Confirm delete:
Do you really want to delete benchmark?