var r32arr = new Uint8Array(32).map(() => Math.floor(Math.random() * 256));
var mil32arr = Array.from({ length: 10_000_000 }, () => new Uint8Array(32).map(() => Math.floor(Math.random() * 256)));
var r96arr = new Uint8Array(96).map(() => Math.floor(Math.random() * 256));
var mil96arr = Array.from({ length: 10_000_000 }, () => new Uint8Array(96).map(() => Math.floor(Math.random() * 256)));
var r160arr = new Uint8Array(160).map(() => Math.floor(Math.random() * 256));
var mil160arr = Array.from({ length: 10_000_000 }, () => new Uint8Array(160).map(() => Math.floor(Math.random() * 256)));
const b = mil32arr.some(arr => arr.every((value, index) => value === r32arr[index]));
const b = mil96arr.some(arr => arr.every((value, index) => value === r96arr[index]));
const b = mil160arr.some(arr => arr.every((value, index) => value === r160arr[index]));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 or 0 out of 10 million 32 byte arrays | |
1 or 0 out of 10 million 96 byte arrays | |
1 or 0 out of 10 million 160byte arrays |
Test name | Executions per second |
---|---|
1 or 0 out of 10 million 32 byte arrays | 3.4 Ops/sec |
1 or 0 out of 10 million 96 byte arrays | 1.3 Ops/sec |
1 or 0 out of 10 million 160byte arrays | 0.8 Ops/sec |
Benchmark Explanation
The provided benchmark tests the performance difference between using bitwise comparisons and string comparisons to find a single element in large arrays.
Test Case Overview
There are three test cases:
r32arr
and mil32arr
)r96arr
and mil96arr
)r160arr
and mil160arr
)Each test case uses the some()
method, which returns a boolean value indicating whether at least one element in the array passes a given callback function.
Benchmarking Options
The benchmark compares the performance of two approaches:
some()
method checks for exact equality between each element of the large array and the corresponding element of the smaller array (r32arr
, r96arr
, or r160arr
) using bitwise operations (e.g., value === r32arr[index]
).some()
method checks for exact equality between each character of the large array and the corresponding character of the smaller array (r32arr
, r96arr
, or r160arr
) using string operations (e.g., value === r32arr[index]
).Pros and Cons
Library Used
In this benchmark, there is no specific library mentioned. However, the use of Array.from()
and Uint8Array
suggests that the benchmark is designed for JavaScript environments that support these APIs.
Special JS Features or Syntax
There are no special JavaScript features or syntax mentioned in the benchmark code. The focus is on comparing bitwise and string comparisons, which are fundamental operations in JavaScript.
Other Alternatives
If you need to compare elements in large arrays, other alternatives to consider:
indexOf()
, findIndex()
) might offer better performance than custom implementations like some()
.Keep in mind that the best approach depends on the specific requirements of your project, including character sets, data formats, and performance constraints.