I would expect that for some values of length it would be deemed "too
sparse" and no longer treated as a single underlying vector.

On Thursday, July 17, 2014, Katelyn Gadd <[email protected]> wrote:

> What happens if you combine 'new Array(length)' + 'array[length - 1] =
> <default value>'? That seems like it would produce the optimal
> behavior in all cases.
>
> On Wed, Jul 16, 2014 at 11:40 PM, Nicholas Nethercote
> <[email protected] <javascript:;>> wrote:
> > On a related note: ObjectElements::VALUES_PER_HEADER is 2. Is this
> > because 2 * sizeof(HeapSlot) is 16 bytes, which is equal to
> > sizeof(ObjectElements)?
> >
> > Nick
> >
> > On Wed, Jul 16, 2014 at 11:34 PM, Nicholas Nethercote
> > <[email protected] <javascript:;>> wrote:
> >> On Wed, Jul 16, 2014 at 11:08 PM, Nicholas Nethercote
> >> <[email protected] <javascript:;>> wrote:
> >>> Hi,
> >>>
> >>> Here's some code from pdf.js:
> >>>
> >>> function stringToArray(str) {
> >>>   var length = str.length;
> >>>   var array = [];
> >>>   for (var i = 0; i < length; ++i) {
> >>>     array[i] = str.charCodeAt(i);
> >>>   }
> >>>   return array;
> >>> }
> >>>
> >>> This kind of code shows up in a number of places in pdf.js. Pretty
> >>> simple -- filling in an array one element at a time.
> >>>
> >>> Underneath the covers, this causes the array to repeatedly double in
> >>> size, which is annoying. And I thought I could avoid it because we
> >>> know how big the array will be in advance. So I changed the 3rd line
> >>> to this:
> >>>
> >>>   var array = new Array(length);
> >>>
> >>> but I still get the same sort of doubling behaviour under the covers.
> >>
> >> Oh wait. It depends on the value of |length|. If it's 2050 or less,
> >> then the right-sized array is allocated immediately. Otherwise, it
> >> allocates 10 elements and then does the doubling thing. I guess
> >> there's a limit to how much we'll pre-allocate.
> >>
> >> So using |new Array(length)| seems like the best thing, then.
> >>
> >> Nick
> > _______________________________________________
> > dev-tech-js-engine-internals mailing list
> > [email protected] <javascript:;>
> > https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals
> _______________________________________________
> dev-tech-js-engine-internals mailing list
> [email protected] <javascript:;>
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals
>
_______________________________________________
dev-tech-js-engine-internals mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals

Reply via email to