Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, <code>var o = {};</code> and <code>var o = new Array();</code> aren't the same. The first initializes an object, the second an array. <code>var o = {};</code> and <code>var o = new Object();</code> are equivalent.</p> <p>Now about the performance of using an object literal instead of adding the properties after. Which one is fastest? The answer is, we don't care, and you shouldn't either. <em>If</em> there is a difference in performance, it will be so small that it will never impact, even if you create 1 million objects at once, which is unlikely to ever happen.</p> <p>This is called premature-optimization, and is the bane of many intermediate programmers. Don't worry about optimizing anything unless you start having performance problems. Then you use a profiler to detect where the bottleneck is and solve it. Just worry about making your app.</p> <p>For completeness' sake, <a href="http://jsperf.com/object-litterals-vs-properties">here is a test I ran on jsperf</a>. In my browser, Chrome 15, the object literal initialization was 53% faster. Wow, 53%, that's huge right? Except if you put your mouse over the tooltip for the test that uses properties after initialization, you'll see it says something like</p> <blockquote> <p>Ran 681,285 times in 0.077 seconds.</p> </blockquote> <p>Your numbers may differ, but you'll be able to observe that the method considered slowest still goes pretty fast by any standards. I think it's safe to say that both are fast enough for any purpose. Just use the one you prefer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload