Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Boolean cast vs !! test
(version: 1)
Comparing performance of:
Boolean(val).valueOf() vs Double Negation (!!)
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var test = true;
Tests:
Boolean(val).valueOf()
Boolean(test).valueOf()
Double Negation (!!)
!!test
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Boolean(val).valueOf()
Double Negation (!!)
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Boolean(val).valueOf()
185899168.0 Ops/sec
Double Negation (!!)
232901216.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark, two methods of converting a value to a boolean in JavaScript are tested: 1. **Boolean(test).valueOf()** 2. **Double Negation (!!)** ### Description of the Approaches: #### 1. Boolean(test).valueOf() - **What it does**: This approach first creates a Boolean object from the `test` variable (which is `true` in this case) and then calls the `.valueOf()` method on that object. The `.valueOf()` method returns the primitive value of the Boolean object (i.e., `true`). - **Pros**: - It is explicit. Someone reading the code can understand that you are converting to a boolean. - It may be useful in contexts where you are packaging a primitive value in an object. - **Cons**: - This method is less efficient due to the overhead of creating a Boolean object. Object creation has a performance cost, especially in tight loops or performance-sensitive applications. - While it provides clarity, it does so at the expense of performance for simple boolean checks. #### 2. Double Negation (!!) - **What it does**: The double negation (`!!`) takes the truthy or falsy value of a variable and converts it to a Boolean primitive. The first negation (`!`) converts the value to a boolean primitive and then negates it, while the second negation reverses it back to the expected boolean value. - **Pros**: - It is concise and performs efficiently since it doesn’t involve any object creation. - It is a common idiom in JavaScript, making it recognizable to many developers. - **Cons**: - Some may argue that it is less readable for those not familiar with the idiom, which can lead to misunderstandings, especially among developers new to JavaScript. - It requires an understanding of truthiness and falsiness in JavaScript, which may not be as intuitive for all developers. ### Benchmark Results: The latest benchmark results indicate the performance of both methods when executed in a specific environment (Chrome 134 on a Windows desktop): - **Double Negation (!!)**: Achieved **256,666,064 executions per second**. - **Boolean(val).valueOf()**: Achieved **189,478,016 executions per second**. This shows that the double negation method is significantly faster than the `Boolean(val).valueOf()` method, reinforcing the preference for `!!` in performance-sensitive scenarios. ### Alternative Approaches: In addition to these two methods, there are a few other approaches to convert values to a boolean in JavaScript, including: - **Using the `Boolean` function directly**: Instead of chaining with `.valueOf()`, you could simply use `Boolean(test)`, which is also efficient and clear. However, it does not create an object, nor does it offer the clarity of converting to a Boolean object. - **Using conditionals**: You could use an if statement or a ternary operator to evaluate the value, though this is not typically how conversions to booleans are done within the context of performance testing. ### Conclusion: When evaluating these two methods, the choice often comes down to the balance between clarity and performance. While `Boolean(val).valueOf()` may be clearer to some developers, the `!!` operator provides superior performance and is widely recognized in the JavaScript community. As always, the best choice depends on the specific context of use, the team's familiarity with the code, and the performance requirements of the application.
Related benchmarks:
Setting boolean
Negation vs normal boolean value
testtest12345232
constructor vs double negation
Boolean conversion - javascript
double-bang-vs-boolean-cast
double-bang-vs-boolean-cast2
test !! && Boolean()
Boolean vs !! vs Cast type
Comments
Confirm delete:
Do you really want to delete benchmark?