const a = Array(1000).fill('a');
const b = Array(1000).fill('b');
const ab = a.concat(b);
const a = Array(1000).fill('a');
const b = Array(1000).fill('b');
const ab = [a, b];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 319415.2 Ops/sec |
Spread operator | 178494.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark measures the performance difference between two approaches to concatenate arrays in JavaScript: using Array.prototype.concat()
and the spread operator (...
).
What is tested?
The benchmark tests how fast each approach can concatenate two large arrays (1000 elements) created with Array(1000).fill('a')
and Array(1000).fill('b')
.
Options compared:
Array.prototype.concat()
:...
):Pros and cons:
Array.prototype.concat()
:...
):concat()
.Array.prototype.concat()
.Library/dependencies:
None mentioned in the benchmark definition or test cases.
Special JavaScript features/syntax:
The spread operator (...
) is a relatively recent feature introduced in ECMAScript 2015 (ES6). It's widely supported across modern browsers, but may not work in older versions.
Other alternatives:
Array.prototype.push()
and Array.prototype.length
:push()
.Array.prototype.forEach()
or Array.prototype.map()
:Keep in mind that these alternatives may have varying degrees of performance and readability compared to the spread operator.
Now, if you're curious about how MeasureThat.net generates benchmark results or what else is tested on the website, feel free to ask!