var LIMIT = 10000;
var ARRAY = [];
var OBJECT = {};
var SPARSE_ARRAY = [];
var SPARSE_OBJECT = {};
for (var i = 0; i < LIMIT; i++) {
ARRAY[i] = i;
OBJECT[i] = i;
if (i % 2 === 0) {
SPARSE_ARRAY[i] = i;
SPARSE_OBJECT[i] = i;
}
}
var total = 0;
var target = ARRAY;
for (var i = 0; i < LIMIT; i++) {
total += target[i];
}
return total;
var total = 0;
var target = OBJECT;
for (var i = 0; i < LIMIT; i++) {
total += target[i];
}
return total;
var total = 0;
var target = SPARSE_ARRAY;
for (var i = 0; i < LIMIT; i++) {
if (i in target) {
total += target[i];
}
}
return total;
var total = 0;
var target = SPARSE_OBJECT;
for (var i = 0; i < LIMIT; i++) {
if (i in target) {
total += target[i];
}
}
return total;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
Object | |
Sparse Array | |
Sparse Object |
Test name | Executions per second |
---|---|
Array | 1200.4 Ops/sec |
Object | 1192.4 Ops/sec |
Sparse Array | 1096.4 Ops/sec |
Sparse Object | 1120.1 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test case, specifically testing the performance of accessing properties using different data structures: arrays and objects (including sparse arrays and sparse objects).
Data Structures Compared
Three types of data structures are compared:
Pros and Cons
Here's a brief overview of each approach:
array[i]
).object[key]
can be slower than array indexing.if (i in target)
).Library and Special JS Features
None of the provided benchmark test cases use external libraries. However, the in
operator used in sparse array and object access checks is a special JavaScript feature that allows checking if a property exists on an object.
Other Alternatives
Alternative approaches to testing array and object access performance could include:
Keep in mind that the specific test case design and variations may be tailored to focus on certain aspects of array and object access performance.