var size = 450000;
var buf = new ArrayBuffer(size);
var normalArray = new Array(size);
var arrayFromBuffer = new Uint8Array(buf);
var arrayWithoutBuffer = new Uint8Array(size);
for (i =0; i< size; i++){
normalArray[i] = 0;
}
for (i =0; i< size; i++){
arrayFromBuffer[i] = 0;
}
for (i =0; i< size; i++){
arrayWithoutBuffer[i] = 0;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
typed array from Buffer | |
typed Array no buffer |
Test name | Executions per second |
---|---|
Array | 18.7 Ops/sec |
typed array from Buffer | 19.7 Ops/sec |
typed Array no buffer | 19.7 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark compares the performance of three types of arrays in JavaScript: regular arrays (Array), typed arrays from buffers (Uint8Array, which we'll assume is a 8-bit unsigned integer array for this explanation), and a plain typed array without a buffer (Uint8Array, with the same assumption as above).
Benchmark Preparation Code
The script preparation code creates three arrays:
normalArray
: a regular JavaScript array of size 450,000.arrayFromBuffer
: a Uint8Array created from an ArrayBuffer of size 450,000. This is done by wrapping an existing ArrayBuffer in a new Uint8Array object.arrayWithoutBuffer
: another Uint8Array of size 450,000, created without referencing an existing ArrayBuffer.The script then populates each array with zeros using a loop that iterates from 0 to the array's length (size).
Benchmark Test Cases
There are three test cases:
normalArray
.arrayFromBuffer
.arrayWithoutBuffer
.Comparison and Pros/Cons
The three arrays are compared in terms of their ability to populate an array with zeros:
Library and Special Features
The benchmark uses the following libraries:
ArrayBuffer
, Uint8Array
, and the new
keyword for object creation.Other Alternatives
If you wanted to compare these arrays using a different approach:
Keep in mind that these alternatives would likely change the nature of the comparison, and the results might not be directly comparable to the JavaScript array tests.