var size = 1024;
var initialArray = [];
for(let i = 0; i < size; i++) {
initialArray.push(Math.random());
}
const arr = new Float64Array(size);
for(let i = 0; i < size; i++) {
arr[i] = initialArray[i]/2;
}
const arr = [];
for(let i = 0; i < size; i++) {
arr.push(initialArray[i]/2);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
typed array | |
not typed array |
Test name | Executions per second |
---|---|
typed array | 743068.9 Ops/sec |
not typed array | 252112.5 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Purpose: The goal of this benchmark is to compare the performance of two approaches when modifying an array in JavaScript:
Float64Array
(a typed array) instead of a regular array
.array
using the push()
method.Options Compared:
Float64Array
): This option uses the Float64Array
constructor to create an array with a specific data type (64-bit floating-point numbers). This can provide better performance and more precise calculations compared to using a regular array
.array
with push()
method: This option uses the push()
method to add elements to a regular array
. While this is a common way to append elements, it may be slower than using a typed array or other optimized data structures.Pros and Cons:
Float64Array
):array
with push()
method:push()
method.Library Used: None, as this benchmark only uses built-in JavaScript features and data structures (arrays).
Special JS Feature/Syntax: The benchmark does not utilize any special JavaScript features or syntax beyond what is commonly used in modern web development. No ES6+ features, no WebAssembly, etc.
Other Alternatives:
Int32Array
, Uint8Array
, or BigInt64Array
might also be compared to see if they offer better performance.The current benchmark focuses on comparing the performance of using a typed array (Float64Array
) versus a regular array
with the push()
method. If you'd like to explore other approaches or optimizations, feel free to modify the benchmark script and see how it affects the results!