Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Slice vs Splice vs Shift vs pop
(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() vs array.pop()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "asdas", "asdas", "asda", "asdas", "asd", "adas", "asefacw", "asdfacs"] var itemsToRemove = 1
Tests:
array.slice()
array = array.slice(1)
array.splice()
array.splice(0, itemsToRemove)
array.shift()
array.shift()
array.pop()
array.pop()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array.slice()
array.splice()
array.shift()
array.pop()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36 EdgA/135.0.0.0
Browser/OS:
Chrome Mobile 135 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.slice()
2646963.2 Ops/sec
array.splice()
15421525.0 Ops/sec
array.shift()
31068284.0 Ops/sec
array.pop()
46512896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what is being tested on the provided JSON and the options being compared. **Benchmark Definition** The benchmark is designed to compare the speed of removing 2 items from the beginning of an array using different methods: `slice()`, `splice()`, `shift()`, and `pop()`. **Options Being Compared** Here are the options being compared, along with their pros and cons: 1. **`array.slice(1)`**: This method creates a new array that includes all elements from the original array, starting from the second element (index 1). The original array remains unchanged. * Pros: Efficient in terms of memory usage, as it doesn't modify the original array. * Cons: Creates a new array object, which can be slower than modifying an existing array. 2. **`array.splice(0, itemsToRemove)`**: This method removes `itemsToRemove` elements from the beginning of the array and returns an array of removed elements. * Pros: Can modify the original array in-place, which might be faster for large arrays. * Cons: Removes elements from the beginning of the array, which can affect subsequent operations. Also, it modifies the original array, so results may vary depending on the use case. 3. **`array.shift()`**: This method removes and returns the first element from the array. * Pros: Simple and fast, as it only needs to access the first element. * Cons: Removes the first element from the array, which can affect subsequent operations. Also, if the array is empty, it throws an error. 4. **`array.pop()`**: This method removes and returns the last element from the array. * Pros: Simple and fast, as it only needs to access the last element. * Cons: Removes the last element from the array, which can affect subsequent operations. Also, if the array is empty, it throws an error. **Library Usage** None of the benchmark cases use any external libraries. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. Now, let's discuss other alternatives: * **`array.indexOf()` and `array.findIndex()`**: These methods can be used to find the index of a specific element in the array. However, they don't remove elements from the beginning of the array. * **`Array.prototype.map()`**: This method creates a new array with the results of applying a provided function to each element in the original array. It's not suitable for removing elements from the beginning of an array. The benchmark provides a simple and concise way to compare the performance of different methods for removing elements from an array. By using the same array size and number of items removed, the results can be directly compared, making it easier to determine which method is fastest in most scenarios.
Related benchmarks:
Slice vs Splice vs Shift
Slice vs Splice vs Shift (Large Array)
Slice vs Splice vs Shiftxxxxxx
Slice vs Splice vs Shift for 1 item
Slice vs Splice vs Shift 231
Comments
Confirm delete:
Do you really want to delete benchmark?