Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Boolean constructor vs double negotiation trick in javascript ewsd
(version: 0)
Comparing performance of:
Boolean vs Double negation trick
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var bFalse = false; var sEmpty = ''; var sFilled = 'Foo'; var nZero = 0; var nOne = 1;
Tests:
Boolean
for(i=0;i<10000;i++){ Boolean(bFalse) Boolean(sEmpty) Boolean(sFilled) Boolean(nZero) Boolean(nOne) }
Double negation trick
for(i=0;i<10000;i++){ !!bFalse !!sEmpty !!sFilled !!nZero !!nOne }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Boolean
Double negation trick
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Boolean
3135.8 Ops/sec
Double negation trick
3403.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking setup and explain what's being tested. **Benchmark Overview** The test case measures the performance of two different approaches in JavaScript: using the `Boolean` constructor and the double negation trick (`!!`). Both methods are used to evaluate a set of variables (`bFalse`, `sEmpty`, `sFilled`, `nZero`, and `nOne`) as booleans. **Options Compared** There are two main options being compared: 1. **Using the `Boolean` constructor**: This involves creating an instance of the `Boolean` class for each variable, like this: `new Boolean(bFalse)`. The constructor returns a new object with a `valueOf()` method that returns the boolean value. 2. **Double negation trick (`!!`)**: This approach uses the double bang operator (`!!`) to force the evaluation of a value as a boolean. For example, `!!bFalse` evaluates `bFalse` as true or false. **Pros and Cons** 1. **Using the `Boolean` constructor**: * Pros: explicit, clear intention (creating a new Boolean object), potentially more readable code. * Cons: overhead of creating an object instance, slower due to object creation. 2. **Double negation trick (`!!`)**: * Pros: concise, faster (due to avoiding object creation). * Cons: might be less readable, can lead to unexpected behavior if not used carefully. **Library/Dependency** There is no explicit library or dependency mentioned in the benchmark definition. However, it's worth noting that some modern JavaScript engines and browsers have optimized `Boolean` constructor usage, making it faster than using the double negation trick. **Special JS Features/Syntax** The test case uses the double bang operator (`!!`) to force evaluation as a boolean. This is a standard JavaScript feature introduced in ECMAScript 2011 (ES6). It's used here to evaluate the variables without explicitly creating a `Boolean` object. **Other Considerations** * **Readability**: The `Boolean` constructor approach might be more readable, especially for developers familiar with it. The double negation trick can make code less readable if not used correctly. * **Caching and Optimization**: Modern browsers often cache the results of previous evaluations using the same variables, so this test case might not accurately reflect real-world performance differences. * **Browser variations**: Different browsers may optimize or implement these features differently, affecting benchmark results. **Alternatives** If you're interested in exploring alternative approaches, consider testing: 1. Using `Boolean` constructor alternatives, such as `true` and `false`. 2. Other boolean evaluation methods, like using `Number()` with a value of 0. 3. Non-standard or experimental features, if available. Keep in mind that the performance differences between these approaches might be negligible in most cases, and benchmarking should focus on identifying performance issues rather than minor optimizations.
Related benchmarks:
Boolean constructor vs double negotiation trick in javascript
Boolean constructor vs double negotiation trick in javascript 2
Boolean constructor vs double negotiation trick in javascript 3
Boolean constructor vs double negotiation trick in javascript but better
Comments
Confirm delete:
Do you really want to delete benchmark?