On Fri, May 22, 2015 at 1:43 PM, Kevin Greer <
k...@google.com> wrote:
> This jsPerf test shows that defineProperty() and defineProperties() are
> about 7 times slower than with the same properties defined using the object
> literal notation:
>
>
https://jsperf.com/define-property-vs-define-properties
>
> Is there some way to use defineProperties() differently to get it as fast as
> the literal method, or could the V8 implementation be improved to make it as
> fast?
No.
defineProperty/defineProperties is a reflective API.
Object literals are statically known so we can create boilerplate
objects for them. If the value of the property is constant (a literal)
then we also include that in the boilerplate. We store this
boilerplate as part of the function and when the object literal is
needed we dop a fast clone of the boilerplate. It the value is not
constant we can still use StoreICs to set the actual value which is
just a few machine instructions compared to the full code and spec of
defineProperty:
https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/v8natives.js&l=752
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-validateandapplypropertydescriptor
(this is the bigpart of it)
> For our app, defineProperty() is the biggest hotspot slowing down our
> startup time.
Why do you need to use reflection to create your objects?
Once the object has been created it does not matter if it was created
from O.dP or a literal.
It is fine to use reflection to build your prototype chain but for the
actual instances you are better off using [[Set]].
--
erik