Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js boolean conversion
(version: 0)
Comparing performance of:
!! vs Boolean vs Comparison
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
!!
let a = !!12; let b = !!'string';
Boolean
let a = Boolean(12); let b = Boolean('string');
Comparison
let a = (12 == true); let b = ('string' == true);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
!!
Boolean
Comparison
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
!!
194155328.0 Ops/sec
Boolean
152396816.0 Ops/sec
Comparison
43507520.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided benchmark. **Overview** The benchmark measures the performance of three different ways to convert values to booleans (true or false) in JavaScript: 1. Using the double bang `!!` operator 2. Using the built-in `Boolean()` function 3. Using equality comparisons (`== true`) **Options Compared** Here's a brief overview of each option being compared: * **!!**: The double bang operator is a shorthand way to convert a value to a boolean. It works by checking if the value is truthy (i.e., not zero, null, undefined, or an empty string) and then casting it to a boolean. * **Boolean()**: This is a built-in JavaScript function that takes a single argument and returns its boolean equivalent. * **Comparison**: This option uses equality comparisons (`== true`) to convert values to booleans. It's not as straightforward as the other two options, but it works by checking if the value is equal to `true`. **Pros and Cons** Here are some pros and cons of each approach: * **!!**: + Pros: Short and concise, easy to read. + Cons: May be slower than using `Boolean()` because it involves an additional operation (the bang operator). * **Boolean()**: + Pros: Fast, straightforward, and easy to understand. + Cons: Requires a function call, which may incur overhead. * **Comparison**: + Pros: Works with any value that can be compared using `==`, including non-boolean values. + Cons: Less readable and maintainable than the other two options. **Library Usage** None of the benchmark tests use external libraries. **Special JS Features or Syntax** None of the benchmark tests use special JavaScript features or syntax, such as async/await, generators, or decorators. **Other Alternatives** If you're interested in exploring alternative ways to convert values to booleans, here are a few examples: * Using `Number()` and checking if the result is zero: `let a = Number(12); let b = Number('string');` * Using a regex pattern: `let a = /^[\d]+$/g.test('12'); let b = /^[\d]+$/.test('string');` Keep in mind that these alternatives may not be as readable or maintainable as the original options. I hope this explanation helps!
Related benchmarks:
Boolean conversion - javascript
boolean vs math
Ternary operator vs String Interpolation
double-bang-vs-boolean-vs-unfedined-cast
Comments
Confirm delete:
Do you really want to delete benchmark?