Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.length vs array.splice
(version: 0)
Want to make sure that delete last elements through length is faster.
Comparing performance of:
Deleting through length vs Deleting through spclie vs Slicing
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var sut = []; for (let i = 0; i < 10000; i++) { sut.push(i); }
Tests:
Deleting through length
sut.length = 10;
Deleting through spclie
sut.splice(10, 9000);
Slicing
sut.slice(10);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Deleting through length
Deleting through spclie
Slicing
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Deleting through length
28523882.0 Ops/sec
Deleting through spclie
37335080.0 Ops/sec
Slicing
50979748.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case for measuring the performance of three different approaches to delete elements from an array: `length`, `splice`, and `slice`. **Tested Options** The benchmark compares the execution time of deleting the last 9000 elements from an array using: 1. **`length`**: Using the length property to set the length of the array, which effectively removes all elements after a certain index. 2. **`splice`**: Using the `splice()` method with negative indices to delete elements from the end of the array. 3. **`slice`**: Using the `slice()` method with a start index equal to the desired offset to create a new array, effectively removing all elements before that index. **Pros and Cons** * **`length`**: This approach is simple and efficient but can be slow for large arrays since it involves setting the length property of the array, which may trigger garbage collection. * **`splice`**: This approach is more flexible and allows for partial deletion, but its performance may degrade as the number of elements to delete increases. It also modifies the original array. * **`slice`**: This approach creates a new array without modifying the original, making it suitable for large arrays. However, it requires creating a new object in memory. **Library and Special JS Features** None mentioned in the provided JSON. **Other Considerations** When comparing performance, other factors such as: * Array size: Larger arrays may require more time to process. * Browser engine: Different browsers may execute JavaScript differently. * Garbage collection: Frequent deletion of elements can trigger garbage collection, which may impact performance. **Alternatives** If you need to delete large numbers of elements from an array efficiently, consider using: 1. **`filter()`**: Instead of deleting elements, use the `filter()` method to create a new array with only the desired elements. 2. **`map()`**: Use the `map()` method to create a new array with transformed data, effectively removing unwanted elements. 3. **NativeArray methods**: Some browsers have native methods like `splice()` and `slice()` optimized for performance. In conclusion, the benchmark provides valuable insights into the performance differences between these three approaches to delete elements from an array. By considering factors such as array size, browser engine, and garbage collection, you can make informed decisions about which approach is best suited for your specific use case.
Related benchmarks:
splice vs spread operator for adding elements into very large 2D arrays
Array splice vs delete
Array.splice(0, N) vs Array.length === N
Empty array: Splice vs Shift
Comments
Confirm delete:
Do you really want to delete benchmark?