Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js boolean double negative isSet
(version: 1)
Comparing performance of:
!! vs Boolean vs isSet
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
!!
let a = !!null; let b = !!{};
Boolean
let a = Boolean(null); let b = Boolean({});
isSet
let a = (null === null || null === undefined || isNaN(null)); let b = ({} === null || {} === undefined || isNaN({}));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
!!
Boolean
isSet
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
!!
199480624.0 Ops/sec
Boolean
165555248.0 Ops/sec
isSet
21908934.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests various methods of evaluating the truthiness of values pertaining to `null` and an empty object in JavaScript. The specific approaches compared are: 1. **Double Negation (`!!`)**: - **Test Case**: ```javascript let a = !!null; // This will result in false let b = !!{}; // This will result in true ``` - **Pros**: - The double negation is a very common idiom in JavaScript for converting values to their boolean equivalent. It is concise and efficient. - **Cons**: - None particularly, but readability may be impacted for those unfamiliar with the idiom. 2. **Boolean Constructor**: - **Test Case**: ```javascript let a = Boolean(null); // This will result in false let b = Boolean({}); // This will result in true ``` - **Pros**: - Using the `Boolean` constructor explicitly expresses the intention to convert a value to a boolean, which can be clearer to some developers. - **Cons**: - It is generally a bit slower than the double negation due to the additional overhead of function calls and may not be as concise. 3. **Explicit Comparison and Logical Operations**: - **Test Case**: ```javascript let a = (null === null || null === undefined || isNaN(null)); // Evaluates to true since null === null is true let b = ({} === null || {} === undefined || isNaN({})); // Evaluates to false ``` - **Pros**: - This method explicitly checks for types and conditions, which can potentially cater to more complex scenarios. - **Cons**: - This approach is significantly more verbose and complex. From a performance perspective, it is also the slowest among the options tested, as it involves multiple equality checks and a function call (`isNaN`). ### Benchmark Results and Considerations The benchmark results show the number of executions per second for each test: - The double negation (`!!`) achieves the highest performance with **199,480,624.0 executions per second**. - The `Boolean` constructor comes next with **165,555,248.0 executions per second**. - The explicit comparison method (`isSet`) performs the worst at **21,908,934.0 executions per second**. ### Other Considerations - **Performance**: When choosing between these methods, if performance is a critical factor (e.g., in tight loops), the double negation operator is the clear winner. - **Readability**: Depending on the audience, the explicit `Boolean` constructor might be preferred for better code clarity. - **Complex Logic**: The explicit comparisons might be warranted in situations where you need to check for multiple conditions or when clarity involving NaN or undefined values is crucial. ### Alternatives - Using other utility libraries (like Lodash's `_.isNil`) could provide even more robust type-checking methods, though these come with their own performance implications due to additional function calls and require loading the library itself. - If you want to handle more complex assertions, writing custom utility functions may help, especially when clarity or specific edge cases are required. - In situations where performance and readability conflict, careful consideration is needed to balance these factors based on the specific requirements and context of the code.
Related benchmarks:
`a += b` vs `a = a + b`
typeof boolean vs === true || === false
js boolean conversion
Boolean conversion - javascript
delete vs null vs undefined vs void 0 vs Object.create(null) propertie
Nullish coalescing V.S. if/else (native)
systematic vs conditional assignements
systematic vs conditional assignements with explicit condition
JS equality vs strict equality
Comments
Confirm delete:
Do you really want to delete benchmark?