<script src="https://unpkg.com/immer@9/dist/immer.umd.production.min.js"></script>
var INITIAL_DATA = { items: {}, count: 0, keys: [] }
for (var index = 0; index < 100; index++) {
INITIAL_DATA[index] = { id: index, name: `ITEM-${index}`, value: Math.random() }
INITIAL_DATA.count++
INITIAL_DATA.keys.push(index)
}
var NEW_ITEM_ID = INITIAL_DATA.count +1
var im = immer;
im.setAutoFreeze(false)
var produce = im.default
data = produce(INITIAL_DATA, draft => {
draft.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 }
draft.counter++
draft.keys.push(NEW_ITEM_ID)
})
im.setAutoFreeze(true)
var produce = im.default
data = produce(INITIAL_DATA, draft => {
draft.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 }
draft.counter++
draft.keys.push(NEW_ITEM_ID)
})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immer no freeze | |
immer freeze |
Test name | Executions per second |
---|---|
immer no freeze | 920.9 Ops/sec |
immer freeze | 2181.7 Ops/sec |
Let's break down the benchmark definition and test cases to understand what is being tested.
Benchmark Definition
The benchmark definition is represented by a JSON object that describes two test cases:
INITIAL_DATA
with 100 properties, each containing an ID, name, and value. It also defines a new item ID NEW_ITEM_ID
.im.setAutoFreeze(false)
and then creates a draft of the initial data using the immer.default
function. The draft is modified to add a new item with ID NEW_ITEM_ID
.im.setAutoFreeze(true)
, which enables automatic freezing of the draft.What is being tested?
The benchmark is testing the performance difference between two Immer configurations:
im.setAutoFreeze(false)
, the draft must be manually frozen before using it.im.setAutoFreeze(true)
, the draft is automatically frozen, which may reduce memory allocation and garbage collection overhead.Pros and Cons of each approach
Library: Immer
Immer is a JavaScript library that provides a simple and efficient way to work with immutable data structures. It's designed to simplify state management in functional programming languages. In this benchmark, Immer is used to create drafts of the initial data, which are then modified and compared for performance differences.
Special JS feature or syntax
There isn't any specific JavaScript feature or syntax mentioned in the benchmark definition that requires special attention. However, it's worth noting that the use of immer.default
and im.setAutoFreeze()
suggests that the author is familiar with the Immer library and its features.
Other alternatives
If you're looking for alternative libraries to compare with Immer, some options might include:
Keep in mind that the choice of alternative library will depend on your specific use case and requirements.