Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
remove last array element (slice vs splice vs length vs pop)
(version: 0)
Comparing performance of:
slice vs splice vs length vs pop
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
slice
let test_array = [1, 2, 3, 4]; test_array.slice(0, 3);
splice
let test_array = [1, 2, 3, 4]; test_array.splice(3, 1);
length
let test_array = [1, 2, 3, 4]; test_array.length = 3;
pop
let test_array = [1, 2, 3, 4]; test_array.pop();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
slice
splice
length
pop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
24587666.0 Ops/sec
splice
16281201.0 Ops/sec
length
10719887.0 Ops/sec
pop
43813216.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark explores different ways to remove the last element from an array in JavaScript: **Methods Compared:** * **`slice(0, 3)`:** Creates a new array containing all elements up to (but excluding) the fourth element. Effectively discarding the last one. * **`splice(3, 1)`:** Removes one element starting at index 3 (the last element). Modifies the original array directly. * **`length = 3;`:** Sets the array's length to 3, effectively removing elements beyond that point. Modifies the original array directly. * **`pop()`:** Removes and returns the last element of the array. Modifies the original array directly. **Pros and Cons:** | Method | Pros | Cons | |--------------|----------------------------------------|-----------------------------------| | `slice(0, 3)` | Creates a new array, preserving the original | Less efficient than methods modifying the array directly | | `splice(3, 1)` | Modifies the original array | Can be slightly less performant than `length` for large arrays | | `length = 3;`| Modifies the original array directly | May not be as intuitive as other methods | | `pop()` | Efficient, returns the removed element | Modifies the original array directly | **Considerations:** * **Performance:** `pop()` and `splice` generally perform best for removing a single element from the end of an array. * **Data Preservation:** If you need to keep the original array intact, `slice(0, 3)` is the safest option. * **Clarity:** Choose the method that makes your code most readable and understandable. Let me know if you'd like a deeper dive into any specific aspect of this benchmark!
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
Empty array: Splice vs Shift vs Pop
Comments
Confirm delete:
Do you really want to delete benchmark?