Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Mel Smith" Newsgroups: comp.lang.javascript Subject: Re: 'pushing' vs direct array assignment ? Date: Thu, 6 Jun 2013 08:54:21 -0600 Lines: 55 Message-ID: References: X-Trace: individual.net Ej9sAvP4PZi2j/0C/ikuQgLgB1lab9VsC4xVcmjDN00V+Gdm2I Cancel-Lock: sha1:6MKlqVwSyf0ORhylTHAMx3tAiTg= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Xref: csiph.com comp.lang.javascript:20168 Rob said: In order to determine a preference for one method or another, it is important to establish the criteria for evaluation. Some that might apply (in approximate order of importance) are: 1. Unintended side effects: iterating over an array by index may create undefined properties, but if you are creating a contiguous list from zero then it doesn't matter. 2. Support in likely host environments: very old browsers don't support Array.prototype.push, so if you need to support those it's better to use direct assignment (or add a push method if feature detection shows it's missing, which may be another question). If you use direct assignment, you never need to worry. But there must be very few browsers on the web that don't have push. 3. Performance: as has been noted, this goes both ways. For 1,500 items, I suspect it won't make much difference on a modern computer, but might be significant in the types of environments where support for push is more likely to be lacking. 4. Maintainability (code clarity): I don't think there are any issues here with one approach or the other. 5. Number of characters to type: direct assignment saves 3 characters, not worth dying in a ditch over (IMHO of course). So on balance, for code expected to be widely used on the web, I'd go with direct assignment, but be happy with push if it was used. Applying the above analysis to your context might give a different result. > or, is there another preferred way to do this quickly ? Depending on your target environment, JavaScript™ 1.7 (released in October 2006 with Firefox 2) introduced iterators and generators[1]. But that approach doesn't get a very high score for the support criterion if you need to work with environments other than those compliant with JavaScript™ 1.7. 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators Hi Rob: The above gives me much more to 'chew on' and puzzle over. Also, the link above on 'Iterators ...' will provide substance for this evening's reading -- The .next() function is completely new to me. Thank you. -Mel