var first = ['1', '2', '3'];
var second = ['4', '5', '6'];
first = [first, second]
first.push(second)
first.concat(second)
first.push.apply(first, second)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Push spread | |
Concat | |
Push apply |
Test name | Executions per second |
---|---|
Spread | 3038.4 Ops/sec |
Push spread | 2802550.8 Ops/sec |
Concat | 5.3 Ops/sec |
Push apply | 2306947.5 Ops/sec |
Let's dive into the world of JavaScript benchmarks.
Overview
MeasureThat.net is a website that allows users to create and run microbenchmarks for JavaScript. The provided JSON represents a benchmark definition, which includes the test cases, script preparation code, and HTML preparation code (none in this case). The latest benchmark results show the performance comparison between different approaches: Spread
, Push spread
, Concat
, and Push apply
.
What is tested
In this benchmark, four different approaches are compared:
Spread
: Using the spread operator (...
) to concatenate two arrays.Push spread
: Pushing an array onto another array using the spread operator (...
).Concat
: Concatenating two arrays using the concat()
method.Push apply
: Pushing an array onto another array using the apply()
method.Options comparison
Here's a brief overview of each approach, their pros and cons:
Spread
:Push spread
:Concat
:concat()
).Push apply
:apply()
method.Library usage
None of the test cases use any external libraries or frameworks. The focus is solely on comparing the different JavaScript operators and methods.
Special JS features or syntax
The benchmark uses the spread operator (...
) in a few test cases, which is a relatively recent addition to the JavaScript language (introduced in ECMAScript 2018). This feature allows for concise array concatenation and spreading of elements into arrays.
Other considerations:
push()
method and concat()
method are both widely supported by most modern browsers.apply()
method is also widely supported, but its usage can be less intuitive for some developers.Alternatives
If you're looking for alternative approaches or alternatives to these methods, consider the following:
Buffer.concat()
(for Node.js environments) or concat()
(in browser environments) to concatenate buffers.Array.prototype.set()
, which can be used for efficient concatenation.These alternatives may offer better performance or more concise syntax but are not part of the standard JavaScript language and may require specific implementations depending on the environment.