From: Lasse Reichstein Nielsen on
Scott Sauyet <scott.sauyet(a)gmail.com> writes:

> The most interesting question was one of incrementing or decrementing
> the array iterator. In Chrome, there are only minor differences for
> array size 10, but at 100 and 1000, decrement is significantly
> faster. But by 10000, increment is twice as fast. In Firefox, 10 is
> too inconsistent to be useful; at 100 decerement is somewhat faster,
> but at 1000 and 10000, increment is noticeably faster.

The reason for this is the Chrome (and some of the other browsers too)
has special support for sparse arrays (which is slower than a
non-sparse array, but saves significant amounts of memory if the array
stays sparse). By starting the assignment from the end, the array
starts out looking very sparse.

If instead you allocate the array with its full size from the start,
i.e.:
var photos = new Array(library.length);
var captions = new Array(library.length);
then the arrays will be non-sparse and won't even need to grow
while filling. That should reduce the difference between increment
and decrement to just the handling of the loop variable.


> The upshot is that decrement is generally better at smaller array
> sizes, but you're probably better off with increment as the array gets
> larger.

Try decrement with a pre-allocated array (although I'm not sure which
browsers apart from Chrome allows using New Array(number) to allocate
a non-sparse array).

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Scott Sauyet on
On Jan 19, 1:16 am, Lasse Reichstein Nielsen <lrn.unr...(a)gmail.com>
wrote:
> Scott Sauyet <scott.sau...(a)gmail.com> writes:
>> The upshot is that decrement is generally better at smaller array
>> sizes, but you're probably better off with increment as the array gets
>> larger.
>
> Try decrement with a pre-allocated array (although I'm not sure which
> browsers apart from Chrome allows using New Array(number) to allocate
> a non-sparse array).

Thank you. That is definitely worth checking out, and I will in a day
or two when I'm not swamped with work.

-- Scott