Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Concat vs. Spread – erikras
(version: 1)
Comparing performance of:
Concat vs Append via Spread vs Prepend
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random()] var toAppend = Math.random()
Tests:
Concat
array.concat(toAppend)
Append via Spread
[...array, toAppend]
Prepend
[toAppend, ...array]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Concat
Append via Spread
Prepend
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark definition defines two main concepts: concatenating arrays using different methods (`array.concat(toAppend)` and `[...array, toAppend]`) and prepending elements to an array (`[toAppend, ...array]`). **Options Compared** Two main options are compared: 1. **Concatenation using `array.concat(toAppend)`:** This method takes two arguments: the array to concatenate and the element to append. 2. **Prepend and Append using spread operator `[...array, toAppend]`:** This method uses the spread operator (`[...]`) to create a new array with the elements in reverse order (prepend) or in the original order (append). **Pros and Cons** * `array.concat(toAppend)`: + Pros: Simple, widely supported, and well-documented. + Cons: May be slower due to the need for method lookup (`concat()`). * `[...array, toAppend]` (Prepend): + Pros: Modern, concise, and fast. + Cons: Requires support for the spread operator in older browsers and may not work as expected if `toAppend` is a large value. **Library and Purpose** There are no libraries used in this benchmark. However, it's worth noting that JavaScript engines like V8 (used by Google Chrome) implement the spread operator using native code, making it faster than other methods. **Special JS Features or Syntax** The test uses the spread operator (`[...array, toAppend]`) and `concat()` method. These are standard features of modern JavaScript, but older browsers may not support them. **Other Considerations** When choosing between these two approaches, consider the trade-off between conciseness, performance, and compatibility: * For small arrays or performance-critical code, `[...array, toAppend]` might be a better choice. * For larger arrays or in situations where compatibility with older browsers is crucial, `array.concat(toAppend)` might be a safer bet. **Alternatives** If you're looking for alternative approaches to concatenate or prepend arrays, consider the following: 1. **Using `slice()` and `push()`**: This method creates a new array using `slice()` and appends elements using `push()`: `[...array.slice(), toAppend]`. 2. **Using `Array.prototype.push.apply()`**: This method uses the `apply()` method to apply `push()` to an existing array: `new Array(array.length + 1).fill(0).map((_, i) => (i === 0 ? toAppend : array[i - 1]))`. Keep in mind that these alternatives may have different performance characteristics and compatibility issues compared to the original methods.
Related benchmarks:
Array.prototype.concat vs spread operator - Immutable version
Array concat vs spread operator vs push with random array 10000
Array.prototype.concat vs splice for joining two arrays test2
Array.prototype.concat vs spread operator vs flat [huge collection]
Comments
Confirm delete:
Do you really want to delete benchmark?