function objectProperties(object, result = {}) {
let proto = object;
while ((proto = Object.getPrototypeOf(proto)) !== Object.prototype) {
for(const name of Object.getOwnPropertyNames(proto)) {
result[name] = name;
}
}
return result;
}
objectProperties(new MediaSource());
function objectProperties(object, result = {}) {
let proto = object;
while ((proto = Object.getPrototypeOf(proto)) !== Object.prototype) {
for(const descriptor in Object.getOwnPropertyDescriptors(proto)) {
result[descriptor] = descriptor;
}
}
return result;
}
objectProperties(new MediaSource());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getOwnPropertyNames | |
getOwnPropertyDescriptors |
Test name | Executions per second |
---|---|
getOwnPropertyNames | 183718.3 Ops/sec |
getOwnPropertyDescriptors | 90338.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark test for two approaches to retrieving property names and descriptors from an object: getOwnPropertyNames
and getOwnPropertyDescriptors
.
What is being tested?
These tests compare the performance of two methods:
getOwnPropertyNames
: This method returns an array of strings representing the names of properties defined directly on the given object. It does not include inherited properties.getOwnPropertyDescriptors
: This method returns an object containing the descriptors of all properties defined directly and indirectly on the given object.Options compared
The two options being compared are:
Object.getOwnPropertyNames()
: Returns an array of strings representing the names of properties defined directly on the object.Object.getOwnPropertyDescriptors()
: Returns an object containing the descriptors of all properties defined directly and indirectly on the object.Pros and Cons:
getOwnPropertyNames()
: Pros:getOwnPropertyDescriptors()
: Pros:Library: None
There are no external libraries used in these tests. The Object
object is a built-in JavaScript object that provides various methods for manipulating objects, including the two options being compared.
Special JS feature or syntax: None
The tests use only standard JavaScript features and syntax.
Benchmark preparation code
The provided benchmark preparation code defines a simple function objectProperties
that takes an object and returns a result object containing property names. The function uses Object.getPrototypeOf
to traverse the object's prototype chain and iterates over its properties using Object.getOwnPropertyNames
(or Object.getOwnPropertyDescriptors
) to retrieve their names.
Individual test cases
There are two individual test cases:
getOwnPropertyNames
: This test case runs the objectProperties
function with a new instance of MediaSource
object, passing Object.getOwnPropertyNames
as the method to use.getOwnPropertyDescriptors
: This test case is similar to the first one, but uses Object.getOwnPropertyDescriptors
instead.Latest benchmark result
The provided JSON contains two execution results:
getOwnPropertyNames
, with 183718 executions per second.getOwnPropertyDescriptors
, with 90338 executions per second.Alternatives
Other alternatives for retrieving property names and descriptors include:
Reflect.ownKeys()
or Object.keys()
to get an array of property names, which may be more efficient than using Object.getOwnPropertyNames()
.Object.entries()
to get an array of key-value pairs, where each pair contains the name and descriptor of a property.