babel: V8 can't optimize Array with initial size

https://v8project.blogspot.com/2017/09/elements-kinds-in-v8.html

Let’s say we’re trying to create an array, for example:

const array = new Array(3);
// The array is sparse at this point, so it gets marked as
// `HOLEY_SMI_ELEMENTS`, i.e. the most specific possibility given
// the current information.
array[0] = 'a';
// Hold up, that’s a string instead of a small integer… So the kind
// transitions to `HOLEY_ELEMENTS`.
array[1] = 'b';
array[2] = 'c';
// At this point, all three positions in the array are filled, so
// the array is packed (i.e. no longer sparse). However, we cannot
// transition to a more specific kind such as `PACKED_ELEMENTS`. The
// elements kind remains `HOLEY_ELEMENTS`.

Once the array is marked as holey, it’s holey forever — even if it’s packed later! Any operation on the array from then on is potentially slower than it could be. If you plan on performing lots of operations on the array, and you’d like to optimize those operations, avoid creating holes in the array. V8 can deal with packed arrays more efficiently.

Cool, so we’ll just reallocate space after pushes. 🙄


EDIT: Check https://github.com/babel/babel/issues/6233#issuecomment-329164507

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Alright, sounds like an easy fix to just use new Array() instead of Array and forget using [] like you said. Would be a nice beginner contribution too.

screen shot 2017-09-13 at 1 14 20 pm

Just a quick search and I found 3 helper locations to change.