Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20136

Re: 'pushing' vs direct array assignment ?

From "Mel Smith" <med_cutout_syntel@aol.com>
Newsgroups comp.lang.javascript
Subject Re: 'pushing' vs direct array assignment ?
Date 2013-06-03 18:40 -0600
Message-ID <b14rcqFnmifU1@mid.individual.net> (permalink)
References <b14atkFk8dnU1@mid.individual.net> <7dc473f5-c0e8-415b-92e1-f6201220fe9d@j8g2000yqa.googlegroups.com> <kojb4i$s6f$1@news.albasani.net>

Show all headers | View raw


Stefan said:

> The first question I would ask is: are you going to populate your array
> once or many times? If it's only once, then just forget about
> optimization, it will run too fast to notice with either method.

    Once or twice for the 'big' page --- which I've now shrunk down to 
234K -- and with gzip its now dow to ~~ 57K

>
> That was the pragmatic answer ... and now for the fun part :)
>
>> One micro-benchmark can be found here:
>>
>>     <http://jsperf.com/array-index-push/3>
>>
>> In all the more recent browsers except IE9, `push` is faster.
>> Unfortunately, in IE8 and IE9, it's quite a bit slower.  And since
>> that's already the slowest environment, you'll have to make a decision
>> with that in mind.
>
> That's unexpected. Looking more closely at your test case, there is one
> optimization that has a very measurable impact here, at least in Chrome:
>
> For the index insertion test, instead of
>
>   var a = [];           // and append 1000 elements
>
> initialize the array to the proper size first:
>
>   var a = Array(1000);  // and set 1000 elements
>
> Obviously, that doesn't work for the two other tests, but there's a
> definite improvement in Chrome for "index insertion":
>
> With a = []:
>
>   UA          | ops/sec
>  -------------+------------------------------
>   Chrome[1]   |  57,773  for index insertion
>   Firefox[2]  |  63,650  for index insertion
>
> With a = Array(1000):
>
>   UA          | ops/sec
>  -------------+------------------------------
>   Chrome[1]   | 230,493  for index insertion
>   Firefox[2]  |  65,300  for index insertion
>
>
> No significant change in FF.
> In Chromium, with the Array() optimization, the index insertion test is
> roughly 50% faster than push(); and ~4x faster than with 'a = []';
> I'm almost out of RAM, so I can't start a VM and test other browsers.
> Stupid old laptops and their 4GB limit...
>
> [1] Chromium 25.0.1364.160
> [2] Firefox 21
> both running on Ubuntu 12.4 64bit
>
>
>> But regardless of speed, `push` is certainly cleaner.  And in my mind,
>> that counts for a fair bit.
>
> I agree.
> Premature micro-optimization is a waste of time (but oh so much fun).
>
> Things were different 6-7 years ago. I remember making exactly this
> optimization and getting a very noticable reduction in UI lag. But that
> was for an HTML/JS spreadsheet app, so we had a _lot_ of array copy
> operations.

Hi Stefan:

    I'll see if I have the length of the array available when I start my 
'pushing' or 'insertion' or whatever -- my largest array is approx 1434 
elements, each containing a property with 2 properties.

Thank you for the additional ideas !

-Mel

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-03 13:59 -0600
  Re: 'pushing' vs direct array assignment ? Scott Sauyet <scott.sauyet@gmail.com> - 2013-06-03 13:39 -0700
    Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-03 14:46 -0600
    Re: 'pushing' vs direct array assignment ? Stefan Weiss <krewecherl@gmail.com> - 2013-06-04 02:07 +0200
      Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-03 18:40 -0600
        Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-03 20:38 -0600
        Re: 'pushing' vs direct array assignment ? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-06-04 09:11 -0600
          Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-04 12:47 -0600
            Re: 'pushing' vs direct array assignment ? Stefan Weiss <krewecherl@gmail.com> - 2013-06-04 22:44 +0200
              Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-04 16:02 -0600
            Re: 'pushing' vs direct array assignment ? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-06-04 20:24 -0600
              Re: 'pushing' vs direct array assignment ? Gregor Kofler <usenet@gregorkofler.com> - 2013-06-05 14:50 +0200
                Re: 'pushing' vs direct array assignment ? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-06-05 22:38 -0600
              Re: 'pushing' vs direct array assignment ? Tim Slattery <Slattery_T@bls.gov> - 2013-06-05 08:51 -0400
                OT: Accept-Encoding (was: 'pushing' vs direct array assignment ?) Christoph Becker <cmbecker69@gmx.de> - 2013-06-05 15:23 +0200
  Re: 'pushing' vs direct array assignment ? RobG <rgqld@iinet.net.au> - 2013-06-05 18:12 -0700
    Re: 'pushing' vs direct array assignment ? "Mel Smith" <med_cutout_syntel@aol.com> - 2013-06-06 08:54 -0600

csiph-web