[JS-internals] Growing arrays

2014-07-17 Thread Nicholas Nethercote
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

Re: [JS-internals] Growing arrays

2014-07-17 Thread Nicholas Nethercote
On Wed, Jul 16, 2014 at 11:08 PM, Nicholas Nethercote n.netherc...@gmail.com 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; }

Re: [JS-internals] Growing arrays

2014-07-17 Thread Katelyn Gadd
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 n.netherc...@gmail.com wrote: On a related note: ObjectElements::VALUES_PER_HEADER is

Re: [JS-internals] Growing arrays

2014-07-17 Thread Jan de Mooij
On Thu, Jul 17, 2014 at 8:34 AM, Nicholas Nethercote n.netherc...@gmail.com wrote: 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

Re: [JS-internals] Growing arrays

2014-07-17 Thread Nicolas B. Pierron
On 07/16/2014 11:08 PM, Nicholas Nethercote wrote: So then I tried reverting that change and inserting this line just before the loop: array[length - 1] = 0; And now it avoids the doubling allocations -- the array elements are allocated once, at the right size. But it feels dirty, and I

Re: [JS-internals] Growing arrays

2014-07-17 Thread Kannan Vijayan
On 2014-07-17, 4:35 AM, Jan de Mooij wrote: On Thu, Jul 17, 2014 at 8:34 AM, Nicholas Nethercote n.netherc...@gmail.com wrote: 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

Re: [JS-internals] Growing arrays

2014-07-17 Thread Terrence Cole
On 07/16/2014 11:40 PM, Nicholas Nethercote 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)? Yes. Nick On Wed, Jul 16, 2014 at 11:34 PM, Nicholas Nethercote