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
dev-tech-js-engine-internals@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-internals

Reply via email to