Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
To Boolean
(version: 0)
Checking two methods to convert types to Boolean
Comparing performance of:
toBoolean vs toBool
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function toBoolean(anyVal,defaultVal) { let bool = defaultVal || false; if (typeof anyVal === 'boolean') { bool = anyVal; } else if (typeof anyVal !== 'undefined') { bool = ((`${anyVal}`).toLowerCase() === 'true'); } return bool; } function toBool(stringVal, defaultVal) { if (stringVal === undefined) { return Boolean(defaultVal); } const strFalseValues = ['0', 'false', 'no'].concat([]); if (typeof stringVal === 'string') { return (strFalseValues.indexOf(stringVal.toLowerCase().trim()) === -1); } // value is likely null, boolean, or number return Boolean(stringVal); }
Tests:
toBoolean
toBoolean("true",true)
toBool
toBool("true",true)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
toBoolean
toBool
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):
**Overview** The provided JSON represents a JavaScript microbenchmark, specifically the "To Boolean" benchmark, which measures how fast two different methods are to convert types to Boolean. The benchmark consists of two test cases: `toBoolean` and `toBool`. **What is being tested?** Two methods are being compared: 1. `toBoolean(anyVal, defaultVal)`: This function checks if the input value `anyVal` is already a boolean, and if so, returns it. Otherwise, it uses a combination of type checking and string matching to determine if the value should be treated as true. 2. `toBool(stringVal, defaultVal)`: This function checks if the input string `stringVal` is not in an array of falsey values (`'0', 'false', 'no'`). If it's undefined or a boolean value, it returns the default value. Otherwise, it uses a similar approach to `toBoolean`. **Options being compared** The two methods are compared based on their execution speed. **Pros and Cons of different approaches:** 1. **`toBoolean(anyVal, defaultVal)`**: * Pros: Simple and efficient for non-string inputs. * Cons: May not handle string inputs correctly (e.g., `('hello')` might be treated as true). 2. **`toBool(stringVal, defaultVal)`**: * Pros: Handles string inputs correctly and provides a more explicit way to convert values to boolean. * Cons: May be slower than the first method due to the string matching step. **Other considerations** 1. **Type checking**: Both methods use type checking to determine if the input value should be treated as true. However, `toBoolean` uses `typeof anyVal === 'boolean'`, which is more explicit and efficient. 2. **String matching**: `toBool` uses a string array to match against falsey values, while `toBoolean` uses a simple `toLowerCase()` check. **Library usage** Neither of the functions use any external libraries, but they do rely on built-in JavaScript features like `typeof`, `===`, and `toLowerCase()`. **Special JS feature or syntax** None are used in this benchmark. However, it's worth noting that both methods use a common pattern of checking for specific types or values to determine if the input should be treated as true. **Other alternatives** There may be other ways to implement these functions, such as using `Number()` or `parseInt()`, but these approaches would likely have similar performance characteristics.
Related benchmarks:
double equals null vs boolean
typeof boolean vs === true || === false
js boolean conversion
typeof vs types
Comments
Confirm delete:
Do you really want to delete benchmark?