Monday, August 1, 2011

The somewhat depressing state of Object.create performance

I have recently been introduced to the niceties of the new EMCA 5 Object model, which revolves around Object.create. The syntax is a bit wonky to those of us that have been javascripting for some time now, but once you get used to it there are some really great features at work here, not the least of which are actual properties, a much better inheritance model, tighter access control, and more! I'm not crazy about losing the ability to "new" my objects, and the funny little hacks that you need to put in place to simulate a constructor are turn-offs for me, but past that there's an awful lot to love here...

... except the performance.

I was curious about how the new model compared with the tried and true methods in terms of speed, so I whipped up a simple jsperf benchmark to gauge how different aspects of the two object methodologies performed. (And I'm not the first either) The results, frankly, were rather depressing.

On my iMac with Chrome 14 (dev channel) new Obj() is currently outperforming Object.create() by a factor of 10! Seriously! 10 times slower, and we've lost constructor functions along the way! Fortunately member access and function calls are virtually indistinguishable performance-wise once the objects are created, which is good (if expected). Sadly, however, utilizing Properties (one of the big bonuses of the new model) is painfully slow. My tests showed a Property to be 200 time slower than a good old setFoo/getFoo pair. The numbers are about the same on Safari, though Firefox showed some interesting variations. There wasn't a single platform where the new model could be called a clear performance winner though.

Of course, the feature is fairly new and hasn't undergone the rigorous optimization that some of the older methods have, so I would fully expect to see these numbers improve moving forward, but for now if you're performance conscious you'd do well to steer clear of Object.create.

(Oh, and despite drastically redesigning the Javascript object model we STILL couldn't be bothered to add operator overloading? Really?!?)


  1. Hello,
    I ran your test on a Firefox 6 on linux mint and it seems that firefox does a good job at Object.create(), running faster than `new`

    new ObjA: 23,676,880
    Object create ObjB: 32,599,882

    Surprisingly the member read write test yields some sad results:

    ObjA member read write: 75,204,755
    ObjB member read write: 19,073,904

    Hopefully the performance gets better soon.

  2. I found this post today, July 25 2014, because sadly I figured out my code was running much slower in Chrome because of using Object.create(proto) instead of new EmptyFunctionWithProto(). It went from 33 seconds to just one second when switching! So three years later, and performance is still terrible...

    At least in Firefox Object.create is faster than using constructors, but I really don't want to have to check what browser I'm using for such a basic action as creating new objects...

  3. As for me, it's really important to find useful writing source. For example, this useful information will help you if you are going to write your dissertation soon.