Performance: Object Literal vs. Object Assignment notation

There are so many things that run through your mind when first learning how to be an amazing programmer. Something that always crept into my mind was performance, even though I really needed to focus on so many other things. I completely forgot the question of ‘which is better: Object Literal or Object Assignment notation?’ until I got the chance to TA at the HTML5DevConf in fabulous San Francisco, CA.

For those of us that need a little refresher –

Object Literal notation looks like this:

var objL = {
  prop : 1,
  meth: function() {
    return 'stuff!';
  }
};

Object Assignment notation looks like this:

var objA = {};
objA.prop = 2;
objA.meth = function() {
  return 'hey!';
};

Back to the performance.  A number of the students asked if there was a performance issue. That took me back to when I asked the same exact question and thanks to my trust, favorite search engine I found the following link and results.

objLit

objlvsobja

(graph represents operations per second – the higher the better)

Needless to say I was shocked at the difference in performance.

Moral of the story:

if you want to have the fastest code possible (and I really hope you do!) you should DEFINITELY get on the Literal bandwagon.

Next homework assignment:

Read up on more ways to improve performance!

Advertisements

2 thoughts on “Performance: Object Literal vs. Object Assignment notation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s