Object.defineProperty Performance

839 views
Skip to first unread message

ohnnyj

unread,
Sep 15, 2011, 1:46:48 PM9/15/11
to v8-users
Hello all:

I am in the process of building a UI framework in JS using ECMA5
whenever possible. I love the new Object.create and
Object.defineProperty syntax with the ability to cleanly add accessor
methods.

I have been doing some profiling of various accessor techniques and
have consistently found Object.defineProperty to be much slower than
property syntax and normal function definitions. For reference please
see:

http://jsperf.com/getter-setter/6

This isn't a small difference, on my machine I was getting something
like 230 million ops/s vs 1.4 million ops/s. I have done further
tests to get a sense of real world usage. For example, I have created
a WebGL animation library and toggled between using functions and
properties defined with defineProperty:

object.getX() / object.setX()

vs

object.x

As the number of objects increases the property syntax shows a clear
decrease in performance over the function calls, somewhere in the
range of 6 to 10 fps.

I am not sure this is due to the perhaps v8 (and in general all JS
engines) being optimized for function syntax over ECMA5 syntax or
perhaps the extra baggage that comes with the latter, namely the
writable, configurable, and enumerable attributes.

Just curious if I am the only one who has seen this.

Thanks.

Rico Wind

unread,
Sep 16, 2011, 1:17:26 AM9/16/11
to v8-u...@googlegroups.com
Quick comment first:
You are running all of this on objects in global scope which will mess
up the results considerably. Running this inside a non global scope
should almost eliminate the difference between the property and method
access times, but the method might still be slightly faster since we
will create optimized code for the two methods, whereas we will still
use ICs for the property load and store.

As your numbers suggest there is no difference in the access times no
matter how you declare accessors on the objects (except when you
create them by hand using methods), but the difference to methods
seems unreasonably high (I get 50X running in function scope), I will
take a closer look at this. I filled
http://code.google.com/p/v8/issues/detail?id=1693 to track this.

Cheers,
Rico

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
>

ohnnyj

unread,
Sep 16, 2011, 10:27:49 PM9/16/11
to v8-users
I really appreciate that you took the time to look into this.

You're right, using function scope will reduce the performance delta.
I tested this by wrapping the code in an anonymous function. This
actually makes sense in regards to the framework I am writing. Most
code is isolated in separate files using anonymous functions,
including the animation code mentioned before. The performance delta
described in the animation example doesn't reflect the 230x difference
in the initial test but more like the 50-70x difference found by using
function scope.

Looks as though the method implementation is heavily dependent on
scope. Global scope is about 4x faster on my machine. Whereas
Object.defineProperty doesn't seem to be affected by scope at all.

Thank you for filing a bug, I will be monitoring its progress.

- john

On Sep 15, 10:17 pm, Rico Wind <ri...@chromium.org> wrote:
> Quick comment first:
> You are running all of this on objects in global scope which will mess
> up the results considerably. Running this inside a non global scope
> should almost eliminate the difference between the property and method
> access times, but the method might still be slightly faster since we
> will create optimized code for the two methods, whereas we will still
> use ICs for the property load and store.
>
> As your numbers suggest there is no difference in the access times no
> matter how you declare accessors on the objects (except when you
> create them by hand using methods), but the difference to methods
> seems unreasonably high (I get 50X running in function scope), I will
> take a closer look at this. I filledhttp://code.google.com/p/v8/issues/detail?id=1693to track this.
Reply all
Reply to author
Forward
0 new messages