var iterations =100000;
var step = 100;
var obj = {};
var arr = [];
var map = new Map();
var set = new Set();
var setLengthArr = new Array(iterations);
var typedArray = new Uint32Array(iterations);
for(let i=0;i<iterations;i+=step){
obj[i]= i;
obj[i+1]=obj[i]+1;
}
for(let i=0;i<iterations;i+=step){
arr[i]=i;
arr[i+1]= arr[i]+1;
}
for(let i=0;i<iterations;i+=step){
setLengthArr[i]=i;
setLengthArr[i+1] = setLengthArr[i]+1;
}
for(let i=0;i<iterations;i+=step){
typedArray[i]=i;
typedArray[i+1]= typedArray[i]+1;
}
for(let i=0;i<iterations;i+=step){
map.set(i,i);
map.set(i+1,map.get(i)+1)
}
for(let i=0;i<iterations;i+=step){
set.add(i);
set.add(set.has(i) && i)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object | |
Array | |
Set Length Array | |
TypedArray | |
map | |
set |
Test name | Executions per second |
---|---|
Object | 2669.0 Ops/sec |
Array | 2659.1 Ops/sec |
Set Length Array | 3956.0 Ops/sec |
TypedArray | 3803.2 Ops/sec |
map | 3364.9 Ops/sec |
set | 3646.3 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Overview
The benchmark compares the performance of different data structures in JavaScript:
Each test case has a simple loop that increments values, demonstrating basic arithmetic operations.
Data Structures Compared
Here's a brief explanation of each data structure and their purpose:
Performance Comparison
The benchmark measures the performance of each data structure by executing a series of loops, incrementing values, and counting the number of executions per second. The results are stored in the ExecutionsPerSecond
field for each test case.
Pros/Cons of Each Approach
Here's a brief analysis of the pros and cons of each data structure:
Other Considerations
When choosing a data structure, consider the following factors:
Keep in mind that this benchmark only compares simple arithmetic operations and may not reflect real-world scenarios where data structures are used for more complex tasks.
Alternatives
If you're looking for alternative data structures or libraries, consider the following:
These alternatives may offer improved performance or additional features depending on your specific use case.