Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof undefined vs falsey check
(version: 0)
which is faster
Comparing performance of:
typeof vs negate
Created:
5 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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
12029783.0 Ops/sec
negate
12029484.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance difference between two approaches to check if a value is "undefined" or not: 1. `typeof` function 2. `!!val` (logical negation) function In JavaScript, `undefined` is a special value that represents an uninitialized variable, null reference, or missing property. The `typeof` operator returns the type of a value as a string ("undefined", "number", "string", etc.). On the other hand, `!!val` is a shorthand way to check if a value is true (or not false) by converting it to a boolean and then negating the result. **Options compared** The two options being compared are: 1. `typeof` function 2. `!!val` (logical negation) function **Pros and Cons of each approach:** 1. **typeof function** * Pros: + Can be used to check for other types as well (e.g., "number", "string", etc.) + Returns a string indicating the type, which can be useful in some cases * Cons: + Slower than `!!val` because it involves a function call and a potentially expensive lookup in the `typeof` object 2. **!!val (logical negation)** * Pros: + Faster than `typeof` because it's just a simple boolean operation + More concise and easier to read * Cons: + Only checks for truthiness, not type + Requires an extra step to negate the result **Library usage** There is no library being used in this benchmark. The code snippets provided are self-contained JavaScript functions. **Special JS features or syntax** None mentioned. **Other considerations** * The `checkType` function is only called with a single argument, which is always `undefined`. This might make the test less representative of real-world scenarios. * The `negate` function is not actually negating anything; it's just returning the opposite boolean value. If the intention was to negate the result, the function should be modified to use a different approach (e.g., `!!val`). **Alternatives** If you wanted to compare other approaches to check if a value is "undefined" or not, here are some alternatives: 1. Use a simple `if` statement: `if (val === undefined) { ... }` 2. Use a conditional expression: `value ? true : false` 3. Use the `==` or `===` operators with a value of 0, as in `if (val == 0 || val === 0) { ... }` However, since the goal is to measure performance and not just check for "undefined", it's best to stick with the `typeof` function and `!!val` options tested in this benchmark.
Related benchmarks:
Typeof x === 'undefined' vs x === undefined
Typeof x === 'undefined' vs x === undefined (test without syntax error)
if(typeof <var> ===undefined) vs if(<var>)
typeof "undefined" versus falsey check
Comments
Confirm delete:
Do you really want to delete benchmark?