Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs splice index
(version: 0)
See if filter is faster than splice method for deletion
Comparing performance of:
Filter vs Splice
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Filter
let array = [1,2,3,4,5,6,7,8] const ignore = [2,4,7] array = array.filter(item => !ignore.includes(item)); console.log(array);
Splice
let array = [1,2,3,4,5,6,7,8] const ignore = [2,4,7] for(i = 0; i < array.length; i++) { if(ignore.includes(array[i])) { array.splice(i,1); i--; } } console.log(array);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter
Splice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 137 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter
32613.2 Ops/sec
Splice
29108.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Description** The benchmark is designed to compare the performance of two approaches for deleting elements from an array: 1. `filter` method 2. `splice` method with manual index management **Options Being Compared** * `filter`: uses the `Array.prototype.filter()` method to create a new array with only the elements that pass the test implemented by the provided function. * `splice`: uses the `Array.prototype.splice()` method to remove elements from the original array, but manually manages the indices to avoid shifting the remaining elements. **Pros and Cons** * `filter`: + Pros: concise, easy to read, and maintainable code. Creates a new array with filtered elements, leaving the original array unchanged. + Cons: may create unnecessary memory allocation for large arrays, potentially impacting performance. * `splice`: + Pros: modifies the original array in-place, which can be beneficial for large datasets or performance-critical applications. + Cons: requires manual index management to avoid shifting elements, making the code more complex and harder to maintain. **Library Used** None explicitly mentioned. However, `Array.prototype.filter()` and `Array.prototype.splice()` are built-in JavaScript methods that do not require any additional libraries. **Special JS Features/Syntax** No special features or syntax are used in this benchmark. The tests focus on the performance comparison of the two approaches. **Other Alternatives** * For deleting elements from an array, other approaches include: + Using `map()` to create a new array with only the desired elements. + Using `forEach()` to iterate over the array and perform actions on each element. + Using `reduce()` to accumulate elements in a new array or perform aggregate operations. In general, the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Remove by splice vs copyWithin vs filter vs set.delete
Deleting using .splice vs .filter
filter vs splice and indexOf
Remove by splice vs filter with a known index
Remove by findIndex+splice vs copyWithin vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?