Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs splice for joining two arrays fixed
(version: 0)
Comparing performance of:
immutable with concat vs direct mutation with splice + spread
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
immutable with concat
var a = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ] var b = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ] var other = a.concat(b)
direct mutation with splice + spread
var a = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ] var b = [ "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello" ] a.splice(-1, 0, ...b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
immutable with concat
direct mutation with splice + spread
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 benchmark. **Benchmark Description** The benchmark is comparing two approaches to joining two arrays in JavaScript: using `Array.prototype.concat()` and using `splice()` with the spread operator (`...`). **Options Compared** The benchmark is testing two options: 1. **Immutable Approach with `concat()`**: This approach creates a new array by concatenating the two input arrays. The original arrays remain unchanged. 2. **Mutable Approach with `splice()` + Spread Operator**: This approach modifies the first array (`.a`) in place by inserting elements from the second array (`b`) at the end of `.a` using `splice()`, and then uses the spread operator to pass the elements of `b` to the `splice()` method. **Pros and Cons** 1. **Immutable Approach with `concat()`** * Pros: + Creates a new array, avoiding mutation of the original arrays. + Can be more predictable and easier to reason about. * Cons: + Creates unnecessary memory allocation and copies data. 2. **Mutable Approach with `splice()` + Spread Operator** * Pros: + Avoids creating a new array, reducing memory allocation and copying data. + Can be more efficient for large arrays or when the order of elements matters. * Cons: + Modifies the original array, which can lead to unexpected behavior if not properly validated. + May have performance overhead due to the `splice()` method. **Library Used** None. The benchmark is using built-in JavaScript methods (`Array.prototype.concat()`, `splice()`) without any additional libraries. **Special JS Feature/Syntax** The benchmark uses the spread operator (`...`) introduced in ECMAScript 2015 (ES6). This feature allows for more concise and expressive array creation. **Other Alternatives** For joining arrays, there are other approaches: 1. **Array.prototype.push()**: Append elements to an existing array using `push()`, followed by assigning the resulting array back to the original variable. 2. **Array.prototype.slice() + Array.prototype.push()**: Create a new array with `slice()` and then append elements using `push()`. 3. **Array.prototype.concat()**: As mentioned earlier, creates a new array by concatenating two input arrays. Keep in mind that these alternatives may have different trade-offs in terms of performance, memory usage, and predictability compared to the approaches tested in this benchmark.
Related benchmarks:
The Many Ways of Concatenating
Array.prototype.concat vs Array.prototype.splice
Array.prototype.concat vs splice for joining two arrays test2
Array Splice vs Concat
concat vs splice for joining two arrays
Comments
Confirm delete:
Do you really want to delete benchmark?