Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
!! vs. hasOwn vs. in with null obj
(version: 2)
Object lookup performance
Comparing performance of:
exist null vs exist hasOwnProperty vs nonexist null vs nonexist hasOwnProperty vs nonexist in vs exist in
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; var obj2 = Object.create(null); Object.assign(obj2, obj);
Tests:
exist null
for (let i = 0; i < 10000; i++) { !!obj.d; if (i % 200 === 0) { obj[i] = 5; } }
exist hasOwnProperty
for (let i = 0; i < 10000; i++) { Object.hasOwn(obj, 'd'); if (i % 200 === 0) { obj[i] = 5; } }
nonexist null
for (let i = 0; i < 10000; i++) { !!obj.f; if (i % 200 === 0) { obj[i] = 5; } }
nonexist hasOwnProperty
for (let i = 0; i < 10000; i++) { Object.hasOwn(obj, 'f'); if (i % 200 === 0) { obj[i] = 5; } }
nonexist in
for (let i = 0; i < 10000; i++) { 'd' in obj2; if (i % 200 === 0) { obj2[i] = 5; } }
exist in
for (let i = 0; i < 10000; i++) { 'f' in obj2; if (i % 200 === 0) { obj2[i] = 5; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
exist null
exist hasOwnProperty
nonexist null
nonexist hasOwnProperty
nonexist in
exist in
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):
Let's dive into the benchmark. **What is being tested?** The benchmark measures the performance of three different ways to check if a property exists in an object: 1. `!!obj.d` (also known as the "double-not" method) 2. `Object.hasOwn(obj, 'd')` 3. `'d' in obj2` **Options compared** Here's a brief explanation of each option and their pros and cons: * **Double-Not (`!!obj.d`)**: This method uses the logical NOT operator to check if `obj.d` is not null or undefined. It's a simple, lightweight approach. + Pros: Fast, easy to understand. + Cons: May be less accurate than other methods, as it doesn't account for objects with non-boolean properties that are truthy (e.g., an object with a property set to 0). * **`Object.hasOwn(obj, 'd')`**: This method checks if `obj` has a direct own property named `'d'`. It's the recommended way to check if a property exists in an object. + Pros: Accurate, easy to understand. + Cons: May be slower than other methods due to the extra function call. * **`'d' in obj2`**: This method uses the "in" operator to check if `'d'` is a property of `obj2`. It's another way to check if an object has a certain property. + Pros: Fast, easy to understand. + Cons: May be less accurate than other methods for objects with non-string properties. **Library** The benchmark uses the `Object.create()` and `Object.assign()` methods to create and populate the objects. The `Object.hasOwn()` method is also used. **Special JS feature or syntax** None mentioned in this benchmark. **Other considerations** When measuring performance, it's essential to consider factors like cache locality, branch prediction, and loop unrolling. In this case, the benchmark uses a simple loop with 10,000 iterations, which might not be representative of real-world scenarios. **Alternatives** If you want to run this benchmark or create your own, here are some alternatives: 1. Benchmarking libraries like `benchmark.js`, `micro-benchmark`, and `fastbench`. 2. JavaScript engines like V8 (Chrome) or SpiderMonkey (Firefox), which have built-in benchmarking tools. 3. Online platforms like MeasureThat.net, where you can create and run your own benchmarks. Keep in mind that the best approach for benchmarking will depend on your specific use case and performance requirements.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty big object
undefined vs hasOwnProperty
in vs. hasOwnProperty
in vs hasOwn
Comments
Confirm delete:
Do you really want to delete benchmark?