Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow copy222ddd
(version: 0)
Comparing performance of:
immer vs shallow copy
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/immer@6.0.5/dist/immer.umd.production.min.js"></script>
Script Preparation code:
var data = { items: {}, count: 0, keys: [] } for (let index = 0; index < 100; index++) { data.items[index] = { id: index, name: `ITEM-${index}`, value: Math.random() } data.count++ data.keys.push(index) } var NEW_ITEM_ID = data.count +1 var produce = immer.default
Tests:
immer
var x = 0; data = produce(data, draft => { draft.keys.forEach(i => x += draft.items[i].id); })
shallow copy
var x = 0; data.keys.forEach(i => x += data.items[i].id);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
immer
shallow copy
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 JSON benchmark definition and explain what's being tested. **What is being tested?** MeasureThat.net is testing two different approaches to update a JavaScript object: 1. **Shallow copy**: The first test case uses a simple loop to iterate over an array of objects (`data.items`) and updates a variable `x` with the sum of IDs of these objects. 2. **Immer**: The second test case uses the Immer library to create a new draft of the original object (`data`) and updates a variable `x` with the sum of IDs of the updated objects in the draft. **Options compared** The two options being compared are: * Shallow copy: A simple, manual iteration over the array of objects and updating the variable `x`. * Immer: Using the Immer library to create a new draft of the object and update the variable `x`. **Pros and Cons** **Shallow copy:** Pros: * Simple, easy to understand * No external dependencies required Cons: * May lead to performance issues due to the need for manual iteration over the array * Not efficient in terms of memory usage **Immer:** Pros: * Efficient in terms of performance and memory usage * Reduces boilerplate code and makes updates more concise Cons: * Requires an external library (Immer) to be included * May have overhead due to the creation of a new draft object **Other considerations** In addition to the options being compared, other factors that may affect the results include: * The specific implementation of the shallow copy approach (e.g., using `for...in` instead of `forEach`) * The version of the Immer library used * Any additional dependencies or overhead in the benchmarking environment **Library and syntax** The test case uses the Immer library, which is a popular library for managing state changes in JavaScript applications. The Immer API provides a simple way to create a new draft of an object and update it incrementally. There is no special JS feature or syntax used in this benchmark. **Alternatives** Some alternative approaches that could be considered instead of shallow copy or Immer include: * Using the `Array.prototype.forEach()` method with a callback function * Using a library like Lodash, which provides utility functions for working with arrays and objects * Using a functional programming approach, such as using `map()` and `reduce()` However, these alternatives may not provide the same level of performance or conciseness as Immer.
Related benchmarks:
Immer vs shallow copy
Immer vs shallow copy + immutable (2)
Immer vs shallow copy vs Immutable
Immer vs shallow copy222dddxxx
Comments
Confirm delete:
Do you really want to delete benchmark?