Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native find vs lodash _.findIndex
(version: 0)
Comparing performance of:
_.findIndex vs my findIndex
Created:
5 years 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:
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true } ];
Tests:
_.findIndex
_.findIndex(users,val=>val.active)
my findIndex
var is_array = function (value) {return value && typeof value === 'object' && typeof value.length === 'number' && typeof value.splice === 'function' && !(value.propertyIsEnumerable('length'));}; function findIndex(array,iteratee) { if(!is_array(array)) return -1; if(typeof iteratee==='function') return array.findIndex(iteratee) // The `_.property` iteratee shorthand. if(typeof iteratee=='string') return array.findIndex(val => _.get(val,iteratee)) // The `_.matchesProperty` iteratee shorthand. return array.findIndex(val=>_.get(val,iteratee[0])===iteratee[1]) } findIndex(users,val=>val.active)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.findIndex
my findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.findIndex
66719460.0 Ops/sec
my findIndex
16163890.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark compares the performance of two approaches: using Lodash's `_.findIndex` method and creating an alternative implementation from scratch. The test cases use arrays and iterate over them to find a specific value that matches a certain condition. **Options Compared** 1. **Lodash's `_findIndex` method**: This is a built-in utility function in the Lodash library, which provides a convenient way to find the index of an element in an array that passes a test. 2. **Custom implementation**: The alternative implementation uses a more low-level approach, checking if the input is an array using a custom `is_array` function and then using the `findIndex` method on the array. **Pros and Cons** * **Lodash's `_findIndex` method**: + Pros: - Convenient and concise way to find an index in an array. - Optimized for performance. + Cons: - Adds external dependencies (Lodash library). - May not be suitable for all use cases or performance-critical applications. * **Custom implementation**: + Pros: - No external dependencies. - Can be optimized further for specific use cases or performance requirements. + Cons: - More verbose and less concise than Lodash's `_findIndex` method. - Requires manual error handling and edge case considerations. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers, including array methods like `_.findIndex`. It helps simplify code by providing pre-built functions for common tasks, making it easier to write more efficient and readable code. In this benchmark, Lodash's `_findIndex` method is used as a reference implementation. **Special JavaScript Feature/Syntax** There are no special JavaScript features or syntax used in these test cases beyond standard ECMAScript syntax. **Other Alternatives** If you need to find an index in an array without using Lodash, there are other approaches: 1. **Using the `indexOf` method**: This is a built-in method that returns the index of the first occurrence of a value in an array. 2. **Using a for loop**: You can use a traditional for loop to iterate over the array and find the index of the desired element. 3. **Using `Array.prototype.findIndex` with a custom implementation**: Similar to the custom implementation used in the benchmark, but without the `is_array` function. Each approach has its trade-offs, and the choice ultimately depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash find vs native find by id
Array find vs lodash _.find
native find vs lodash _.find equal
native find vs lodash _.find for objects equality
Comments
Confirm delete:
Do you really want to delete benchmark?