var array = new Array(100000).fill(0).map(_x => 0);
var typedArray = new Uint32Array(100000).fill(0);
const len = array.length;
for (let i = 0; i < len; ++i) {
array[i] = array[i] + 1;
}
const len2 = typedArray.length;
for (let i = 0; i < len2; ++i) {
typedArray[i] = typedArray[i] + 1;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array write | |
TypedArray write |
Test name | Executions per second |
---|---|
Array write | 11182.0 Ops/sec |
TypedArray write | 22334.2 Ops/sec |
The provided benchmark tests the performance of two approaches to writing data to a collection in JavaScript: using a regular JavaScript array and using a typed array (specifically, a Uint32Array
).
JavaScript Array (array
):
const len = array.length;
for (let i = 0; i < len; ++i) {
array[i] = array[i] + 1;
}
Typed Array (typedArray
):
Uint32Array
), which is designed for handling binary data in a more efficient way.const len2 = typedArray.length;
for (let i = 0; i < len2; ++i) {
typedArray[i] = typedArray[i] + 1;
}
TypedArray
write operation executes approximately 22,334 times per second, while the standard array write operation executes about 11,182 times per second. This indicates that writing to a typed array is significantly faster in this scenario.JavaScript Array:
Typed Array (Uint32Array
):
Uint32Array
, JavaScript provides other typed arrays like Int32Array
, Float32Array
, and Float64Array
, which can be used depending on the required precision and data type.Overall, this benchmark clearly illustrates the performance advantages of typed arrays for numeric data manipulation, encouraging developers working with JavaScript to consider their data storage choices based on their specific performance and flexibility needs.