Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some vs. Filter vs. indexOf vs. includes
(version: 0)
Comparing performance of:
Array.some vs Array.filter vs Array.indexOf vs Array.includes
Created:
6 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;
Array.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
Array.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.some
386576.1 Ops/sec
Array.filter
89208.9 Ops/sec
Array.indexOf
2551988.0 Ops/sec
Array.includes
2570441.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. The provided JSON represents a benchmark that tests four different approaches for checking if an array contains a specific value: `some()`, `filter()`, `indexOf()`, and `includes()`. **What is tested on the provided json?** In this benchmark, we have two arrays: 1. `hasZero`: an array containing 10,000 random integers between 0 and 999. 2. `withoutZero`: an array containing 10,000 random integers between 0 and 999, but with one additional value (1000). The benchmark checks the performance of each approach on both arrays: * `some()`: returns `true` if at least one element in the array satisfies the condition. * `filter()`: returns a new array containing only the elements that satisfy the condition. * `indexOf()`: returns the index of the first occurrence of the specified value, or -1 if not found. * `includes()`: returns `true` if the array contains the specified value. **Options compared** The benchmark compares the performance of each approach on the two arrays: | Approach | Array | Method | | --- | --- | --- | | `some()` | `hasZero`, `withoutZero` | Iterates through the array, checking for presence | | `filter()` | `hasZero`, `withoutZero` | Creates a new array with elements that satisfy the condition | | `indexOf()` | `hasZero`, `withoutZero` | Searches for the specified value in the array | | `includes()` | `hasZero`, `withoutZero` | Checks if the array contains the specified value | **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `some()`: Pros: + Simple to implement + Fast for small arrays + Suitable for finding any element, not just exact matches * Cons: + Can be slower than other methods for large arrays + Returns true if any element satisfies the condition, regardless of order * `filter()`: Pros: + Creates a new array with filtered elements, which can be useful for further processing + Suitable for finding all matching elements in the correct order * Cons: + Requires creating a new array, which can be memory-intensive + Slower than other methods for large arrays * `indexOf()`: Pros: + Returns the index of the first occurrence, making it suitable for finding a specific element + Can be faster than other methods for exact matches * Cons: + May return -1 if the value is not found + Not suitable for finding any element that satisfies the condition * `includes()`: Pros: + Fast and efficient for checking presence + Suitable for both small and large arrays * Cons: + Returns true or false, but not an index **Other considerations** * `some()` and `filter()` are often used together to implement more complex checks. * `indexOf()` is commonly used with `slice()` or `substring()` to extract a specific part of the array. **Library usage** In this benchmark, no libraries are explicitly mentioned. However, it's worth noting that some JavaScript engines may use libraries like V8 (Chrome) or SpiderMonkey (Firefox) under the hood. **Special JS features or syntax** There are no special JS features or syntax used in this benchmark. **Alternatives** If you're looking for alternative approaches to these methods, here are a few options: * For `some()` and `filter()`, you can also use `every()` and `map()` respectively. * For `indexOf()`, you can use `findIndex()` as an alternative. * For `includes()`, you can use `reduce()` or other accumulation-based approaches to implement a custom check. Keep in mind that these alternatives may have different performance characteristics, and some might be more suitable for your specific use case.
Related benchmarks:
Some vs. Filter vs. indexOf 1
Some vs. Filter vs. indexOf v includes
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?