Large defineProperties() vs literal getter and setter Performance

63 views
Skip to first unread message

Kevin Greer

unread,
May 22, 2015, 1:43:18 PM5/22/15
to v8-u...@googlegroups.com
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?

For our app, defineProperty() is the biggest hotspot slowing down our startup time.

Thanks

Erik Arvidsson

unread,
May 26, 2015, 4:10:22 PM5/26/15
to v8-u...@googlegroups.com
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

Kevin Greer

unread,
May 26, 2015, 4:26:16 PM5/26/15
to v8-u...@googlegroups.com
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]].

We aren't using it to create objects, just prototypes.  We use defineProperty() to create setters for our prototype properties which work similar to Java's property change support.  This lets our objects fire property change events when they update, so that we can bind them efficiently to our GUI's, even on systems that don't have Object.observe().  We also have pre-set filters, post-set callbacks, and hooks into our Functional Reactive Programming system.

Thanks,

KGR



--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/SuMyWPS_IKg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages