Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
undefined vs. typeof vs. in vs. hasOwnProperty (Object.create(null))
(version: 0)
Object lookup performance
Comparing performance of:
undefined vs typeof vs in vs bool
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = Object.assign(Object.create(null), { a: 1, b: 2, c: 3, d: 4, e: 5 });
Tests:
undefined
undefined !== obj.d;
typeof
'undefined' !== typeof obj.d;
in
'd' in obj;
bool
!! obj.d;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
undefined
typeof
in
bool
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
undefined
82348520.0 Ops/sec
typeof
77903920.0 Ops/sec
in
72259888.0 Ops/sec
bool
81941672.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark test case, measuring the performance of different approaches for object lookup in a specific scenario. **Benchmark Definition** The benchmark definition is as follows: * The script preparation code creates an object `obj` with properties `a` to `e`. * Each test case compares a different expression: + `undefined` + `typeof` + `in` + `bool` The expressions are compared against the value of `obj.d`. **Options Compared** The benchmark tests four options for comparing an expression against a value: 1. **`undefined`**: Tests if the value is not equal to `undefined`. 2. **`typeof`**: Tests if the type of the value is different from the expected type (in this case, `number`). 3. **`in`**: Tests if the value is present in the object as a property key. 4. **`bool`**: Tests if the value is truthy (i.e., not equal to `0`, `false`, `null`, `undefined`, or an empty string). **Pros and Cons of Each Approach** 1. **`undefined`**: * Pros: Simple, easy to understand, and widely supported. * Cons: May be slower due to the need for a separate check for `undefined`. 2. **`typeof`**: * Pros: Can handle type checks more efficiently than other approaches. * Cons: May not work as expected for certain types (e.g., `NaN`, `Infinity`). 3. **`in`**: * Pros: Fast and efficient, especially for large objects. * Cons: May not be supported in all browsers or environments (e.g., older versions of Internet Explorer). 4. **`bool`**: * Pros: Simple and intuitive, with a clear meaning. * Cons: May be slower due to the need for an additional type check. **Library and Special JS Features** The benchmark uses no external libraries. It does not use any special JavaScript features beyond what is specified in the benchmark definition. **Other Considerations** When choosing an approach for object lookup, consider the following factors: * **Performance**: If you're dealing with large objects or performance-critical code, `in` might be a good choice. * **Browser Support**: If you need to support older browsers, make sure to test with those versions. * **Readability and Maintainability**: Choose an approach that is easy to understand and maintain. **Alternatives** Other approaches for object lookup include: * Using the `===` operator (equal to) instead of `!==` * Using a `switch` statement or a `case` expression * Using a library like Lodash's `_.includes()` function However, these alternatives may not be as efficient or widely supported as the options tested in this benchmark.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
undefined vs. typeof vs. in vs. hasOwnProperty vs. Object.prototype.hasOwnProperty.call
undefined vs. typeof vs. in vs. hasOwnProperty for non-existing property
undefined vs. typeof vs. in vs. hasOwnProperty 222
Comments
Confirm delete:
Do you really want to delete benchmark?