Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Empty array: Splice vs Shift vs Pop
(version: 0)
Compares the speed for removing all items from the beginning of an array
Comparing performance of:
array.splice() vs array.shift() vs array.pop()
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [1, 2, 3, 4, 5, 6, 7, 8]
Tests:
array.splice()
array.splice(0)
array.shift()
while (array.length > 0) array.shift();
array.pop()
while (array.length > 0) array.pop();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.splice()
27720114.0 Ops/sec
array.shift()
36435736.0 Ops/sec
array.pop()
36534964.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares the speed of removing all items from the beginning of an array using three different approaches: `splice(0)`, `shift()`, and `pop()`. The goal is to determine which approach is the fastest. **Options Compared** * **`array.splice(0)`**: This method removes the first element(s) from the specified position in the array. In this case, it's used to remove all elements from the beginning of the array. * **`while (array.length > 0) array.shift();`**: This approach uses a `while` loop to continuously remove and return the first element from the array until there are no more elements left. * **`while (array.length > 0) array.pop();`**: Similar to the previous approach, this method uses a `while` loop to remove and return the last element from the array until there are no more elements left. **Pros and Cons of Each Approach** * **`array.splice(0)`**: * Pros: This method is concise and easy to read. * Cons: It modifies the original array, which may not be desirable in some cases. Additionally, if the array has a large number of elements, this method can be slower due to the overhead of modifying the array. * **`while (array.length > 0) array.shift();`**: * Pros: This approach is more memory-efficient than `splice(0)` since it doesn't modify the original array. It's also a good choice when you need to preserve the original order of elements. * Cons: The code can be less readable due to the use of a `while` loop, and performance may degrade if the array has a large number of elements. * **`while (array.length > 0) array.pop();`**: * Pros: Similar to `shift()`, this approach preserves the original order of elements. However, it's slightly slower due to the overhead of accessing the last element in the array using `pop()`. **Library Used** In none of the provided benchmark definitions is a library explicitly mentioned. However, MeasureThat.net uses the V8 JavaScript engine, which is a popular open-source JavaScript engine developed by Google. **Special JS Features or Syntax (Not Applicable)** There are no special JavaScript features or syntax used in these benchmark tests. **Alternatives** For removing elements from an array, you can also consider other approaches such as: * **`slice()`**: Creates a new array containing the specified elements from the original array. * **`filter()`**: Creates a new array with all elements that pass the test implemented by the provided function. * **`map()`**: Creates a new array with the results of applying the provided function to each element in the original array. Keep in mind that these alternatives might have different performance characteristics and use cases compared to `splice()`, `shift()`, and `pop()`.
Related benchmarks:
Splice vs shift to remove at beginning of array (fixed from slice)
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?