tiny JS helper of the day: const createArray = (length, fn) => Array.from( {length}, typeof fn === 'function' ? fn : () => fn ); createArray(5, 0); // [0,0,0,0,0] createArray(3, (_, i) => i * i); // [0, 1, 4]
-
-
so, basically all I had to do in order to have PACKED_(XXX_)?ELEMENTS back was to add a `.map($ => $)` after creation
const createArray = (length, fn) => Array.from(
typeof length === 'number' ? {length} : length,
typeof fn === 'function' ? fn : () => fn
).map($ => $); -
meaning: every Array .fill(value) should be followed by an identity map in order to have fastest v8 Array optimizations: Array(3).fill(0); // HOLEY_SMI_ELEMENTS Array(3).fill(0).map($ => $); // PACKED_SMI_ELEMENTS now you know (and one more time I miss a Function.identity)
- 6 more replies
New conversation -
-
-
Please have a look at my first suggestion too, and why v8 wouldn't promote an array fill as not holed, when the kind of array is inferred by its filled content? Also with Array.from, where each entry *could* infer the content of the array
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
@mathias it does seem like v8 could benefit from a "contiguous elements" kind, which indicates that the lower elements are packed and upper elements undefined. Would have to store a "packed length" internal field. Seems like this would avoid many instances of holeyness.Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.