Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.find vs _.find
(version: 0)
Comparing performance of:
native vs lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
Script Preparation code:
var templates = [ { name: 'a', b: 123, c: 234, d: 567 }, { name: 'b', b: 123, c: 234, d: 567 }, { name: 'c', b: 123, c: 234, d: 567 }, { name: 'd', b: 123, c: 234, d: 567 }, { name: 'e', b: 123, c: 234, d: 567 }, { name: 'f', b: 123, c: 234, d: 567 }, { name: 'g', b: 123, c: 234, d: 567 }, { name: 'h', b: 123, c: 234, d: 567 }, ] var nameToFind = 'e'
Tests:
native
templates.find(t => t.name === nameToFind)
lodash
_.find(templates, {name: nameToFind})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native
lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition:** The benchmark is comparing two approaches to find an element in an array: 1. **Native JavaScript**: `templates.find(t => t.name === nameToFind)` 2. **Lodash (JavaScript library)**: `_find(templates, {name: nameToFind})` Both approaches aim to find the first element in the `templates` array that matches the condition specified in the `nameToFind` variable. **Native JavaScript Approach:** * This approach uses the built-in `Array.prototype.find()` method, which returns the first element in the array that satisfies the provided callback function. * The callback function takes an element from the array as its argument and returns a boolean value indicating whether it matches the condition. * In this case, the callback function simply checks if the element's `name` property is equal to `nameToFind`. * Pros: + Built-in method with no additional dependencies. + Compact and expressive code. * Cons: + May be slower due to overhead of JavaScript engine and interpreter. + Limited optimization opportunities. **Lodash Approach:** * This approach uses the Lodash library, which provides a function called `_find()` that finds the first element in an array that satisfies a condition. * The condition is specified as an object with a `name` property that matches the value of `nameToFind`. * Pros: + Provides a consistent and standardized way to perform similar operations across different libraries and frameworks. + Optimized for performance and reliability. * Cons: + Requires loading the Lodash library, which may introduce additional overhead. + May not be as compact or expressive as native JavaScript code. **DevicePlatform:** `Desktop` This indicates that the benchmark was run on a desktop device (i.e., not on mobile or tablet devices). **OperatingSystem:** `Linux` This indicates that the benchmark was run on a Linux-based operating system. **ExecutionsPerSecond:** These values represent the average number of executions per second for each test case. A higher value typically indicates faster performance. * **Native JavaScript**: 1,908,761.5 executions/second * **Lodash**: 1,046,668.375 executions/second Based on these results, the native JavaScript approach appears to be slightly faster than the Lodash approach. **Other Considerations:** * The benchmark is designed to measure performance in a specific context (finding an element in an array) and may not generalize well to other use cases. * The results are sensitive to the specific implementation details of each library (e.g., how JavaScript engines handle array operations). * Other libraries or approaches might be faster or more suitable for certain scenarios, so this benchmark should be taken as a starting point rather than a definitive answer. **Alternatives:** Other alternatives to Lodash for finding elements in arrays include: 1. **Array.prototype.some()**: Similar to `find()`, but returns true if any element matches the condition. 2. **Array.prototype.filter()`: Returns an array of all elements that match the condition, rather than just the first one. 3. **Vanilla JavaScript approaches**: Implement custom loops or recursive functions to find elements in arrays. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to Lodash or native JavaScript.
Related benchmarks:
native find vs lodash _.find es6
lodash vs es6 in find method
Array find vs lodash _.find
Array.prototype.find vs Lodash find object
Lodash test - find
Comments
Confirm delete:
Do you really want to delete benchmark?