var state = {}
var cols = [{id: 1, key: 'value'}, {id: 2, key: 'value'}]
for (var i = 0; i < 2000; i++) {
state[i] = {
id: i,
key: 'value'
};
};
const colDefs = cols.map(c => {return {[c.id]: c}});
const test = Object.assign({}, state, colDefs);
const colDefs = {}
cols.forEach(c => {
colDefs[c.id] = c;
});
const test = Object.assign({}, state, colDefs);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Mutate |
Test name | Executions per second |
---|---|
Spread | 3121.3 Ops/sec |
Mutate | 3181.2 Ops/sec |
Let's break down the benchmark and explain what is being tested, along with the pros and cons of each approach.
Benchmark Definition
The benchmark measures the performance difference between two approaches when assigning new values to an object using Object.assign()
. The test case uses Redux state, but this is not relevant to the core algorithm being compared.
Approaches Compared:
...
) to create a new array of objects and then assigns it to the original object using Object.assign()
.Pros and Cons:
Spread Approach:
Pros:
Cons:
Mutate Approach:
Pros:
Cons:
Library:
The benchmark uses Object.assign()
, which is a built-in JavaScript method that creates a shallow copy of an object.
Special JS Feature/Syntax:
There are no special features or syntax used in this benchmark. The focus is on comparing two different approaches to assigning values to an object.
Other Alternatives:
If you need to assign new values to an object, other alternatives include:
assignIn()
methodKeep in mind that the choice of approach depends on the specific use case and requirements. The spread approach is generally recommended for its readability and maintainability, while the mutate approach may be preferred for performance-critical applications.