Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
undefined vs typeof vs in vs hasOwnProperty / ver. 2
(version: 0)
Object lookup performance
Comparing performance of:
undefined vs typeof vs in vs hasOwnProperty vs bool
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; var keys = new Array(4096 * 5); for (let i = 0; i < keys.length; i += 1) keys[i] = Math.random().toString(); for (let i = 0; i < keys.length; i += 2) obj[keys[i]] = i;
Tests:
undefined
for (let i = 0; i < 4096; i += 1) obj[keys[4096 * 0 + i]] === undefined;
typeof
for (let i = 0; i < 4096; i += 1) typeof obj[keys[4096 * 1 + i]] === "undefined";
in
for (let i = 0; i < 4096; i += 1) keys[4096 * 2 + i] in obj;
hasOwnProperty
for (let i = 0; i < 4096; i += 1) obj.hasOwnProperty(keys[4096 * 3 + i]);
bool
for (let i = 0; i < 4096; i += 1) !obj[keys[4096 * 4 + i]];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
undefined
typeof
in
hasOwnProperty
bool
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):
The provided JSON represents a JavaScript benchmark test case, specifically designed to measure the performance of different ways to check if a key is present in an object. **Benchmark Definition** The benchmark definition is specified by the "Script Preparation Code" and the individual test cases. The script preparation code creates an object `obj` with keys `a`, `b`, `c`, `d`, and `e`. It also generates an array of 20,000 random strings, `keys`, which will be used to access the object. **Individual Test Cases** There are five individual test cases: 1. **"undefined"`**: This test case checks if a key is present in the object by using the `obj[keys[4096 * 0 + i]]` expression. Since the value at this position is not defined, this test should return false. 2. **"typeof"`**: This test case checks if a key is present in the object by using the `typeof obj[keys[4096 * 1 + i]]` expression. Since the value at this position is not defined, this test should return "undefined". 3. **"in"`**: This test case checks if a key is present in the object by using the `keys[4096 * 2 + i] in obj` expression. 4. **"hasOwnProperty"`**: This test case checks if a key is present in the object by using the `obj.hasOwnProperty(keys[4096 * 3 + i])` expression. 5. **"bool"`**: This test case checks if a key is present in the object by using the `!obj[keys[4096 * 4 + i]]` expression. **Options Compared** The five test cases compare different ways to check if a key is present in an object: * Direct property access (`obj[keys[i]]`) * `typeof` operator * `in` keyword * `hasOwnProperty` method (only on objects) * Boolean negation (`!obj[key]`)
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
undefined vs. typeof vs. in vs. hasOwnProperty big object
undefined vs. typeof vs. in vs. hasOwnProperty 222
undefined vs. typeof vs. in vs. hasOwnProperty vs Object.hasOwn
Comments
Confirm delete:
Do you really want to delete benchmark?