Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs Spread Reduce4
(version: 0)
Comparing performance of:
immer vs Spread
Created:
6 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(100)
Tests:
immer
data.reduce((acc, curr) => { return immer.produce({}, draft => { draft[curr.name] = curr.visited; }); }, {});
Spread
data.reduce((acc, curr) => ({ ...acc, [curr.name]: curr.visited, }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
immer
Spread
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 and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two approaches to update an object in JavaScript: 1. `Immer`: Using the Immer library to create a draft of the original object and then updating it. 2. `Spread`: Using the spread operator (`...`) to create a new object and then assigning values to its properties. **Script Preparation Code** The script preparation code defines two functions: `getItems` and `data`. The `getItems` function creates an array of objects with 100 items, each with a unique `name` property. The `data` variable is assigned this array. The HTML preparation code includes links to the Immer library (`immer.umd.min.js`) and Lodash library (`lodash.min.js`). **Individual Test Cases** There are two test cases: 1. **Immer**: The benchmark definition uses the `immer.produce` function to create a draft of the original object (`data`) and then updates it using the spread operator. ```javascript data.reduce((acc, curr) => { return immer.produce({}, draft => { draft[curr.name] = curr.visited; }); }, {}); ``` The purpose of this approach is to create a safe and efficient way to update an object by creating a new draft that can be manipulated without affecting the original object. Pros: * Safe: Immer ensures that the original object is not modified accidentally. * Efficient: Using `immer.produce` reduces the number of assignments required to update the object. Cons: * Overhead: Creating a new draft and managing its lifecycle may introduce additional overhead compared to other approaches. 2. **Spread**: The benchmark definition uses the spread operator (`...`) to create a new object and then assigns values to its properties. ```javascript data.reduce((acc, curr) => ({ ...acc, [curr.name]: curr.visited }), {}); ``` The purpose of this approach is to create a lightweight way to update an object by creating a new object with the updated properties. Pros: * Lightweight: Using the spread operator creates a new object without introducing additional overhead. * Fast: Assigning values to existing properties can be faster than using `immer.produce`. Cons: * Insecure: If not used carefully, this approach can lead to unexpected side effects or errors when working with large objects. **Library and Syntax** The Immer library is used to create a safe and efficient way to update an object. Its purpose is to provide a simple and intuitive API for managing drafts of objects. No special JavaScript features or syntax are required for this benchmark. It only relies on standard JavaScript language features, such as arrays, objects, and the spread operator. **Alternatives** Other alternatives to compare in similar benchmarks might include: * Using `Object.assign()` instead of Immer * Using a library like React's `useReducer` hook or MobX * Implementing a custom update function using recursion or iteration These alternatives would require careful consideration of their pros and cons, depending on the specific use case and performance requirements.
Related benchmarks:
Spread Operator vs Lodash with not so many items
Native map & filter vs reduce with spread
Lodash map & filter vs reduce with push and desctructuring (10 000 samples )
Native map & filter vs reduce with push
Lodash map & filter vs reduce with spread (2)
Comments
Confirm delete:
Do you really want to delete benchmark?