Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter vs indexOf Speed small sized array 2
(version: 0)
Filter vs indexOf in array remove element Javascript
Comparing performance of:
remove finding element using indexof vs remove finding element using filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = []; for (var i = 0; i < 10; i++) { testArray.push(Math.floor(i * 10) + 1) }
Tests:
remove finding element using indexof
function removeElement(testArray, element) { return ( index = testArray.indexOf(element) !==-1) ? testArray.splice(index, 1):0; } removeElement(testArray, 9);
remove finding element using filter
function removeElement(testArray, element) { return testArray.filter(s => s !== element); } removeElement(testArray, 9);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
remove finding element using indexof
remove finding element using filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
remove finding element using indexof
9125195.0 Ops/sec
remove finding element using filter
19037464.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested, compared, and the pros and cons of each approach. **What is being tested?** The benchmark compares two approaches to remove an element from an array: 1. `filter()`: Removes all elements from the array that do not match a specified condition. 2. `indexOf()` and `splice()`: Finds the index of the first occurrence of a specified value in the array and removes it using `splice()`. **Options compared** The benchmark compares two options: 1. **Filter**: Uses the `filter()` method to remove elements from the array. * Pros: More concise, doesn't modify the original array. * Cons: May be slower than other methods for large arrays. 2. **IndexOf() and Splice**: Finds the index of the first occurrence of a specified value in the array using `indexOf()`, and then removes it using `splice()`. **Pros and cons** * **Filter**: + Pros: - More concise and readable code - Does not modify the original array + Cons: - May be slower than other methods for large arrays due to the overhead of creating a new array - Requires a callback function, which can add complexity * **IndexOf() and Splice**: + Pros: - Faster than filter for large arrays - More control over the removal process + Cons: - More verbose code - Modifies the original array **Library used** None. **Special JavaScript features or syntax** The benchmark uses the following feature: * **Callback functions**: The `filter()` method takes a callback function as an argument, which is called for each element in the array. * **Arrow functions**: The callback function in the filter example is written using an arrow function (`s => s !== element`). **Other alternatives** There are other ways to remove elements from an array in JavaScript: * Using `map()` and `slice()`: Create a new array with only the elements that match the condition. * Using a `for` loop: Iterate over the array and push or pop each element into a new array. For example: ```javascript // Map and slice function removeElement(testArray, element) { return testArray.filter(s => s !== element).slice(); } ``` Or: ```javascript // For loop function removeElement(testArray, element) { const result = []; for (let i = 0; i < testArray.length; i++) { if (testArray[i] !== element) { result.push(testArray[i]); } } return result; } ``` These alternatives may have different performance characteristics and are not necessarily better or worse than the `filter()` and `indexOf()`/`splice()` approaches.
Related benchmarks:
IndexOf vs Includes array of numbers
slice with indexof vs filter
Array.filter vs push
Remove first element from array - slice vs filter
Empty array: Splice vs Shift
Comments
Confirm delete:
Do you really want to delete benchmark?