let a = [];
for (let i = 0; i < 1000; i += 1) {
const values = [i, 10000 + i, 100000 + i];
values.forEach(value => {
a.push(value);
});
}
console.log(a);
let a = [];
for (let i = 0; i < 1000; i += 1) {
const values = [i, 10000 + i, 100000 + i];
Array.prototype.push.apply(a, values);
}
console.log(a);
const a = [];
for (let i = 0; i < 1000; i += 1) {
const values = [i, 10000 + i, 100000 + i];
a.push(values);
}
console.log(a);
let a = [];
for (let i = 0; i < 1000; i += 1) {
const values = [i, 10000 + i, 100000 + i];
a = [a, values];
}
console.log(a);
let a = [];
for (let i = 0; i < 1000; i += 1) {
const values = [i, 10000 + i, 100000 + i];
a.push(values);
}
console.log(a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
push | |
push.apply | |
const push spread | |
reassign spread | |
let push spread |
Test name | Executions per second |
---|---|
push | 1393.9 Ops/sec |
push.apply | 1150.8 Ops/sec |
const push spread | 1398.5 Ops/sec |
reassign spread | 294.5 Ops/sec |
let push spread | 1387.6 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Overview
The benchmark compares the performance of four different approaches to push elements onto an array:
let a = []
and then using push()
const a = []
and then using push()
(with spread syntax)let a = []
and then using push(...values)
a
variable to a new array with spread syntax (a = [...newArray, ...values]
)Options Compared
The benchmark is comparing the performance of these four approaches:
let a = []
+ push()
: Using a mutable variable a
and pushing elements onto it using the push()
method.const a = []
+ push()**: Using an immutable variable
aand pushing elements onto it using the
push()method. Note that this approach is less efficient because
a` cannot be reassigned.let a = []
+ push(...)**: Using a mutable variable
a and pushing elements onto it using the spread syntax (
...values). This approach is similar to the first one but uses spread syntax instead of
push()`.a = [...newArray, ...values]
): Reassigning the a
variable to a new array with spread syntax.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
let a = []
+ push()
: Pros: Simple and easy to understand. Cons: Less efficient due to repeated reassignments.const a = []
+ push()`: Pros: Immutable, which can be beneficial for performance and memory safety. Cons: Less efficient due to constant reassignments.let a = []
+ push(...)`: Pros: Similar to the first approach but with spread syntax, which can be more concise. Cons: Still less efficient than reassigning.a = [...newArray, ...values]
): Pros: Most efficient due to minimizing the number of assignments and using a single operation to create the new array. Cons: Requires creating a new array on each iteration.Library and Special JS Features
The benchmark uses the push()
method, which is a part of the Array prototype in JavaScript. The spread syntax (...
) is also used, but it's not specific to any library or feature.
Other Alternatives
If you want to test other approaches, here are some alternatives:
unshift()
instead of push()
splice()
instead of push()
concat()
with spread syntax ([...array].concat(values)
)However, the benchmark is already testing some common and efficient approaches, so it's likely that these alternatives wouldn't show significant differences in performance.