var f32arr = new Float32Array(1000000);
f32arr.forEach(function (a) {
a = 123456;
});
var doublearr = [];
for (var i = 0; i < 1000; i++) {
doublearr[i] = [];
for (var j = 0; j < 1000; j++) {
doublearr[i].push(123456);
}
}
var doublearrwithobject = [];
for (var i = 0; i < 1000; i++) {
doublearrwithobject[i] = [];
for (var j = 0; j < 1000; j++) {
doublearrwithobject[i].push({ value: 123456 });
}
}
function lookupFloat32 () {
var x = f32arr[722500];
}
function lookupDoubleArr () {
var y = doublearr[850][850];
}
function lookupDoubleArrWithObject () {
var z = doublearrwithobject[850][850].value;
}
lookupFloat32();
lookupDoubleArr();
lookupDoubleArrWithObject();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lookupFloat32 | |
lookupDoubleArr | |
lookupDoubleArrWithObject |
Test name | Executions per second |
---|---|
lookupFloat32 | 5461284.0 Ops/sec |
lookupDoubleArr | 5597454.5 Ops/sec |
lookupDoubleArrWithObject | 5463460.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a platform for testing JavaScript performance, and the provided benchmark definition represents three microbenchmarks: lookupFloat32
, lookupDoubleArr
, and lookupDoubleArrWithObject
. These tests aim to measure the performance of JavaScript engines when accessing array elements with different data types (float 32, double array, and object).
Benchmark Definition Explanation
The benchmark definition consists of a script preparation code, which initializes arrays and populates them with data. The main test functions (lookupFloat32
, lookupDoubleArr
, and lookupDoubleArrWithObject
) access the elements of these arrays using different methods.
f32arr
: A float 32 array with 1 million elements, where each element is assigned a value.doublearr
: A double array with 1000 inner arrays, each containing 1000 elements, all initialized to the same value (123456).doublearrwithobject
: A double array with 1000 inner arrays, each containing an object with a single property value
, also initialized to the same value (123456).The test functions simply access the elements of these arrays using their indices.
Options Compared
The benchmark compares three options:
f32arr[722500]
): Directly accessing the element at index 722500 in the f32arr
array.doublearr[850][850]
): Accessing the inner array at index 850 of the doublearr
array, and then accessing the first element of that inner array (at index 0).doublearrwithobject[850][850].value
): Accessing the property value
of the object stored in the inner array at index 850 of the doublearrwithobject
array.Pros and Cons
Library and Special JS Features
None of the benchmark tests use any external libraries. However, it's worth noting that some modern JavaScript engines (e.g., V8) have introduced new features like let
and const
declarations, which can affect performance in certain cases.
Other Considerations
When dealing with large arrays or complex data structures, other factors to consider include:
Alternatives
Other alternatives for testing array access performance might include: