var arrDim = 1000;
var clArr = new Array(arrDim);
for (var i = 0; i < arrDim; i++) {
clArr[i] = new Uint8Array(arrDim);
}
clArr.getSlice = function(i) {
return this[i];
}
clArr.setVal = function(i, j, val) {
this[i][j] = val;
}
clArr.getVal = function(i, j) {
return this[i][j];
}
var flArr = new Uint8Array(Math.pow(arrDim, 2));
flArr.getSlice = function(i) {
var start = i*arrDim;
return this.slice(start, start + arrDim);
}
flArr.setVal = function(i, j, val) {
this[i*arrDim + j] = val;
}
flArr.getVal = function(i, j) {
return this[i*arrDim + j];
}
flArr.setVal(123, 123, 1);
clArr.setVal(123, 123, 1);
flArr.getVal(123, 123);
clArr.getVal(123, 123);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
flat array set | |
2d array set | |
flat array get | |
2d array get |
Test name | Executions per second |
---|---|
flat array set | 260144480.0 Ops/sec |
2d array set | 239926224.0 Ops/sec |
flat array get | 274478304.0 Ops/sec |
2d array get | 241388304.0 Ops/sec |
The benchmark titled "flat typed array vs 2d typed array" compares the performance of two different approaches to storing and accessing 2D array-like structures in JavaScript: a flat typed array (flArr
) and a traditional 2-dimensional array constructed using arrays of Uint8Array
(clArr
).
Flat Typed Array (flArr
):
Uint8Array
to represent a 2D array. Accessing elements involves computing a single index based on the current row and column.setVal(i, j, val)
: Sets the value at row i
, column j
by calculating the corresponding index.getVal(i, j)
: Retrieves the value from the calculated index.2D Typed Array (clArr
):
Uint8Array
, effectively simulating a 2D array. setVal(i, j, val)
: Uses the array reference for the ith element to set the jth value.getVal(i, j)
: Retrieves the jth value from the ith Uint8Array
.flArr
):Pros:
Cons:
clArr
):Pros:
Cons:
Uint8Array
instances).The test cases benchmark the effectiveness of element retrieval and modification for both structures:
flArr.setVal(123, 123, 1)
: Measures the performance of setting a value in the flat array.clArr.setVal(123, 123, 1)
: Measures the performance of setting a value in the 2D array.flArr.getVal(123, 123)
: Measures the performance of retrieving a value from the flat array.clArr.getVal(123, 123)
: Measures the performance of retrieving a value from the 2D array.The benchmark results indicate that the flat array generally performs better than the 2D array, both for setting and getting values. For example:
flArr.getVal
at approximately 274 million executions per second.clArr
) trails behind, but is slower by relatively small margins.flArr
and clArr
use Uint8Array
, a part of JavaScript's Typed Arrays feature designed for efficient binary data manipulation. Typed arrays enable direct manipulation of binary data in memory, offering performance improvements for numeric computations.Other alternatives for implementing and optimizing 2D array structures include:
Float32Array
for different numeric ranges.In conclusion, the choice between flat typed arrays and 2-dimensional arrays should be guided by specific use cases and performance requirements, weighing the trade-offs between simplicity and efficiency.