Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some vs. Filter vs. findIndex again
(version: 1)
Comparing performance of:
some test vs filter test vs findIndex test
Created:
7 years ago
by:
Registered User
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:
some test
var tempResult = !!Math.round(Math.random()) ? hasZero.some(v => v === 0) : withoutZero.some(v => v === 0);
filter test
var tempResult = !!Math.round(Math.random()) ? hasZero.filter(v => v === 0) : withoutZero.filter(v => v === 0);
findIndex test
var tempResult = !!Math.round(Math.random()) ? hasZero.findIndex((i) => i == 0) > -1 : withoutZero.findIndex((i) => i == 0) > -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
some test
filter test
findIndex test
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
some test
359965.7 Ops/sec
filter test
54321.6 Ops/sec
findIndex test
322184.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and what are the pros and cons of different approaches. **Benchmark Definition** The benchmark is comparing three ways to check if an array contains at least one zero value: using `some()`, `filter()`, and `findIndex()`. **Test Case 1: `some`** * The script preparation code creates two arrays, `hasZero` and `withoutZero`, with 10,000 elements each. `hasZero` has a mix of zeros and non-zeros, while `withoutZero` only has non-zero values. * The benchmark definition uses the `some()` method to check if at least one element in either array is equal to zero. **Pros:** * Simple and concise syntax * Fast iteration over arrays **Cons:** * May return false positives if an empty array is passed as an argument * Less intuitive than other methods for this specific use case **Test Case 2: `filter()`** * The script preparation code remains the same as above. * The benchmark definition uses the `filter()` method to create a new array with only the elements that are equal to zero. **Pros:** * More explicit and controlled iteration over arrays * Can be more efficient than `some()` for large arrays **Cons:** * Creates a new array, which can be memory-intensive * May be slower than `some()` due to the overhead of creating a new array **Test Case 3: `findIndex()` followed by a conditional check** * The script preparation code remains the same as above. * The benchmark definition uses `findIndex()` to find the index of the first zero element in either array. If the index is greater than -1, it returns true; otherwise, it returns false. **Pros:** * More explicit and controlled iteration over arrays * Can be more efficient than `some()` for large arrays **Cons:** * May be slower than `filter()` due to the overhead of finding an index * Requires a conditional check to return true or false **Libraries used** None are explicitly mentioned in the benchmark definition, but it's likely that the `Array.prototype.some()`, `Array.prototype.filter()`, and `Array.prototype.findIndex()` methods are being tested. **Special JavaScript features or syntax** The use of template literals (`"var tempResult = !!Math.round(Math.random()) ? hasZero.some(v => v === 0) : withoutZero.some(v => v === 0);"`), arrow functions (`(i) => i == 0`), and the `??` operator (implied by the ternary operator, but not explicitly used in this example) are modern JavaScript features. **Other alternatives** There may be other ways to implement these checks, such as using `indexOf()` or `includes()`, but `some()`, `filter()`, and `findIndex()` are commonly used and efficient methods for this specific use case. As a side note, it's worth mentioning that the benchmark results indicate that `findIndex()` is the fastest method, followed by `some()`, and then `filter()`. This may be due to the optimized implementation of these methods in modern browsers.
Related benchmarks:
Some vs. Filter vs. findIndex
Some vs. Filter vs. findIndex vs find
Some vs. Filter vs. findIndex with object
benchmark filter vs findIndex
Comments
Confirm delete:
Do you really want to delete benchmark?