var iterations =10000;
var step = 10;
const obj = {};
for(let i=0;i<iterations;i+=step){
obj[i]= i;
obj[i+1]=obj[i]+1;
}
const arr = [];
for(let i=0;i<iterations;i+=step){
arr[i]=i;
arr[i+1]= arr[i]+1;
}
const setLengthArr = new Array(iterations);
for(let i=0;i<iterations;i+=step){
setLengthArr[i]=i;
setLengthArr[i+1] = setLengthArr[i]+1;
}
const typedArray = new Uint32Array(iterations);
for(let i=0;i<iterations;i+=step){
typedArray[i]=i;
typedArray[i+1]= typedArray[i]+1;
}
const map = new Map();
for(let i=0;i<iterations;i+=step){
map.set(i,i);
map.set(i+1,map.get(i)+1)
}
const set = new Set();
for(let i=0;i<iterations;i+=step){
set.add(i);
set.add(set.has(i) && i)
}
const obj = {};
for(let i=0;i<iterations;i+=step){
const val = 'a' + i;
obj[val]= val;
obj[i+1]=obj[val];
}
const arr = [];
for(let i=0;i<iterations;i+=step){
const val = 'a' + i;
arr[i]= val;
arr[i+1]=arr[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object | |
Array | |
Set Length Array | |
TypedArray | |
map | |
set | |
Object Alpha index | |
Array numeric index |
Test name | Executions per second |
---|---|
Object | 7140.0 Ops/sec |
Array | 8248.5 Ops/sec |
Set Length Array | 9440.4 Ops/sec |
TypedArray | 8513.7 Ops/sec |
map | 5903.5 Ops/sec |
set | 7096.3 Ops/sec |
Object Alpha index | 3544.3 Ops/sec |
Array numeric index | 7514.6 Ops/sec |
Let's dive into the world of JavaScript benchmarks!
Benchmark Overview
The provided benchmark tests various data structures in JavaScript: objects, arrays, set length arrays, typed arrays, maps, sets, and even object alpha indexing. The test cases aim to measure the performance of each data structure by iterating over a large number of elements (10000) with a step size of 10.
Options Compared
The benchmark compares several options:
Pros and Cons
Here's a brief summary of each data structure's strengths and weaknesses:
Performance Comparison
The benchmark results show that:
Object Alpha Indexing
The Object Alpha Indexing test case uses an object with a large number of keys to measure its performance. The results show that this approach is still relatively slow compared to other data structures.
In summary, the benchmark provides valuable insights into the relative performance of different JavaScript data structures. When choosing a data structure for your use case, consider factors such as efficiency, flexibility, and memory management requirements.