<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, i];
const value = 'value'+i;
obj.set(key, {key, value});
}
let obj = new Map();
for(i=0;i<10000;i++){
const key = ['key'+i, i];
const value = 'value'+i;
obj.set(key, {key, value});
}
let obj = Immutable.Map();
for(i=0;i<10000;i++){
const key = ['key'+i, i];
const value = 'value'+i;
obj.set(key, {key, value});
}
let count = 0;
for(i=0;i<10000;i++){
const key = ['key'+i, i];
if (obj.has(key)) {
count++;
const temp = obj.get(key);
}
}
let obj = new Map();
for(i=0;i<10000;i++){
const key = ['key'+i, i];
const value = 'value'+i;
obj.set(key, {key, value})
}
let count = 0;
for(i=0;i<10000;i++){
const key = ['key'+i, i];
if (obj.has(key)) {
count++;
const temp = obj.get(key);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immutable Map | |
Native Javascript Map | |
Read immutable Map | |
Read Native Javascript Map |
Test name | Executions per second |
---|---|
immutable Map | 1410.4 Ops/sec |
Native Javascript Map | 775.3 Ops/sec |
Read immutable Map | 1112.1 Ops/sec |
Read Native Javascript Map | 442.5 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Definition
The benchmark measures the performance of two approaches:
Map
object, which is mutable.Options Compared
The benchmark compares the following options:
Pros and Cons of Each Approach
Library Used
The benchmark uses two libraries:
Special JS Features or Syntax
None are mentioned explicitly in the provided benchmark definition.
Alternative Approaches
Other alternatives to consider when working with maps:
Map
instances or create your own custom data structure.Keep in mind that these alternatives may have different trade-offs in terms of performance, memory usage, and code complexity. The choice ultimately depends on the specific requirements of your project.
In general, if you need to perform frequent reads or updates on a large dataset, using an immutable map or a native JavaScript map might be a good approach. However, if you prioritize data consistency and thread-safety, using Immutable.js might be a better fit.