<!--your preparation HTML code goes here-->
class Utility {
static allowedValues = [true, false, 'true', 'false', 1, 0, '1', '0'];
static isBooleanInline(value) {
return value === true || value === false || value === 'true' || value === 'false' || value === 1 || value === 0 || value === '1' || value === '0';
}
static isBooleanPredefined(value) {
return this.allowedValues.includes(value);
}
}
let value = 2;
const valid = Utility.isBooleanPredefined(++value);
const valid = Utility.isBooleanInline(++value);
const valid = Utility.isBooleanPredefined('a');
const valid = Utility.isBooleanInline('a');
const valid = Utility.isBooleanPredefined(++value % 2);
const valid = Utility.isBooleanInline(++value % 2);
const valid = Utility.isBooleanPredefined(1);
const valid = Utility.isBooleanInline(1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
use predefined (no cache, false) | |
use inline (no cache, false) | |
use predefined (cache, false) | |
use inline (cache, false) | |
use predefined (no cache, true) | |
use inline (no cache, true) | |
use predefined (cache, true) | |
use inline (cache, true) |
Test name | Executions per second |
---|---|
use predefined (no cache, false) | 15162271.0 Ops/sec |
use inline (no cache, false) | 16021370.0 Ops/sec |
use predefined (cache, false) | 67420488.0 Ops/sec |
use inline (cache, false) | 155750864.0 Ops/sec |
use predefined (no cache, true) | 15354675.0 Ops/sec |
use inline (no cache, true) | 16041618.0 Ops/sec |
use predefined (cache, true) | 97784024.0 Ops/sec |
use inline (cache, true) | 181689488.0 Ops/sec |
The benchmark provided in the JSON data evaluates two different approaches to checking if a value is a valid boolean representation using JavaScript functions: isBooleanInline()
and isBooleanPredefined()
. The performance of these functions is analyzed based on how they handle various inputs, their caching behavior, and general execution speed.
Inline Boolean Check (isBooleanInline
):
const isBooleanInline = function(value) {
return value === true || value === false || value === 'true' || value === 'false' || value === 0 || value === 1 || value === '1' || value === '0';
}
Predefined Boolean Check (isBooleanPredefined
):
const isBooleanPredefined = function(value) {
return allowedValues.includes(value);
}
allowedValues
) and checks if the input value is included in that array.allowedValues
array.Array.includes()
may introduce overhead compared to a series of direct comparisons since it effectively has to iterate over the array.The benchmark runs several test cases, each comparing the performance of the two different functions under various conditions including caching and different input values:
No Cache Tests:
++value
).'a'
) is also tested, showcasing how each method handles unexpected inputs.Cache Tests:
1
), where previous evaluations may influence performance.Based on the latest benchmark results, the performance in terms of executions per second for various test cases is recorded. Key observations:
isBooleanInline
consistently outperforms isBooleanPredefined
, especially with caching enabled.While both methods provide a way to determine valid boolean values, the choice between isBooleanInline
and isBooleanPredefined
depends on the context of use. The inline method provides speed and may suit performance-critical applications, especially when predictable inputs are used. Meanwhile, the predefined method favors maintainability and clarity, making it suitable for applications where the set of accepted values may need adjustment over time.
Other alternatives to consider might include:
typeof
operator alongside type-checking functions could provide another approach to validating inputs, though it may require more extensive implementations.lodash
offer robust utilities for deep checks and validations, though integrating such dependencies should be weighed against the needs of the project.This benchmark serves as a reminder to consider both performance and maintainability when implementing validation logic in software applications.