Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.some vs _.includes vs array.find vs array.some vs _.find
(version: 1)
Some vs Includes vs array.find
Comparing performance of:
some vs Includes vs Find vs lodash find vs JS Some
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function makeid(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } var a = []; for (let i=0; i<1000000; i++){ a.push(makeid(15)); }
Tests:
some
_.some(a, x=>x===a[23424]); _.some(a, x=>x===a[0]); _.some(a, x=>x===a[99999]);
Includes
_.includes(a, a[23424]); _.includes(a, a[0]); _.includes(a, a[99999]);
Find
a.find(x=>x===a[23424]) !== undefined; a.find(x=>x===a[0]) !== undefined; a.find(x=>x===a[99999]) !== undefined;
lodash find
_.find(a, x=>x===a[23424]) !== undefined; _.find(a, x=>x===a[0]) !== undefined; _.find(a, x=>x===a[99999]) !== undefined;
JS Some
a.some(x=>x===a[23424]) !== undefined; a.some(x=>x===a[0]) !== undefined; a.some(x=>x===a[99999]) !== undefined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
some
Includes
Find
lodash find
JS Some
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
some
372.0 Ops/sec
Includes
474.4 Ops/sec
Find
359.9 Ops/sec
lodash find
376.3 Ops/sec
JS Some
358.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON tests the performance of different methods for checking the presence of an element in an array in JavaScript. Specifically, it compares the following approaches: 1. **`_.some` from Lodash** - A utility library that provides additional functions for working with arrays, objects, and other data types. 2. **`_.includes` from Lodash** - This method checks if a value is present in an array. 3. **`array.find`** - A built-in JavaScript method that returns the value of the first element that satisfies the provided testing function, or `undefined` if no elements satisfy the function. 4. **`_.find` from Lodash** - Similar to `array.find`, but provides some additional functionality and benefits of using Lodash. 5. **`array.some`** - A built-in JavaScript method that tests whether at least one element in the array passes the test implemented by the provided function. ### Test Cases Breakdown: - **`_.some(a, x => x === a[23424]);`** and similar cases: - **Pros**: Flexible; it can check for any condition specified in the function. - **Cons**: It may be slower than simpler lookups since it evaluates every element until a match is found or the array is exhausted. - **`_.includes(a, a[23424]);`**: - **Pros**: Direct and easy to read; it performs a simple check for the presence of a value. - **Cons**: Less versatile compared to the function-based approaches if complex conditions are needed. - **`array.find(x => x === a[23424]);`** and **`_.find(a, x => x === a[23424]);`**: - **Pros**: Great for finding an element based on complex criteria. - **Cons**: Returns the found element or `undefined`, which could be less efficient if only checking for existence. - **`array.some(x => x === a[23424]);`**: - **Pros**: Provides an easy syntax and, like `_.some`, allows for a custom test function. - **Cons**: This method can be slower than `_.includes` because it still evaluates all elements unless a match is found early. ### Overall Performance Considerations: From the benchmark results, `_.includes` outperformed the other methods in terms of executions per second. This suggests that for simple element presence checks, Lodash's `_.includes` is likely the best choice in terms of performance. However, if you need more complex conditions to find an element, `_.some` or `array.some` with a custom function might be more appropriate. ### Alternatives: 1. **Native Operator**: You could use the `indexOf` method or the `in` operator for certain cases, but they may not always cover all needs as comprehensively as `_.includes`. 2. **Set or Map**: For large datasets or frequent searches, using a `Set` or `Map` might be a better choice due to their optimized lookup times. 3. **For Loops**: Sometimes, a basic `for` loop might provide better performance, especially in scenarios where early exits are frequent. In conclusion, the choice of which method to use depends heavily on the specific requirements of the task at hand, such as readability, performance, and the nature of the conditions being tested.
Related benchmarks:
Lodash _.some vs _.includes
Lodash _.some vs _.includes vs array.find
Lodash vs vanila 2
Lodash vs vanila 3.2
Lodash vs vanila 3.3
array find() vs js object
toLowerCase() Sorting
IndexOf vs Includes vs lodash includes vs dictionary with large array
lodash _.some vs Array.some
Comments
Confirm delete:
Do you really want to delete benchmark?