class CustomArray extends Array {
constructor(args) {
super(args);
this.position = 0;
}
}
class CustomUint8Array extends Uint8Array {
constructor(args) {
super(args);
this.position = 0;
}
}
const LENGTH = 12000;
var rArray = [];
var uint8Array = new Uint8Array(LENGTH);
var uint8ArrayExp = new Uint8Array(LENGTH);
uint8ArrayExp.position = 0;
var cArray = new CustomArray(LENGTH);
var cUint8Array = new CustomUint8Array(LENGTH);
for (let i = 0; i < LENGTH; i++) {
const v = ~~(Math.random() * 255);
rArray.push(v);
uint8Array[i] = v;
uint8ArrayExp[i] = v;
cArray[i] = v;
cUint8Array[i] = v;
}
function read(arr, offset) {
let pos = offset;
while (pos < arr.length) {
pos++;
if (arr[pos] < 127) {
return pos;
}
}
return pos;
}
let o = 0;
while (o < rArray.length) {
o = read(rArray, o);
}
function read(arr, offset) {
let pos = offset;
while (pos < arr.length) {
pos++;
if (arr[pos] < 127) {
return pos;
}
}
return pos;
}
let o = 0;
while (o < uint8Array.length) {
o = read(uint8Array, o);
}
function read(arr) {
while (arr.position < arr.length) {
arr.position++;
if (arr[arr.position] < 127) {
return arr.position;
}
}
return arr.position;
}
let o = 0;
uint8ArrayExp.position = 0;
while (o < uint8ArrayExp.length) {
o = read(uint8ArrayExp);
}
function read(arr) {
while (arr.position < arr.length) {
arr.position++;
if (arr[arr.position] < 127) {
return arr.position;
}
}
return arr.position;
}
let o = 0;
while (o < cArray.length) {
o = read(cArray);
}
function read(arr) {
while (arr.position < arr.length) {
arr.position++;
if (arr[arr.position] < 127) {
return arr.position++;
}
}
return arr.position;
}
let o = 0;
while (o < cUint8Array.length) {
o = read(cUint8Array, o);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regular Array | |
Uint Array | |
Uint Array Expando | |
Custom Array | |
Custom Uint Array |
Test name | Executions per second |
---|---|
Regular Array | 938.6 Ops/sec |
Uint Array | 939.6 Ops/sec |
Uint Array Expando | 875.0 Ops/sec |
Custom Array | 3601176.5 Ops/sec |
Custom Uint Array | 3610304.0 Ops/sec |
Measuring performance differences between various approaches to extending ArrayBufferView is a complex task, as it depends on the specific use case and requirements. Here's a breakdown of what's being tested:
Main Variables:
Uint8Array
: A typed array for 8-bit unsigned integers.CustomUint8Array
(CUsU) which extends Uint8Array
with additional functionality (explained later).CustomArray
(CA) which extends the built-in Array
type with a custom implementation.CustomUint8Array (CUsU) Purpose:
The CustomUint8Array
extension provides a lightweight way to add functionality to Uint8Array without creating a new object. It achieves this by adding a position
property and overriding the basic operations of Uint8Array, such as indexing and slicing. The goal is to optimize performance by allowing developers to take advantage of existing Uint8Array implementations while still providing some custom functionality.
Performance Considerations:
Recent Benchmark Results:
The latest benchmark results show that:
CustomUint8Array
(CUsU) performs best across all platforms and tests, with an average execution rate of around 3.6 million executions per second.CA
) follows closely behind, but lags about 50% in performance compared to CUsU.RA
) and Uint Array (UA
) have significantly lower performance rates.Insights:
CustomUint8Array
extension provides a lightweight way to optimize Uint8Array operations while still maintaining compatibility with existing implementations.When evaluating these results, keep in mind that:
To get the most out of these results:
By understanding these factors, you can develop more effective strategies for optimizing your own performance-critical code.