Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.some vs _.includes vs array.find
(version: 0)
Some vs Includes vs array.find
Comparing performance of:
some vs Includes vs Find
Created:
5 years ago
by:
Registered User
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
some
Includes
Find
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
384.3 Ops/sec
Includes
485.7 Ops/sec
Find
366.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons of those approaches, and other considerations. **Benchmark Overview** The test case is designed to compare the performance of three different methods for searching an array: 1. `_.some()` (a method from the Lodash library) 2. `_.includes()` (also a method from Lodash) 3. `array.find()` (a native JavaScript method) **Comparison Methods** Let's analyze each comparison method: 1. **`_.some(a, x=>x===a[23424])`:** * This method checks if any element in the array `a` matches the condition `x === a[23424]`. The `23424` index is randomly generated for each test run. * Pros: Fast and efficient, as it only needs to check one element against the condition. Cons: May lead to poor performance if the array size is large, as it has to iterate over all elements. 2. **`_.includes(a, a[23424])`:** * This method checks if the array `a` contains an element that matches the value of `a[23424]`. * Pros: Fast and efficient, similar to `_some()`, as it uses a similar algorithm under the hood. * Cons: Similar to `_some()` in terms of performance, but may be slightly slower due to additional overhead from checking for inclusivity. 3. **`a.find(x=>x===a[23424]) !== undefined`:** * This method finds the first element in the array `a` that matches the condition `x === a[23424]`. If no match is found, it returns `undefined`. * Pros: Fast and efficient, as it stops iterating over the array once it finds a match. Cons: May be slower than `_some()` or `_.includes()`, especially for large arrays. * Note: This method is not as common as the other two in Lodash's API. **Library Used** The test case uses the Lodash library, which provides various utility functions for JavaScript. The `lodash.min.js` file is included in the HTML preparation code to enable these functions. **Special JavaScript Features/Syntax** This benchmark does not specifically use any special JavaScript features or syntax beyond standard JavaScript and Lodash's API. **Other Considerations** * **Data Generation**: The test case generates an array of 1 million random strings, each with a length of 15 characters. This helps to simulate real-world data scenarios where arrays are large and need to be searched efficiently. * **Browser/Platform**: The benchmark is run on a Chrome browser on a Mac OS X 10.15.7 platform, which may not be representative of all possible browsers or platforms. **Alternatives** If you're interested in exploring alternative methods for searching arrays, here are some options: * `Array.prototype.indexOf()` (native JavaScript method): Similar to `_.includes()`, but without the additional overhead. * `Binary search`: If the array is sorted, binary search can be an efficient approach, especially for smaller arrays. However, this requires extra sorting effort upfront. Keep in mind that the choice of method ultimately depends on the specific use case and performance requirements. If you're interested in exploring more benchmarking scenarios or modifying this one, I'd be happy to help!
Related benchmarks:
Lodash sort vs array.prototype.sort string
Lodash sort vs array.prototype.sort for objects with strings
Lodash difference vs JS filter and includes with big array
lodash _.some vs Array.some
Comments
Confirm delete:
Do you really want to delete benchmark?