Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Single includes vs _.has inside for-statement
(version: 0)
Comparing performance of:
Includes vs has
Created:
2 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 booleanOperators = ['none', 'or', 'and']; var complexVisibilityDependsOn = {'and': ['rule1', 'rule2']};
Tests:
Includes
for (const operator in complexVisibilityDependsOn) { if (_.includes(booleanOperators, operator)) { return operator; } }
has
for (const operator in booleanOperators) { if (_.has(complexVisibilityDependsOn, operator)) { return operator; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Includes
4881776.0 Ops/sec
has
1730164.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. The benchmark is testing two different approaches to check if an object has a certain property: 1. **Includes**: This approach uses the `_.includes()` function from the Lodash library to check if an array contains a specific element. 2. **Has**: This approach uses the `_._has()` function from the same Lodash library to directly check if an object has a specific property. The test cases are prepared by defining two variables: * `booleanOperators`: an array containing strings that represent boolean operators (e.g., "none", "or", "and"). * `complexVisibilityDependsOn`: an object with nested structure, where the keys are strings and the values are arrays of strings. In this case, it's defined as `{ 'and': ['rule1', 'rule2'] }`. The test preparation code is as follows: ```javascript var booleanOperators = ['none', 'or', 'and']; var complexVisibilityDependsOn = {'and': ['rule1', 'rule2']}; ``` This setup creates a scenario where the test cases will attempt to access the `complexVisibilityDependsOn` object and check if certain properties exist using both `_.includes()` and `_._has()`. **Options comparison:** * **_.includes()**: This method is suitable for arrays, checking if an element exists within a specific array. However, in this case, it's being used to check if a property exists within an object. + Pros: - Can be more efficient than iterating over the entire object. + Cons: - May not be as straightforward or intuitive for some developers. - Requires Lodash library, which might add overhead. * **_._has()**: This method is specifically designed to check if an object has a certain property. + Pros: - More straightforward and intuitive than using _.includes(). - Optimized for this specific use case. + Cons: - Requires Lodash library, which might add overhead. **Library usage:** In both test cases, the Lodash library is used to provide the `_.includes()` function. The `_._has()` function is also part of the Lodash library, but it's not being used in this specific benchmark. **Special JS feature or syntax:** There are no special features or syntax being tested in this benchmark. Both test cases use standard JavaScript and Lodash functions. **Other alternatives:** If you didn't want to use Lodash, you could implement the checks using vanilla JavaScript: * To check if an array contains a specific element: `Array.prototype.includes()` * To check if an object has a certain property: Using `in` operator (e.g., `if ('rule1' in complexVisibilityDependsOn) { ... }`) Keep in mind that these alternatives might be less efficient or more error-prone than using Lodash functions. The benchmark is designed to compare the performance of two approaches to check if an object has a certain property. By comparing the execution times and raw UA strings, users can see which approach is faster on their specific system configuration.
Related benchmarks:
lodash has vs hasOwnPropertie
lodash/isEmpty vs NOT operator
array using every vs includes vs some
lodash isNil vs native isNil with if
Comments
Confirm delete:
Do you really want to delete benchmark?