Well, with Firefox 31 using Array(length).fill(0) to construct the the
array could be the best solution as it preallocates the array in one
go.

On 18 July 2014 00:42, Nicholas Nethercote <[email protected]> wrote:
> In this case, maybe not, but there are many similar cases where using
> a typed array is less feasible.
>
> Nick
>
> On Thu, Jul 17, 2014 at 2:18 PM, Igor Bukanov <[email protected]> wrote:
>> Any reasons to avoid Uint16Array ?
>>
>> On 17 July 2014 08:08, 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.
>>>
>>> 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 don't
>>> know if would give the same behaviour in other JS engines.
>>>
>>> Is there a "right way" to do this?
>>>
>>> Nick
>>> _______________________________________________
>>> dev-tech-js-engine-internals mailing list
>>> [email protected]
>>> 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