Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
double bang vs typeof check vs truthy
(version: 0)
Comparing performance of:
double bang vs typeof vs truthy
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
double bang
const obj = Object.assign( function() {}, { x: 1 }, ); for (let i = 0; i < 100000; i++) { if (!!obj) {} }
typeof
const obj = Object.assign( function() {}, { x: 1 }, ); for (let i = 0; i < 100000; i++) { if(typeof obj === 'object' || typeof obj === 'function') {} }
truthy
const obj = Object.assign( function() {}, { x: 1 }, ); for (let i = 0; i < 100000; i++) { if (obj) {} }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
double bang
typeof
truthy
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 definition and test cases. **Benchmark Definition:** The benchmark is comparing three different approaches to check if an object exists: 1. **Double Bang (`!!`)**: This method uses the double exclamation mark operator to force JavaScript to evaluate the expression inside the parentheses as a boolean value. 2. **Type Check (`typeof obj === 'object' || typeof obj === 'function'`)**: This method checks the type of `obj` using the `typeof` operator and then performs an OR operation with another type check. 3. **Truthy (`if (obj)`)**: This method uses a simple truthy value check, where if `obj` is truthy, it's considered as true. **Pros and Cons of each approach:** 1. **Double Bang (`!!``):** * Pros: + Simple to read and write. + Fast, as it only evaluates the expression once. * Cons: + Can be slower due to unnecessary computations. 2. **Type Check (`typeof obj === 'object' || typeof obj === 'function'`):** * Pros: + More accurate than `!!`, as it checks specific types instead of relying on boolean conversion. * Cons: + May be slower due to type checking and OR operation. 3. **Truthy (`if (obj)`)**:** * Pros: + Fast, as it only evaluates the object existence once. * Cons: + Less readable than `!!` or Type Check, as its performance is dependent on JavaScript's truthy value rules. **Library and purpose:** None of these test cases rely on a specific library. However, they do utilize built-in JavaScript features and operators. **Special JS feature or syntax:** * The use of double exclamation marks (`!!`) relies on the `Boolean()` function in older versions of JavaScript (before ECMAScript 5). In newer versions, this behavior is still supported, but it's not the recommended way to force boolean evaluation. * The `typeof` operator with type checks (`typeof obj === 'object' || typeof obj === 'function'`) relies on the TypeScript compiler or JSDoc annotations for proper type checking. In plain JavaScript, you would use a simple truthy value check like the third test case. **Other alternatives:** If you need to write more complex checks or if you want to avoid relying on `!!` or `typeof`, consider using: * **Nullish Coalescing (`??`)**: A relatively new operator (introduced in ECMAScript 2020) that can be used for more expressive and efficient null/undefined checks. * **Optional Chaining (`?.`) and Property Access (`..`)**: These operators can help simplify property access and existence checks. Keep in mind that `!!`, Type Check, and Truthy approaches have their own trade-offs in terms of readability, performance, and code maintainability.
Related benchmarks:
Check function. typeof vs constructor + null check
Strict equality VS typeof
Check function. typeof vs constructor + null check II
Check object. typeof vs constructor + null check
Ternary operator vs String Interpolation
Comments
Confirm delete:
Do you really want to delete benchmark?