<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);
}
}
console.log('Immutable Map:: ', count);
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);
}
}
console.log('Map:: ', count);
--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 | 1130.5 Ops/sec |
Native Javascript Map | 611.1 Ops/sec |
Read immutable Map | 556.9 Ops/sec |
Read Native Javascript Map | 273.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The provided benchmark compares the performance of two data structures: Immutable Maps and Native JavaScript Maps, both used for storing key-value pairs. The focus is on three scenarios:
Options compared
The two main options being tested are:
Map
data structure in JavaScript.Pros and Cons of each approach:
Library and its purpose
The Immutable
library is a popular JavaScript library that provides immutable data structures, such as Maps, Sets, Lists, and more. Its primary goal is to ensure data integrity and thread-safety by using immutable data structures.
In the context of this benchmark, Immutable Maps are used to create a read-only Map, which helps prevent unintended modifications during the execution of the test case.
Special JS feature or syntax
This benchmark does not specifically utilize any special JavaScript features or syntax, such as async/await, Promises, or WebAssembly. The focus is on comparing the performance of Immutable Maps and Native JavaScript Maps.
Other alternatives
If you're interested in exploring alternative data structures for your applications, here are a few options:
Map
data structure that uses weak references for its keys.Keep in mind that each alternative has its own trade-offs and use cases.