Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof "undefined" versus falsey check
(version: 0)
which is faster
Comparing performance of:
typeof vs negate
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const undef = undefined function checkType(val) { return typeof val === 'undefined' } function negate(val) { return !val }
Tests:
typeof
const undef = undefined checkType(undef)
negate
const undef = undefined negate(undef)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
typeof
negate
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
6487204.5 Ops/sec
negate
6488988.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what's being tested. **Benchmark Definition** The benchmark is testing which approach is faster: using the `typeof` operator or negating the value (using the `!` operator) to check if a value is "falsy" (i.e., undefined, null, 0, empty string, etc.). **Script Preparation Code** The script preparation code defines two functions: * `checkType(val)` returns `true` if `val` is of type `'undefined'`. * `negate(val)` returns the negation of `val`, effectively checking if it's falsy. **Html Preparation Code** There is no HTML preparation code, which means that this benchmark only focuses on JavaScript performance and doesn't involve any DOM-related tasks. **Individual Test Cases** The benchmark has two test cases: 1. `typeof`: This test case checks the performance of using the `typeof` operator to check if a value is `'undefined'`. 2. `negate`: This test case checks the performance of negating the value (using the `!` operator) to check if it's falsy. **Options Compared** The options being compared are: * Using `typeof` to check for 'undefined' * Negating the value (using `!`) to check for falsiness **Pros and Cons** Using `typeof`: Pros: * More explicit and readable * Can be used with other operators (e.g., `in`, `instanceof`) Cons: * May incur additional overhead due to the function call * May not be as efficient as a simple negation operation Negating the value: Pros: * Often faster, as it's a single operation * Can be more concise and readable in some cases Cons: * Less explicit and less readable for those unfamiliar with this pattern * May not work correctly with all types of falsy values (e.g., NaN) **Library Used** There is no library used in the provided benchmark code. The `typeof` operator and negation are built-in JavaScript features. **Special JS Feature or Syntax** The only special syntax used here is the use of the `!` operator to negate a value, which is a common idiom in JavaScript for checking for falsiness. **Other Alternatives** If you're interested in testing other approaches, here are some alternatives: * Using `Object.prototype.hasOwnProperty.call(obj, 'undefined')`: This approach uses the prototype chain to check if an object has a property called `'undefined'`. * Using a library like Lodash's `isUndefined` function: This approach relies on a external library for its functionality. * Using a different negation operator, such as `== -1` or `=== 0`: These approaches use a different syntax to check for falsiness. Keep in mind that the choice of approach often depends on the specific requirements and constraints of your project.
Related benchmarks:
Testing for false vs === undefined vs hasOwnProperty for undefined member
Typeof x === 'undefined' vs x === undefined
Typeof x === 'undefined' vs x === undefined (test without syntax error)
if(typeof <var> ===undefined) vs if(<var>)
Comments
Confirm delete:
Do you really want to delete benchmark?