var a = [Array(10000)].map(_ => Math.random());
var ta = (new Float64Array(10000)).map(_ => Math.random());
a.reverse();
ta.reverse();
for (let i = 0; i < 10000; ++i)
a[i] = a[i] + 1;
for (let i = 0; i < 10000; ++i)
ta[i] = ta[i] + 1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array reverse | |
typedArray reverse | |
array i/o | |
typedArray i/o |
Test name | Executions per second |
---|---|
array reverse | 142797.8 Ops/sec |
typedArray reverse | 348571.4 Ops/sec |
array i/o | 42226.1 Ops/sec |
typedArray i/o | 36137.1 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of arrays (native JavaScript data structure) with Float64Array (a typed array for 64-bit floating-point numbers). The test cases focus on common operations like reversing an array, assigning values to elements in an array, and accessing elements in a typed array.
Options Compared
The benchmark compares two approaches:
Pros and Cons
Native Array:
Pros:
Cons:
TypedArray (Float64Array):
Pros:
Cons:
Float64Array
constructorLibrary Used:
In this benchmark, no specific library is mentioned. However, Float64Array is a native JavaScript data structure that does not require any external libraries.
Special JS Feature or Syntax:
There are no special JavaScript features or syntaxes used in this benchmark. The tests only involve basic arithmetic operations and array manipulations.
Other Alternatives:
To improve performance, alternative approaches can be explored:
Keep in mind that these alternatives may require additional setup and expertise.
Benchmark Preparation Code Explanation
The preparation code is used to create an array of 10,000 random numbers and another typed array (Float64Array) for the same purpose:
var a = [...Array(10000)].map(_ => Math.random()); // native array
var ta = (new Float64Array(10000)).map(_ => Math.random()); // typed array
This code uses the spread operator (...
) to create an array from an array expression, which is then used in a map
function to generate random numbers. For the typed array, it creates a new Float64Array instance and maps over its elements using the same random number generator.
Individual Test Cases:
Each test case measures the performance of a specific operation:
array reverse
: Reverses the native arraytypedArray reverse
: Reverses the typed array (Float64Array)array i/o
: Assigns values to elements in the native array, then accesses those values.typedArray i/o
: Assigns values to elements in the typed array (Float64Array), then accesses those values.These tests are designed to identify any potential performance differences between using native arrays and typed arrays for specific operations.