var continuous = [];
var sparse = [];
var C = 900000;
for(var i=0; i<C; ++i){
continuous.push(C-i);
}
for(var i=0; i<C; ++i){
sparse[i * 2] = C - i;
}
var total = continuous.reduce(function(acc, cur){
return acc + cur;
});
var total = sparse.reduce(function(acc, cur){
return acc + cur;
});
continuous.forEach(function(cur, i, arr){
arr[i] += 100;
});
sparse.forEach(function(cur, i, arr){
arr[i] += 100;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
continuous reduce | |
sparse reduce | |
continuous update | |
sparse update |
Test name | Executions per second |
---|---|
continuous reduce | 114.0 Ops/sec |
sparse reduce | 95.9 Ops/sec |
continuous update | 133.8 Ops/sec |
sparse update | 111.7 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition: The benchmark compares the performance of two types of arrays in JavaScript:
undefined
values at those indices.The script preparation code creates a continuous array with 900,000 elements and a sparse array with the same number of elements, but with every other element being undefined
.
Script Preparation Code:
var continuous = [];
var sparse = [];
var C = 900000;
for (var i = 0; i < C; ++i++) {
continuous.push(C - i);
}
for (var i = 0; i < C; ++i) {
sparse[i * 2] = C - i;
}
This code initializes both arrays with the same elements, but the sparse array has every other element being undefined
.
Test Cases: The benchmark consists of four test cases:
reduce()
method on the continuous array.reduce()
method on the sparse array.forEach()
method with a callback function that updates an element in the continuous array.forEach()
method with a callback function that updates an element in the sparse array.Options Compared: The benchmark compares two options:
Pros and Cons:
reduce()
method, especially for large datasets.Library Used: The benchmark does not explicitly mention any libraries being used. However, it's likely that some underlying framework or runtime environment is providing the JavaScript engine and execution platform.
Special JS Feature/Syntax: None mentioned in this specific benchmark.
Other Considerations:
Alternative Benchmarks: If you're interested in exploring other benchmarking options, here are a few alternatives:
Keep in mind that different benchmarking libraries may have varying levels of complexity, customization options, and performance characteristics.