<script crossorigin src="https://unpkg.com/immutable-assign@2.0.4/deploy/iassign.js"></script>
var state = {
hasChanged: false,
name: 'Default state',
tags: [],
nested: {
hasChanged: false,
name: 'Default nested state',
tags: [],
nested: {
hasChanged: false,
name: '',
tags: []
},
map: new Map()
},
map: new Map()
};
const s7 = iassign(state, s => {
s.hasChanged = true;
return s;
});
const s8 = iassign(s7, s => s.nested, nested => {
nested.hasChanged = true;
return nested;
});
const s9 = iassign(s8, s => s.tags, tags => {
tags.push('new tag');
return tags;
});
const s10 = iassign(state, s => {
s.hasChanged = true;
s.nested = iassign(s.nested, nested => {
nested.hasChanged = true;
return nested;
});
s.tags = iassign(s.tags, tags => {
tags.push('new tag');
return tags;
});
return s;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
sequential calls | |
nested calls |
Test name | Executions per second |
---|---|
sequential calls | 123234.5 Ops/sec |
nested calls | 364814.5 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark tests the performance difference between sequential and nested assignment in the Immutable.js library.
Immutable.js Library
Immutable.js is a library for working with immutable data structures in JavaScript. It provides a way to create immutable objects that cannot be modified directly, but instead must be recreated with each change. This can improve the performance of applications by reducing unnecessary reassignments of variables.
In this benchmark, Immutable.js is used to compare the performance of sequential assignment (i.e., assigning values one at a time) versus nested assignment (i.e., assigning multiple values within an object).
Options Compared
The benchmark compares two approaches:
Pros and Cons of Each Approach
Sequential Assignment
Pros:
Cons:
Nested Assignment
Pros:
Cons:
Other Considerations
In this benchmark, the iassign
function from Immutable.js is used to perform both sequential and nested assignment. The iassign
function takes three arguments: the initial value, a partial function that updates part of the value, and a final function that returns the updated value.
The benchmark uses two test cases:
iassign
function to update each property individually.Alternative Approaches
Other approaches could be used to compare performance, such as:
assign
from Lodash)However, the benchmark is designed to focus on the performance difference between sequential and nested assignment within the context of Immutable.js.
I hope this explanation helps! Let me know if you have any further questions.