Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The author of the code used empty JavaScript object as a basis of a array like object, i.e. the one that can be accessed by index and has a length property.</p> <p>There could be two reasons that I could think of:</p> <ol> <li>memory use - if array is backed by implementation that allocates n elements, when it reaches it's limits it grows by some factor to increase it's capacity, and thusly wastes <code>capacity - length</code> of memory</li> <li>cpu time - implementor choose insertion speed over random access speed -- a return from this method is more likely to be iterated over sequentially than random accessed, and resizing the array in insertion causes allocation-copy-deallocation which has a cpu penalty</li> </ol> <p>I'm betting that similar code would be found in other JavaScript libraries, and that it's result of benchmarking and finding the best to fit solution across different browsers.</p> <p><em>edited after comment by Justin</em></p> <p>Upon further googling it appears that array-like objects are common among JavaScript developers: checkout JavaScript: the definitive guide by David Flanagan, it has a whole <a href="http://books.google.com/books?id=2weL0iAfrEMC&amp;pg=PT144&amp;lpg=PT144&amp;dq=David+Flanagan+array-like&amp;source=bl&amp;ots=_8-ymy_45C&amp;sig=I_Q6RRm_rnyI5_nzTFmDZVUkeBM&amp;hl=en&amp;ei=JC85S6mEK5D34Abp3qWqCA&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=2&amp;ved=0CA8Q6AEwAQ#v=onepage&amp;q=&amp;f=false" rel="nofollow noreferrer">sub-chapter on Array-like objects</a>. Also <a href="http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/" rel="nofollow noreferrer">these</a> <a href="http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript" rel="nofollow noreferrer">guys</a> mention them.</p> <p>No mention of why should one prefer array-like vs array object. This could be a good SO question.</p> <p>So a third option could be the key: compliance with norms of JavaScript API's.</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