var data = new Array(100000).fill(undefined).map((_,i) => i)
var obj = data.reduce((obj, idx) => {obj['idx' + idx] = idx; return obj}, {});
var map = new Map(Object.entries(obj));
let a = {obj};
let a = new Map(map);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Obj spread | |
new Map |
Test name | Executions per second |
---|---|
Obj spread | 16.4 Ops/sec |
new Map | 74.1 Ops/sec |
Benchmark Overview
The provided benchmark is designed to compare the performance of two approaches in JavaScript: object spreading (obj spread
) and creating a new Map from an existing object.
Object Spreading (obj spread
)
In this approach, the let a = {...obj};
expression creates a shallow copy of the obj
object by iterating over its entries and copying each property to a new object. This process involves:
{}
.obj
.idx
) to the corresponding key ('idx' + idx
) on the new object.Pros:
Cons:
New Map (new Map
)
In this approach, a new Map is created from an existing object using Object.entries(obj)
. This process involves:
Map()
constructor.Pros:
Cons:
Map()
constructor.Other Considerations
Object.assign()
instead of object spreading might be more suitable.Benchmark Preparation Code
The script preparation code is as follows:
var data = new Array(100000).fill(undefined).map((_, i) => i)
var obj = data.reduce((obj, idx) => { obj['idx' + idx] = idx; return obj }, {})
var map = new Map(Object.entries(obj))
This code creates an array data
with 100,000 elements, generates a reduced object obj
using the provided script preparation code, and then creates a new Map map
from obj
.
Individual Test Cases
The benchmark consists of two test cases:
let a = {...obj};
.let a = new Map(map);
.These test cases are likely used to measure the performance differences between object spreading and creating a new Map for large datasets.
Latest Benchmark Result
The latest benchmark result shows that the "new Map" approach outperforms the "obj spread" approach. The results indicate that:
This suggests that creating a new Map from an existing object is more efficient than using object spreading for large datasets in this specific benchmark scenario.