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:
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;
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:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/136.0.7103.91 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 136 on iOS 18.4.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.some
275395.5 Ops/sec
Array.filter
106123.2 Ops/sec
Array.indexOf
56694796.0 Ops/sec
Array.includes
279069.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Overview** The benchmark consists of four test cases that compare the performance of different methods to filter or search for a specific value in an array: `Array.some`, `Array.filter`, `Array.indexOf`, and `Array.includes`. **Test Cases** 1. **`Array.some`**: This method returns `true` if at least one element in the array satisfies the provided condition. 2. **`Array.filter`**: This method creates a new array with all elements that satisfy the provided condition. 3. **`Array.indexOf`**: This method returns the index of the first element in the array that matches the specified value, or -1 if no such element exists. 4. **`Array.includes`**: This method checks if an element is present in the array and returns `true` if it does. **Comparison** The benchmark compares the performance of each of these methods on two arrays: `hasZero` (contains 10,000 random integers between 0 and 999) and `withoutZero` (contains 10,000 random integers between 0 and 1000). **Options Compared** * `Array.some` + **Pros**: Simple to implement, flexible, can be used for filtering or searching. + **Cons**: May have poor performance due to the use of `for...in` loops in older browsers, may not work as expected with large arrays. * `Array.filter` + **Pros**: Creates a new array, preserving the original array's integrity, less memory-intensive than `some`. + **Cons**: More complex implementation, can be slower due to the creation of a new array. * `Array.indexOf` + **Pros**: Fast and efficient for finding a single element in an array, often used as a fallback when other methods are too slow. + **Cons**: Returns the index of the first match, may not work correctly if there's no match, can be slower than `some` or `filter`. * `Array.includes` + **Pros**: Simple and intuitive implementation, fast and efficient for searching an array. + **Cons**: May have poor performance in older browsers due to the use of `for...in` loops, may not work correctly with large arrays. **Library Used** None of these methods rely on a specific library. However, it's worth noting that some implementations might use internal libraries or optimizations that are not exposed in the standard specification. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is written using standard JavaScript and does not utilize any modern features like async/await, promises, or destructuring. **Alternatives** Other methods that could be used for filtering or searching arrays include: * `Array.prototype.forEach`: This method executes the provided callback function once for each element in the array. * Custom loops: Writing a custom loop to iterate over the array and check for elements can also be an option. * Other libraries or frameworks: Depending on the specific requirements, other libraries like Lodash or Underscore.js might provide more efficient or convenient methods for filtering or searching arrays. Keep in mind that this benchmark focuses specifically on comparing the performance of `Array.some`, `Array.filter`, `Array.indexOf`, and `Array.includes`.
Related benchmarks:
Some vs. Filter vs. indexOf 1
Some vs. Filter vs. indexOf vs every
Some vs. Filter vs. indexOf v includes
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
Comments
Confirm delete:
Do you really want to delete benchmark?