DEV Community

Be aware of Arrays - V8 engine advice

Alireza Ebrahimkhani on February 13, 2024

In this blog, I decided to talk about arrays and their behavior inside v8. By understanding these you can write efficient code that is good for v8 ...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Why does every "500 javascript one-liners" article get inundated with attention but stuff like this just barely makes it into my feed on the front page. I want more interesting stuff like this.

Collapse
 
peerreynders profile image
peerreynders • Edited

Look in the right place and follow the right people.

This content was available 7 years ago by Mathias Bynens.

Mathias Bynens (@mathias@mastodon.xyz) - Mastodon

3 Posts, 232 Following, 2.34K Followers · I work on @ChromeDevTools (ex-@v8js) at Google and on ECMAScript through TC39. ♥ JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.

favicon mastodon.xyz

Axel Rauschmayer

Axel Rauschmayer (@rauschma@fosstodon.org) - Fosstodon

4.14K Posts, 219 Following, 4.69K Followers · Topics: #JavaScript #TypeScript #fedi22 Other interests: – Languages: German, English, French, Spanish, Dutch, Mandarin – Sustainability, degrowth, permaculture, urbanism – Tiny houses – Education – Psychology, getting out of one’s head, heart-centered living – Minimalist spirituality: Advaita, Daoism, Buddhism, Christian mysticism, J. Krishnamurti, … I live in Munich. http://pronoun.is/he Non-tech: – :pixelfed: Photos: @rauschma@pixelfed.de – 💬 Languages: @langtales@mastodon.social

favicon fosstodon.org

has covered holes since 2012:

If you are interested in this kind of thing: dig into why you may want to minimize megamorphic objects:

Collapse
 
gabrielfallen profile image
Alexander Chichigin

You might also want to check out old posts by one of the former V8 devs (nowadays a Dart dev) who developed some nice profiling tools: mrale.ph/

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

More importantly, how the hell was 2017 7 years ago that feels so wrong 😖

Collapse
 
alirezaebrahimkhani profile image
Alireza Ebrahimkhani

Thanks!
These articles are amazing. You can also follow the v8 engines' official blog at v8.dev

Collapse
 
manchicken profile image
Mike Stemle

“Holey” is probably better written as “sparse,” which is the common technical term for this phenomenon.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Fascinating, thank you!

Collapse
 
marco_cabrera_81e1796f41f profile image
marco cabrera

Wow. Great article I definitely don’t think a lot about the V8 side of things. Thanks for the great information.

Collapse
 
bwca profile image
Volodymyr Yepishev

Fascinating 🤓

Collapse
 
starswan profile image
Stephen Dicks

Its a shame to only frame this in terms of performance. Unlike other languages, Javascript only has one collection type called 'associative arrays' which handle both the 'array' (contiguous list of items) and hash/dictionary/map of other languages. x[9] = 3 where x is [1,2,3] is not just slow, it should be impossible on your domain as its effectively changing the type from list to dict - so ideally designs should hide these things to prevent clients misusing them.

Collapse
 
jdtoombs profile image
Jonny • Edited

Great article!

Quick question - if I had an array of ints and I were to remove one of the items and replace it with undefined, that would make it now PACKED_DOUBLE_ELEMENTS instead of PACKED_SMI_ELEMENTS?

The reason I ask is because I see if you add NaN or Infinity to a SMI_ELEMENTS array it becomes DOUBLE_ELEMENTS. Does v8 handle undefined the same way?

// example (remove 3 -> add undefined)
const array = [1, 2, undefined, 4]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
s333mo profile image
Simone Pizzoleo

Crazy it only appeared me in 2024. Thanks for sharing, adding more and more knowledge everyday with these "kinds" (🤪) of articles!

Collapse
 
alihkz profile image
Alien

mind-blowing paper. thank you.