<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/immutability-helper@2.7.0/index.min.js"></script>
let obj = Immutable.Map();
for(i=0;i<10000;i++){
const key = 'key'+i
const value = 'value'+i
obj.set(key, {key, value})
}
let obj = new Map();
for(i=0;i<10000;i++){
const key = 'key'+i
const value = 'value'+i
obj.set(key, {key, value})
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immutable Map | |
Native Javascript Map |
Test name | Executions per second |
---|---|
immutable Map | 1067.2 Ops/sec |
Native Javascript Map | 572.8 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark compares two approaches to creating an immutable data structure in JavaScript: Immutable.js maps and native JavaScript Maps.
Immutable vs Native Javascript Map
Immutable.Map()
and then add 10,000 key-value pairs to it using the set()
method. We repeat this process multiple times.new Map()
and then add 10,000 key-value pairs to it using the set()
method. We repeat this process multiple times.Options Compared
The two options being compared are:
Library Used
The benchmark uses two libraries:
Special JS Feature or Syntax
The benchmark uses the set()
method to add key-value pairs to both types of maps. The set()
method is a standard part of JavaScript, and its usage is not specific to any particular feature or syntax.
Other Considerations
When choosing between an immutable map and a native map, consider the following factors:
Alternatives
Other alternatives to Immutable.js and native JavaScript Maps include:
These alternatives may offer different trade-offs in terms of performance, functionality, and complexity, so be sure to evaluate them carefully before making a decision.