Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS filter vs splice +indexOf
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.filter vs Array.splice + Array.indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Array.filter
const item = arr.filter(item => item !== 1E5);
Array.splice + Array.indexOf
const index = arr.splice(arr.indexOf(1E5), 1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.filter
Array.splice + Array.indexOf
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.filter
294.5 Ops/sec
Array.splice + Array.indexOf
1333574.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark and explain what's being tested. **What is being tested?** The benchmark tests two different approaches to find and remove an item from an array in JavaScript: 1. `Array.filter()`: This method creates a new array with all elements that pass the test implemented by the provided function. 2. `Array.splice() + Array.indexOf()`: This approach uses the `indexOf()` method to find the index of the element to be removed, and then uses `splice()` to remove the element at that index. **Options compared** The two options being compared are: * `Array.filter()` * `Array.splice() + Array.indexOf()` **Pros and Cons of each approach:** * **`Array.filter()`**: + Pros: - More concise and expressive code - Creates a new array, which can be more memory-efficient for large datasets + Cons: - Can be slower than `splice()` + `indexOf()` for very large arrays (due to the overhead of creating a new array) * **`Array.splice() + Array.indexOf()`**: + Pros: - Faster execution time for large arrays (since it only modifies the original array) + Cons: - Less concise and expressive code - Can be more memory-intensive since it modifies the original array **Library used** The `Array.filter()` method is a built-in JavaScript method, introduced in ECMAScript 5. **Special JS feature or syntax** None are mentioned in this benchmark. **Other alternatives** There are other methods to remove an item from an array in JavaScript, such as: * Using the `map()` method with the callback function returning an empty value (e.g., `arr.map(item => item === 1E5 ? undefined : item)`), but this is less efficient than using `filter()`. * Using a regular loop to iterate over the array and remove items (e.g., `for (var i = 0; i < arr.length; i++) { if (arr[i] === 1E5) arr.splice(i, 1); }`), but this is less concise than using `filter()` or `splice() + indexOf()`. The benchmark uses the most common and efficient approach for each method, which is why we see `Array.filter()` and `Array.splice() + Array.indexOf()` being compared.
Related benchmarks:
FindIndex + splice vs filter FindIndex
splice(indexof()) vs filter
Filter vs FindIndex splice
filter vs findIndex & splice (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?