On Wed, Jul 16, 2014 at 11:08 PM, Nicholas Nethercote <[email protected]> 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] https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals

