<script src="https://cdn.jsdelivr.net/npm/immer@9.0.13/dist/immer.umd.production.min.js"></script>
const createList = () =>
Array.from({ length: 100 }, (_, i) => ({ id: i, value: `Item ${i}` }));
const list = createList();
const nextItem = { id: "x", value: "I'm next" };
const updatedList = immer.produce(list, (draft) => {
draft.push(nextItem);
});
const updatedList = [list, nextItem];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immer | |
spread |
Test name | Executions per second |
---|---|
immer | 167956.1 Ops/sec |
spread | 14294164.0 Ops/sec |
This benchmark compares two different methods of adding an item to a list in JavaScript—using the Immer library's produce
function versus the spread operator syntax. The main goal is to evaluate their performance in terms of execution speed.
Script Preparation Code: This code snippet defines a function createList
that generates an array of objects with an id
and a value
property. The resulting list
contains 100 items, and a new nextItem
object is created, which will be added to this list during the benchmarking.
Html Preparation Code: The benchmark uses the Immer library, loaded from a CDN. Immer is designed to enable immutable state management in JavaScript applications by allowing users to work with mutable representations of state that automatically track changes.
Immer Approach
const updatedList = immer.produce(list, (draft) => { draft.push(nextItem); });
nextItem
is pushed to list
. Immer handles the complexity of immutability, allowing developers to write code that appears to mutate state directly while creating immutable updates under the hood.Spread Operator Approach
const updatedList = [...list, nextItem];
list
and appending nextItem
. It offers a concise way to maintain immutability, as it creates a copy of the entire array rather than modifying it in place.The benchmark results indicate a significant difference in execution speed:
14,294,164
executions per second.167,956
executions per second.Pros:
Cons:
Pros:
Cons:
Array.prototype.concat(): Similar to the spread operator, it is used to combine arrays without modifying the original array. E.g., const updatedList = list.concat(nextItem);
Immutable.js: A library for creating immutable data structures. While it provides performance benefits, it has a steeper learning curve compared to standard JavaScript syntax.
Object.assign() with Arrays: This approach can also achieve similar results but is less common and can be less readable.
Overall, the benchmark effectively highlights the trade-offs between utilizing a library like Immer for state management versus leveraging native JavaScript features like the spread operator. Choosing the right approach depends on the specific use case, performance requirements, and the complexity of the state under consideration. For high-performance applications that manage large datasets, the spread operator may often be the preferred choice. For applications that require extensive immutability with complex state changes, Immer can provide significant benefits, albeit with a performance cost.