Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs Array.prototype.splice
(version: 2)
Comparing performance of:
immutable with concat vs direct mutation with splice
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = []; var b = []; for (let i = 0; i < 200000; i++) { let s = i.toString(); a.push(s); b.push(s); }
Tests:
immutable with concat
a.slice(0, 100000).concat(b, a.slice(100000))
direct mutation with splice
let pos_a = 100000; let pos_b = 0; for (let i = 0; i < 4; i++) { a.splice(pos_a, 0, ...(b.slice(pos_b, pos_b + 50000))); pos_a += 50000; pos_b += 50000; }
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
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 benchmark and explain what's being tested. **What is being tested?** The provided JSON represents two test cases for measuring the performance of JavaScript arrays using different methods: `concat` and `splice`. The tests aim to compare the execution speed of these two approaches when working with large datasets. **Options compared:** 1. **Immutable array concatenation using `concat`:** This approach creates a new array by concatenating two existing arrays. It involves creating multiple intermediate arrays and copying data between them. 2. **Mutable array mutation using `splice`:** This approach modifies the original array by inserting or removing elements at specific positions. It involves updating the internal state of the array. **Pros and Cons:** 1. **Immutable array concatenation using `concat`:** * Pros: + Easier to read and understand, as it creates a new array without modifying the existing one. + Less prone to side effects, as it doesn't modify the original array. * Cons: + Creates multiple intermediate arrays, which can lead to increased memory usage. + May be slower due to the overhead of creating and copying data between arrays. 2. **Mutable array mutation using `splice`:** * Pros: + More efficient in terms of memory usage, as it only modifies the existing array. + Can be faster, as it avoids the overhead of creating intermediate arrays. * Cons: + More complex and harder to understand, as it involves modifying the internal state of the array. + Prone to side effects, as changes to the original array can have unintended consequences. **Library usage:** None mentioned in the provided code. However, some JavaScript libraries may use optimized implementations of these methods or provide alternative approaches for working with arrays. **Special JS feature/syntax:** The tests do not use any special JavaScript features or syntax. The code is written in vanilla JavaScript and uses standard language constructs. **Other alternatives:** If you're looking for alternative approaches to these two methods, consider the following: 1. **Array.prototype.push() + Array.prototype.shift():** This approach involves pushing elements onto the end of an array and then shifting them to the front until a specified number is reached. While it's not as efficient as `concat`, it can be faster in some cases. 2. **Array.prototype.slice().reverse():** This approach involves creating a new array by slicing a portion of the original array and then reversing it. This method can be slower due to the overhead of creating and copying data between arrays. Keep in mind that these alternatives may have their own trade-offs in terms of performance, memory usage, and readability.
Related benchmarks:
Array spread vs. push performance
Concat vs Slice
Array construct vs array push vs array concat
Concat vs Slice f1
Array Shallow Copy: Array.prototype.slice() vs. Array.prototype.concat() vs. Spread syntax vs. Array.from()
Comments
Confirm delete:
Do you really want to delete benchmark?