var arr32 = new Uint32Array(16384)
crypto.getRandomValues(arr32)
var array = Array.from(arr32)
array.reverse()
arr32.reverse()
array.toSorted()
arr32.toSorted()
array.sort();
array.reverse(); // fast "un-sort"
arr32.sort();
arr32.reverse(); // fast "un-sort"
for (let i = 0; i < 16384; i++)
array[i]++
for (let i = 0; i < 16384; i++)
arr32[i]++
for(let i = 0; i < 16384; i++)
array[i * 11003 % 16384] = array[i * 12011 % 16384] + array[i * 13001 % 16384]
for(let i = 0; i < 16384; i++)
arr32[i * 11003 % 16384] = arr32[i * 12011 % 16384] + arr32[i * 13001 % 16384]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.reverse() | |
TypedArray.reverse() | |
Array.toSorted() | |
TypedArray.toSorted() | |
Array.sort() | |
TypedArray.sort() | |
Array[i] (sequential) | |
TypedArray[i] (sequential) | |
Array[i] (random) | |
TypedArray[i] (random) |
Test name | Executions per second |
---|---|
Array.reverse() | 123147.8 Ops/sec |
TypedArray.reverse() | 171878.8 Ops/sec |
Array.toSorted() | 330.0 Ops/sec |
TypedArray.toSorted() | 1561.7 Ops/sec |
Array.sort() | 646.8 Ops/sec |
TypedArray.sort() | 44933.4 Ops/sec |
Array[i] (sequential) | 80557.3 Ops/sec |
TypedArray[i] (sequential) | 53184.0 Ops/sec |
Array[i] (random) | 10319.5 Ops/sec |
TypedArray[i] (random) | 15914.1 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Definition
The benchmark measures the performance of three types of array operations:
array.reverse()
arr32.reverse()
(using TypedArray)array.sort()
followed by array.reverse()
(a sequence of operations)Additionally, two other tests are included:
array[i]
)arr32[i]
)array[i]
with random indices)arr32[i]
with random indices)Options Compared
The benchmark compares the performance of different approaches:
array
TypedArray
(specifically, Uint32Array
)Pros and Cons of Different Approaches
array
:Uint32Array
):Results
The benchmark results show varying performance across different tests. The TypedArray
approach generally performs better, especially for sequential access and random access. However, the sequence of operations test shows mixed results, with some tests benefiting from this approach while others are slower.
Notable Insights
Overall, this benchmark provides valuable insights into the performance characteristics of different approaches when working with arrays in JavaScript.