Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some vs. Filter vs. indexOf v includes
(version: 0)
Comparing performance of:
Array.some vs Array.filter vs Array.indexOf vs includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var hasZero = []; var withoutZero = []; for (var i = 0; i < 10000; i++) { hasZero.push(Math.floor(Math.random() * 1000)); withoutZero.push(Math.floor(Math.random() * 1000) + 1) }
Tests:
Array.some
var tempResult = !!Math.round(Math.random()) ? hasZero.some(v => v === 0) : withoutZero.some(v => v === 0);
Array.filter
var tempResult = !!Math.round(Math.random()) ? hasZero.filter(v => v === 0) : withoutZero.filter(v => v === 0);
Array.indexOf
var tempResult = !!Math.round(Math.random()) ? hasZero.indexOf(0) > -1 : withoutZero.indexOf(0) > -1;
includes
var tempResult = !!Math.round(Math.random()) ? hasZero.includes(0) : withoutZero.includes(0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.some
Array.filter
Array.indexOf
includes
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.some
124514.0 Ops/sec
Array.filter
29363.9 Ops/sec
Array.indexOf
860346.8 Ops/sec
includes
840171.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The benchmark tests four different methods to check if an element is present in an array: `some`, `filter`, `indexOf`, and `includes`. The test data consists of two arrays, `hasZero` and `withoutZero`, which are populated with random integers. The arrays have similar elements, but the latter includes a zero. **Test Case Breakdown** 1. **Array.some**: This method returns true if at least one element in the array satisfies the provided condition. In this case, it checks if any of the elements in `hasZero` or `withoutZero` is equal to 0. 2. **Array.filter**: This method creates a new array with all elements that pass the test implemented by the provided function. In this case, it filters out elements that are not equal to 0 from both arrays. 3. **Array.indexOf**: This method returns the index of the first element in the array that satisfies the provided condition (i.e., is equal to 0). If no such element exists, it returns -1. 4. **includes**: This method checks if an element is present in the array. In this case, it checks if 0 is included in both arrays. **Comparison of Methods** Here's a summary of the pros and cons of each approach: * `some`: * Pros: Efficient for checking presence of multiple values, simple implementation. * Cons: Returns true as soon as a match is found, might be slow if array size is very large, can be slower than other methods in some cases due to branching. * `filter`: * Pros: Allows filtering out all non-matching elements, efficient for small arrays. * Cons: Creates new array, might be slower than direct indexing, more complex implementation. * `indexOf`: * Pros: Fastest method overall, works well with large arrays, no branching. * Cons: Returns index only if element is found (returns -1 otherwise), might require additional checks for non-matching elements. * `includes`: * Pros: Simple implementation, fast when array size is small. * Cons: Can be slower than direct indexing due to the function call overhead, might be slower than `indexOf` in some cases. **Library Usage** The `some`, `filter`, and `indexOf` methods use built-in JavaScript functions. No external libraries are required for this benchmark. No special JavaScript features or syntax are used in this benchmark.
Related benchmarks:
Some vs. Filter vs. indexOf 1
Some vs. Filter vs. indexOf vs every
Some vs. Filter vs. indexOf vs Includes
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
Comments
Confirm delete:
Do you really want to delete benchmark?