Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some vs. Filter vs. indexOf vs. includes (Corrected version)
(version: 0)
Comparing performance of:
Array.filter vs Array.some vs Array.indexOf vs Array.includes
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var hasZero = []; var withoutZero = []; var comparer = (v) => v === 0; for (var i = 0; i < 10000; i++) { hasZero.push(Math.floor(Math.random() * 1000)); withoutZero.push(Math.floor(Math.random() * 1000) + 1) }
Tests:
Array.filter
var tempResult = Math.round(Math.random()) ? hasZero.filter(comparer) : withoutZero.filter(comparer);
Array.some
var tempResult = Math.round(Math.random()) ? hasZero.some(comparer) : withoutZero.some(comparer);
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.filter
Array.some
Array.indexOf
Array.includes
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/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.filter
14949.0 Ops/sec
Array.some
32861.0 Ops/sec
Array.indexOf
1420297.1 Ops/sec
Array.includes
6311482.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **What is being tested?** The benchmark tests the performance of four different ways to check if an array contains a specific value: 1. `Array.filter()`: Returns an array with all elements that pass the test implemented by the provided function. 2. `Array.some()`: Returns a boolean indicating whether at least one element in the array passes the test implemented by the provided function. 3. `Array.indexOf()`: Returns the index of the first element in the array that matches the specified value, or -1 if no elements match. 4. `Array.includes()`: Returns a boolean indicating whether an element with the specified value exists in the array. **Options compared** The benchmark compares the performance of these four methods for different test cases: * `filter()` and `some()` are compared directly in the benchmark definition, testing their execution time. * `indexOf()` is compared against `includes()`, as they serve similar purposes but have different implementation details. * The comparison between `Array.filter()` and `Array.some()` may seem counterintuitive at first. While both methods are used to test an array for a specific value, `filter()` returns the filtered array, whereas `some()` returns a boolean result. This difference might affect performance in certain scenarios. **Pros and Cons of each approach** 1. **`Array.filter()`**: * Pros: Efficient when you need to return a new array with only the desired elements. * Cons: Can be slower than other methods if you only need to check for existence, as it creates a new array. 2. **`Array.some()`**: * Pros: Fast and efficient when all you need is to know if at least one element matches. * Cons: Returns a boolean result, which might not be what you expect in every case (e.g., `some()` returns true for an empty array). 3. **`Array.indexOf()`**: * Pros: Quick and easy to use when you just need to find the index of a value. * Cons: Returns -1 if no element matches, which might be considered an error in some cases. 4. **`Array.includes()`**: * Pros: Similar to `indexOf()`, but returns a boolean result instead of an index. * Cons: Some older browsers might not support this method. **Library and its purpose** In the provided benchmark code, the `comparer` function is used as a callback in both `filter()` and `some()` methods. This function takes a value `v` and returns `true` if it matches the desired condition (i.e., `v === 0`). The purpose of this library-like structure is to decouple the comparison logic from the array testing code, making it easier to reuse or modify. **Special JS feature** There's no explicit mention of special JavaScript features in the provided benchmark code. However, some features like `const`, `let`, and `arrow functions` (used in the `comparer` function) are generally considered good practice in modern JavaScript development. Overall, this benchmark helps illustrate the performance differences between these four array testing methods, which can be useful for optimizing code or choosing the best approach for specific use cases.
Related benchmarks:
Some vs. Filter vs. indexOf vs. includes
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?