Reputation: 53
If, during the loading stages of a script, many properties are defined using Object.defineProperty, Object.defineProperties or Object.create. And most descriptors are either non-writable (if not accessors), non-configurable, non-enumerable or a combination. Relative to regular assignment or object initialising, what is the passive performance difference if any and the performance difference of accessing the target properties?
I've heard that defining non-writable and/or non-configurable properties involves setting up guards to prevent writing and/or re-definition will be one cause of performance loss, is this true?
Definition example:
Object.defineProperty(window, 'foo', {
configurable: false,
enumerable: false,
writable: false,
value: 10
});
Upvotes: 5
Views: 1245
Reputation: 40511
Creating properties with Object.defineProperty/defineProperties is definitely slower than just plain assigning them. (How much? Depends. Measure! The best way to make benchmarks relevant is to make them as similar as possible to the real use case you're interested in. Ideally, you measure different versions of your actual production code.)
Reading properties has the same speed regardless of how the property was defined.
Writing to existing properties must always check for writability, precisely because it can't know whether foo.bar = baz
was used or Object.defineProperty(foo, "bar", {value: baz})
. So that's the same speed too.
There are no special guards.
Upvotes: 2