var arr1 = new Uint8Array(2500);
var arr2 = new Uint8ClampedArray(2500);
for(let i = 0; i < 2500; i += 4) {
let next = (i + 4) % 2500;
arr1[i] = arr1[next];
arr1[i+1] = arr1[next+1];
arr1[i+2] = arr1[next+2];
arr1[i+3] = arr1[next+3];
}
arr1[37] = arr1[732]
for(let i = 0; i < 2500; i += 4) {
let next = (i + 4) % 2500;
arr2[i] = arr2[next];
arr2[i+1] = arr2[next+1];
arr2[i+2] = arr2[next+2];
arr2[i+3] = arr2[next+3];
}
arr2[37] = arr2[732]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Uint8Array | |
Uint8ClampedArray |
Test name | Executions per second |
---|---|
Uint8Array | 6825.6 Ops/sec |
Uint8ClampedArray | 6860.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided JSON represents a benchmark test comparing two types of arrays in JavaScript: Uint8Array
and Uint8ClampedArray
. The test measures how fast it is to copy values between these two array types using a specific pattern.
Options Compared
Two options are compared:
Uint8Array
: An array that stores unsigned 8-bit integers, with no clipping or bounds checking.Uint8ClampedArray
: An array that stores unsigned 8-bit integers, but with clipping and bounds checking.Pros and Cons of Each Approach
Uint8Array
:Uint8ClampedArray
:Library Used
The arr1
and arr2
arrays are instances of the Uint8Array
and Uint8ClampedArray
classes, respectively. These classes are part of the JavaScript standard library, providing a way to create typed arrays that can be used in various applications.
JavaScript Feature/Syntax Used
The benchmark uses modern JavaScript features:
for...of
loop headers and string interpolation use template literals (\r\n
) for formatting code.(i, next) => { ... }
).Other Considerations
Alternative Benchmarks
If you're interested in exploring other JavaScript microbenchmark scenarios:
push()
, pop()
, and splice()
.Keep in mind that each benchmark is designed to test a specific aspect of JavaScript performance.