Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift (Large Array)
(version: 0)
Compares the speed for removing 2 items from the beginning of an array
Comparing performance of:
array.slice() vs array.splice() vs array.shift()
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array(3000).fill(0) var itemsToRemove = 1000
Tests:
array.slice()
array = array.slice(2)
array.splice()
array.splice(0, itemsToRemove)
array.shift()
for (let i = 0 ; i < itemsToRemove ; i++) array.shift()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array.slice()
array.splice()
array.shift()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.slice()
63761336.0 Ops/sec
array.splice()
23080100.0 Ops/sec
array.shift()
238037.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares the performance of three methods to remove 2 items from the beginning of an array: 1. `array.slice(2)` 2. `array.splice(0, 1000)` (removes 1000 elements) 3. `for (let i = 0; i < 1000; i++) array.shift()` (shifts 1000 elements) **Library and Purpose** The benchmark uses the `Array` prototype method for all three tests, which is a built-in JavaScript library. **Special JS Features and Syntax** None of the test cases use any special JavaScript features or syntax. The focus is on comparing the performance of basic array manipulation methods. **Comparison of Methods** 1. **`array.slice(2)`**: This method creates a new array with elements from index 2 to the end of the original array. It has a time complexity of O(n), where n is the length of the array. * Pros: Efficient and concise way to extract a subset of an array. * Cons: Creates a new array, which can lead to increased memory usage. 2. **`array.splice(0, 1000)`**: This method removes elements from the beginning of the array and returns them as an array. It has a time complexity of O(n), where n is the length of the removed elements (in this case, 1000). * Pros: Removes elements from the beginning of the array in one operation. * Cons: Can be slow if many elements need to be removed, and it modifies the original array. 3. **`for (let i = 0; i < 1000; i++) array.shift()`**: This method uses a loop to shift elements from the beginning of the array. It has a time complexity of O(n), where n is the number of elements shifted. * Pros: Does not modify the original array and can be more efficient for smaller arrays or when only shifting a small portion. * Cons: Can be slower than `splice` for large arrays, as it involves multiple loop iterations. **Benchmark Results** The latest benchmark results show that: * `array.splice(0, 1000)` is the fastest method, with an average execution time of around 107,759,000 executions per second. * `array.slice(2)` is slower, with an average execution time of around 118,810,860 executions per second. * `for (let i = 0; i < 1000; i++) array.shift()` is the slowest method, with an average execution time of around 13,587 executions per second. **Alternatives** Other alternatives to these methods include: * Using `Array.prototype.slice.call()` or `Array.prototype.splice.call()` to avoid creating new objects. * Using a library like Lodash to optimize array manipulation. * Using a Just-In-Time (JIT) compiler or optimizing the code for a specific browser/OS combination. Keep in mind that the performance differences between these methods can be significant, and the choice of method depends on the specific use case and requirements.
Related benchmarks:
Slice vs Splice vs Shift
Slice vs Splice vs Shift for 1 item
Slice vs Splice vs Shift 231
Empty array: Splice vs Shift
Comments
Confirm delete:
Do you really want to delete benchmark?