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
- use `new Array(len)` instead of `Array(len)` for V8 optimisation see https://github.com/babel/babel/issues/6233 — committed to zloirock/core-js by zloirock 7 years ago
- Allocate by using `new Array(n)` Array(n) is causing some deopts in v8 - https://github.com/babel/babel/issues/6233#issuecomment-329055890 — committed to Andarist/callbag-merge by Andarist 6 years ago
- Allocate by using `new Array(n)` Array(n) is causing some deopts in v8 - https://github.com/babel/babel/issues/6233#issuecomment-329055890 — committed to Andarist/callbag-combine by Andarist 6 years ago
- Allocate by using `new Array(n)` Array(n) is causing some deopts in v8 - https://github.com/babel/babel/issues/6233#issuecomment-329055890 — committed to staltz/callbag-combine by Andarist 6 years ago
- Allocate by using `new Array(n)` Array(n) is causing some deopts in v8 - https://github.com/babel/babel/issues/6233#issuecomment-329055890 — committed to Andarist/next by Andarist 6 years ago
- Allocate by using `new Array(n)` (#5) Array(n) is causing some deopts in v8 - https://github.com/babel/babel/issues/6233#issuecomment-329055890 — committed to emotion-js/next by Andarist 6 years ago
Alright, sounds like an easy fix to just use
new Array()
instead ofArray
and forget using[]
like you said. Would be a nice beginner contribution too.Just a quick search and I found 3 helper locations to change.
Array(x)
tonew Array(x)
in whatever places