obj = {a: 42, b:66, c: 88}
Object.hasOwn(obj, 'b')
'b' in obj
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.hasOwn | |
in |
Test name | Executions per second |
---|---|
Object.hasOwn | 36727512.0 Ops/sec |
in | 80147928.0 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark compares two ways to check if a property exists in an object: Object.hasOwn
and the "in"
operator (also known as the "has property" operator).
Options Being Compared
There are two options:
Object.hasOwn(obj, 'b')
: This is the traditional way to check if a property exists in an object using the Object.hasOwn()
method.**: This uses the
"in"operator to check if the property
'b'` exists in the object.Pros and Cons of Each Approach
Object.hasOwn(obj, 'b')
:Library and Its Purpose
The Object.hasOwn()
method is a built-in JavaScript method that checks if an object has a property with the given name. It's used to avoid the error "TypeError: Cannot read own property of undefined or null" when trying to access a non-existent property.
Special JS Feature/Syntax
There isn't any special JS feature or syntax being tested in this benchmark, as both approaches are standard JavaScript methods and operators.
Other Alternatives
If you were to compare other ways to check for property existence in an object, some alternatives could be:
in
operator with typeof
: e.g., 'b' in obj && typeof obj['b'] !== 'undefined'
hasOwnProperty()
that can check for property existence.Benchmark Preparation Code
The script preparation code creates an object obj
with three properties: 'a'
, 'b'
, and 'c'
. The values of these properties are hardcoded as numbers (42, 66, and 88).
Latest Benchmark Result
The latest benchmark result shows the execution rate per second for each browser on a desktop platform. The results suggest that the "in"
operator is faster than Object.hasOwn()
in this specific case.
In summary, the benchmark compares two ways to check if a property exists in an object: Object.hasOwn()
and the "in"
operator. While both approaches have their pros and cons, the "in"
operator appears to be slightly faster in this particular case.