var array = new Array(100000).fill(0);
for (let i=0; i<array.length; i++) {
array[i] = array[i] + 1;
}
var typedArray = new Uint8Array(100000).fill(0);
for (let i=0; i<typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
var typedArray = new Float32Array(100000).fill(0);
for (let i=0; i<typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
var typedArray = new Uint32Array(100000).fill(0);
for (let i=0; i<typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array write | |
TypedArray write | |
f32 | |
i32 |
Test name | Executions per second |
---|---|
Array write | 2614.1 Ops/sec |
TypedArray write | 8280.5 Ops/sec |
f32 | 4532.1 Ops/sec |
i32 | 4302.5 Ops/sec |
The benchmark titled "Array vs TypedArray vs f32 write performance" compares the performance of different JavaScript array types during a simple write operation. The individual tests included in the benchmark assess the speed of writing values to different array implementations: a standard JavaScript Array
, a TypedArray
of type Uint8Array
, a TypedArray
of type Float32Array
, and another TypedArray
of type Uint32Array
.
Array Write:
Array
is created with 100,000 elements, all initialized to 0
. The benchmark iterates through the elements and increments each value by 1
.TypedArray Write (Uint8Array):
Uint8Array
(an 8-bit unsigned integer array) is similarly created with 100,000 elements initialized to 0
, and each element is incremented by 1
.TypedArray Write (Float32Array):
Float32Array
(32-bit floating-point number array) is also created and operated on in the same manner.TypedArray Write (Uint32Array):
Uint32Array
(32-bit unsigned integer array) is tested using the same increment operation.The most recent benchmark results show the number of executions per second for each test case:
From the results, it's clear that:
Array
. This speed advantage stems from their more efficient memory handling and fixed data type, which allows JavaScript engines to optimize performance better than with flexible arrays.TypedArrays
, Uint8Array
showed the best performance, likely due to its simple nature in terms of data structure, followed by Float32Array
and Uint32Array
.Alternatives to using these different data structures depend on the specific application requirements.
Set
structures may be considered where specific operations (duplicates management, dynamic size, etc.) are required, though these may not be well-suited for raw performance tasks.Int16Array
, Float64Array
, etc., provide different ranges and precisions suited for particular applications but would follow similar performance patterns based on their fixed size and data types.In summary, this benchmark illustrates the performance advantages of using TypedArrays
over standard JavaScript arrays when high-speed numerical operations are required. The trade-offs between flexibility and performance should be considered based on the specific needs of the application, with TypedArrays
being the preferred choice for performance-critical tasks that involve large sets of numerical data.