Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes() vs _.includes
(version: 1)
Comparing performance of:
Native vs Lodash
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Script Preparation code:
var arr = [ [{n: 1}, {n: 2}, {n: 3}, {n: 4}, {n: 5}], [{n: 1}, {n: 3}, {n: 5}], [{n: 1}, {n: 5}] ]; var targets = [1, 5];
Tests:
Native
arr.forEach(el => targets.includes(el));
Lodash
_.forEach(arr, function(value){ value => _.includes(targets, value.n, 0) });
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
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two methods for checking if elements within an array contain a specific value: * **Native:** Uses JavaScript's built-in `includes()` method along with a `forEach` loop to iterate through the array and check if each element contains the target values. * **Lodash:** Utilizes Lodash library's `_.forEach` and `_.includes` functions for the same task. **Here's a breakdown of the pros and cons:** **Native Approach:** * **Pros:** * No external dependencies: It relies solely on built-in JavaScript functionality, making it lightweight and straightforward to implement. * Potentially faster in some cases: For simple operations like this, native methods can be highly optimized by the browser's JavaScript engine. * **Cons:** * Can be verbose: The code might appear slightly longer compared to Lodash's more concise syntax. * Less feature-rich: It lacks some advanced functionalities that Lodash provides for array manipulation and other common tasks. **Lodash Approach:** * **Pros:** * Conciseness: Lodash often offers shorter, more readable code for common operations. * Extensive Functionality: Lodash is a comprehensive utility library with a wide range of functions for data manipulation, string operations, object handling, and more. This can be beneficial if you need to perform other tasks in addition to checking array elements. * **Cons:** * External Dependency: You need to include the Lodash library in your project, which adds some overhead. * Potential Performance Overhead: While Lodash is generally well-optimized, using external libraries can sometimes introduce a slight performance penalty compared to native methods. **Other Alternatives:** Beyond these two approaches, you could explore other JavaScript features or libraries depending on your specific needs: * **Array Methods:** Consider using other built-in array methods like `filter` or `some` for more efficient filtering or checking if elements meet certain criteria. * **Custom Looping:** If performance is critical and you have a very specific use case, you could implement your own custom looping logic to optimize the process. **Key Takeaways:** * This benchmark highlights the trade-offs between using native JavaScript methods and external libraries like Lodash for common tasks. * The best approach often depends on factors like project size, performance requirements, code readability, and the need for additional library functionalities.
Related benchmarks:
arr test
Lodash some vs includes
Array.indexOf vs Array.includes vs lodash includes with numerical values
JavaScript Benchmark: includes vs indexOf
array using indexOf vs includes vs some
Comments
Confirm delete:
Do you really want to delete benchmark?