Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof boolean vs === true || === false
(version: 0)
Comparing performance of:
typeof value === "boolean" vs value === true || value === false
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = Array.from({length: 10000}, (_, i) => i % 5 === 0 ? i % 3 === 0 : i % 2 ? i.toString() : i);
Tests:
typeof value === "boolean"
let bools = 0; for (let i = 0; i < values.length; i++) { const value = values[i]; if (typeof value === "boolean") { bools++; } }
value === true || value === false
let bools = 0; for (let i = 0; i < values.length; i++) { const value = values[i]; if (value === true || value === false) { bools++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
typeof value === "boolean"
value === true || value === false
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof value === "boolean"
44538.9 Ops/sec
value === true || value === false
26335.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark provided by MeasureThat.net. **Benchmark Definition** The benchmark measures the performance difference between two approaches to check if a value is a boolean: 1. `typeof value === "boolean"`: This method checks the type of the value using the `typeof` operator. 2. `value === true || value === false`: This approach uses a simple conditional statement to check if the value is either `true` or `false`. **Options compared** The benchmark compares the performance of these two approaches, which can be thought of as: * **Type checking**: Using `typeof` to check the type of a value. * **Value comparison**: Directly comparing the value with `true` and `false`. **Pros and Cons of each approach:** 1. **Type checking (`typeof value === "boolean"`)**: * Pros: Simple, straightforward, and efficient for checking if a value is indeed a boolean. * Cons: Can be slow for large numbers of booleans due to the overhead of type checking. 2. **Value comparison (`value === true || value === false`)**: * Pros: Faster for large numbers of booleans since it's just a simple comparison. * Cons: Can be slower for single boolean values, as it involves an additional comparison. **Library and special JS features** There are no libraries or special JavaScript features used in this benchmark. The code is straightforward and only uses native JavaScript operators and data types. **Other considerations** Keep in mind that the performance difference between these two approaches may not be significant for most use cases, but it can add up when working with large datasets. **Alternatives** If you need to check if a value is a boolean in your code, `typeof` might still be a good choice. However, for large datasets or performance-critical code paths, using a simple comparison like `value === true || value === false` might be more efficient. It's worth noting that some JavaScript engines, like V8 (used by Chrome), have optimized the `typeof` operator to be faster for certain types of values.
Related benchmarks:
Strict equality VS typeof
Double bang vs Boolean
check if arrya
Array length boolean check
Number.isInteger() vs typeof
Comments
Confirm delete:
Do you really want to delete benchmark?