const a = [];
for (let i = 0; i < 10_000; i++) {
a.push([]);
}
const a = [];
for (let i = 0; i < 10_000; i++) {
a.push(new Float32Array());
}
const a = new Float32Array(10_000);
for (let i = 0; i < 10_000; i++) {
a[i] = new Float32Array();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
create arrays | |
create typed arrays | |
create typed arrays in typed array |
Test name | Executions per second |
---|---|
create arrays | 9357.8 Ops/sec |
create typed arrays | 2153.1 Ops/sec |
create typed arrays in typed array | 338.7 Ops/sec |
I'd be happy to explain what's being tested in the provided benchmark.
The benchmark is comparing the performance of three different approaches when creating arrays and typed arrays in JavaScript:
a
and then pushes new empty arrays into it using the push()
method. This approach creates a new array object on each push operation.Float32Array
and then pushes new instances of Float32Array
into it using the push()
method. This approach also creates a new array object on each push operation, but with a specific type (float32).Float32Array
and then pushes new instances of Float32Array
into it using the assignment operator (a[i] = new Float32Array();
). This approach creates a new instance of Float32Array
on each push operation, but within the context of an existing typed array.Pros and Cons:
Library usage:
There is no explicit library usage mentioned in the benchmark definitions or test cases.
Special JS feature or syntax:
None of the provided test cases use any special JavaScript features or syntax.
Other considerations:
Alternatives:
If you wanted to create an alternative benchmark, you could consider adding more test cases to cover different scenarios, such as:
Array.from()
or TypedArray.from()
Array.prototype.slice()
vs. Array.prototype.push()
)