var array = new Array(100000).fill(0);
for (let i=0; i<array.length; i++) {
array[i] = array[i] + 1;
}
let sum = 0;
for (let i=0; i<array.length; i++) {
sum+= array[i]
}
var typedArray = new Uint8Array(100000).fill(0);
for (let i=0; i<typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
let sum = 0;
for (let i=0; i<typedArray.length; i++) {
sum += typedArray[i]
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array write | |
TypedArray write |
Test name | Executions per second |
---|---|
Array write | 3079.1 Ops/sec |
TypedArray write | 7915.9 Ops/sec |
The benchmark you've provided focuses on comparing the performance of standard JavaScript arrays against typed arrays, specifically Uint8Array
, in terms of write operations. It consists of two test cases: one that uses a typical JavaScript array and another that employs a typed array. Here’s a detailed analysis of the different approaches, their pros and cons, and other related considerations.
Array Write Performance:
var array = new Array(100000).fill(0);
for (let i = 0; i < array.length; i++) {
array[i] = array[i] + 1;
}
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
Typed Array Write Performance:
var typedArray = new Uint8Array(100000).fill(0);
for (let i = 0; i < typedArray.length; i++) {
typedArray[i] = typedArray[i] + 1;
}
let sum = 0;
for (let i = 0; i < typedArray.length; i++) {
sum += typedArray[i];
}
Uint8Array
, which is a type of typed array designed for handling binary data in an array-like structure with specific numeric types.Standard Array:
map
, filter
, etc.).Typed Array (Uint8Array
):
Based on the latest execution results from the benchmark:
Typed Array:
TypedArray
are significantly faster than those using the standard array, suggesting an advantage in performance for operations involving large datasets of number types.Standard Array:
Use-Cases:
Alternatives:
ArrayBuffer
: A low-level representation of binary data that can be used alongside typed arrays for more complex data manipulations.Uint8Array
, there are various typed arrays (e.g., Int16Array
, Float32Array
, etc.) that cater to different numerical needs, each optimized for different ranges and types of numeric data.Context in Performance: The results may vary based on the engine optimization, hardware, and other factors in the runtime environment. It’s important to profile in the specific context of usage to make informed decisions based on performance needs.
In conclusion, the benchmark illustrates that while standard JavaScript arrays are versatile, typed arrays like Uint8Array
provide a significant performance boost for numerical operations, making them a better choice for specific scenarios.