Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs direct assignment
(version: 0)
compares perf between array includes vs direct assignment for multiple or clause
Comparing performance of:
Includes vs direct assignment with Or
Created:
4 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:
const isPaymentMethodUpdated = false; const isPaymentDataUpdated = false; const isPriceDataUpdated = false; const isCreditCardUpdated = false; const hasPricing = false; const isOmisePhoneStateUpdated = false; const changeCreditCardToggled = false; const isUserDataUpdated = false; const hasPricingAndUpdatedPrice = isPriceDataUpdated && hasPricing
Tests:
Includes
const isPaymentMethodUpdated = false; const isPaymentDataUpdated = false; const isPriceDataUpdated = false; const isCreditCardUpdated = false; const hasPricing = false; const isOmisePhoneStateUpdated = false; const changeCreditCardToggled = false; const isUserDataUpdated = false; const hasPricingAndUpdatedPrice = isPriceDataUpdated && hasPricing const shouldUpdateBookingButton = [ hasPricingAndUpdatedPrice, isCreditCardUpdated, isPaymentDataUpdated, isOmisePhoneStateUpdated, changeCreditCardToggled, isUserDataUpdated ].includes((condition) => condition === true)
direct assignment with Or
const isPaymentMethodUpdated = false; const isPaymentDataUpdated = false; const isPriceDataUpdated = false; const isCreditCardUpdated = false; const hasPricing = false; const isOmisePhoneStateUpdated = false; const changeCreditCardToggled = false; const isUserDataUpdated = false; const hasPricingAndUpdatedPrice = isPriceDataUpdated && hasPricing const shouldUpdateBookingButton = hasPricingAndUpdatedPrice || isPaymentMethodUpdated || isCreditCardUpdated || isPaymentDataUpdated || isOmisePhoneStateUpdated || changeCreditCardToggled || isUserDataUpdated;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Includes
direct assignment with Or
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 benchmark and its test cases. **What is being tested?** The benchmark compares the performance of two approaches for multiple clause conditions: 1. Using the `includes()` method on an array, specifically with a callback function as the condition. 2. Direct assignment using logical OR operators (`||`) between multiple variables. **Options compared:** * **Includes**: This approach uses the `includes()` method on an array to check if at least one of the conditions is true. The array contains multiple conditions, and each condition is passed as a separate argument to the callback function. * **Direct assignment with Or**: This approach directly assigns values to variables using logical OR operators (`||`) between multiple conditions. **Pros and Cons:** * **Includes**: + Pros: - Clearer code structure, especially for more complex conditions - Less prone to errors due to the callback function's type checking + Cons: - Potential performance overhead due to the use of `includes()` - May be slower than direct assignment for simpler conditions * **Direct assignment with Or**: + Pros: - Can be faster for simpler conditions - Less memory allocation and garbage collection due to no array creation + Cons: - Code can become cluttered and harder to read, especially for complex conditions - More prone to errors due to the lack of type checking **Library used:** * **Lodash**: The `lodash` library is included in the benchmark using the `Html Preparation Code`. Specifically, it provides the `includes()` method, which is used in one of the test cases. **Special JS feature or syntax:** The benchmark does not explicitly use any special JavaScript features or syntax. However, it does rely on the `||` operator for logical OR assignments, which might be a common practice but not a built-in feature. It also uses the `const` keyword for variable declarations, which is a modern JavaScript feature. **Alternative approaches:** Other alternatives to consider: * **Using a switch statement**: Instead of using an array with conditions, you could use a switch statement with multiple cases. This approach can be more efficient and readable but might not work well with complex condition hierarchies. * **Using a custom function**: You could define a custom function that takes the values as arguments and returns true if any of them are true. This approach would eliminate the need for `includes()` or logical OR operators. Keep in mind that these alternatives may have different trade-offs in terms of performance, code readability, and maintainability.
Related benchmarks:
Array.indexOf vs Array.includes vs lodash includes with numerical values
Number array indexOf vs includes vs some
array using indexOf vs includes vs some
array using every vs includes vs some
array.indexOf vs array.includes vs array.some vs equality
Comments
Confirm delete:
Do you really want to delete benchmark?