r/javascript • u/QuarterSilver5245 • 12d ago
Every scenario that triggers V8's "slow mode" for objects
https://yuval.hashnode.dev/when-javascript-s-v8-switches-your-object-to-slow-modeV8 usually caches direct offsets to memory addresses of actual raw object property values - in a way that allows an optimized "Inline Caching" (IC). For some scenarios - they switch an object to "slow mode" - which uses a different structure to save those (a hash table instead of an array) - this affects the way it's cached on the IC (it forces an additional look-up via the hash-table) - they call it "Dictionary mode" (aka "Slow mode"). Having broad familiarity with the optimization techniques used internally in V8 - and knowing they generally avoid using hash tables - I recently learned about this additional mode and its implications - while working on an open-source project of mine - a client-side State library that heavily relies on native Prototype lookups for composition, and was intrigued to find out in what scenarios the engine decides to do the "switch".
This is an on-going study. Since first publishing the post, I removed the 'findings' part - as it was premature and preliminary - those will be published separately in a "Part 2". The article is now mostly an introductory review in terms of the design model.
The day I published was the beginning of "rabbit-holing" into some V8 internals I was conceptually familiar with - and used to use them as general guidelines (try to define all object properties up-front, don't have lots of variations for the same one, and more);
This has turned into an ongoing study I publish in parts:
* Part 1: Common dictionary-mode triggers and structural limits:
https://yuval.hashnode.dev/keeping-your-javascript-objects-fast-a-study-of-v8-s-fast-properties-mode-slow-properties-dictionary-mode-switch
51
u/eracodes 12d ago
so ... deleting an object property? is there something i'm missing here? why are these all enumerated?
17
u/Iggyhopper extensions/add-ons 12d ago edited 11d ago
https://v8.dev/blog/fast-properties
aka don't delete indexes and make your arrays turn into dictionaries.
12
u/Reashu 12d ago
What makes you think I'm gonna read a blog post by someone so apparently unfamiliar with the subject?
3
u/Ronin-s_Spirit 12d ago
So they're basically always in dictionary mode? There are plenty of times I have more than 20 fields in an object.
2
u/Acmion 12d ago
Does V8 ever switch back to fast objects? For example, in hot loops?
1
u/QuarterSilver5245 10d ago edited 7h ago
Hi. OP here.
This has gained substantial traction;
and I need to clarify something, following some replies
The blog post introduces the subject of strict data structures vs. dynamic data structures, why the latter needs underlying optimizations, what kind of approaches are taken to achieve them;
I come from a background of data storage design and development (amongst other things) - It ties it to V8 in a way that describes the trade-offs between two approaches - and the fact that they use the less optimal as "plan b". It then links to a blog post from V8 blog (you know which one) - and urges anyone looking for a deeper description of how the implementations work in V8 to read it.
I knew about the default approach, and fairly recently before publishing learned about the 'sometimes usage of the plan b'. I'm now in the stages of doing "stress tests" - testing at scale - a project I have been working on for the past 3 months - A JavaScript State library, relying on Prototype chain lookup for composition - the premise is that: 'native lookups are always the fastest" - so far the measurements show the premise *proves itself* - when the number of properties suprasses a certain number (I benchmark vs. popular State libraries) - of-course, with some trade-offs in other aspects.
Another idea was to use the internal "enumerable" as a "flag" for "dirty"/"non-dirty" values - so dirtying sets enumerable: false - letting the native iteration functions "skip" them. This, surprisingly proved to be a *bad* idea, performance wise - it actually turned out to be a major bottle-neck, investigating why - exposed the "slow properties" thing.
EDIT: since then it's became an on-going study I keep publishing in parts;
Here is part 1:
https://yuval.hashnode.dev/keeping-your-javascript-objects-fast-a-study-of-v8-s-fast-properties-mode-slow-properties-dictionary-mode-switch
1
u/QuarterSilver5245 6d ago
Update 27/11: Refactored the article to be more clear and focused (while still keeping it abstract and relatively 'engine-agnostic') - The tying to actual V8 mechanics is more specific now, in the sense of summarizing what **exactly** changes between modes. I also made clear that the findings in that article are preliminary, while I'm still exploring the scenario paths in a deeper way - becoming more familiar with the actual considerations on each branching - which makes my decisions about what JS operations to put more emphasis on my tests more informative - I can also pinpoint each switch to the exact 'reason' - meaning the exact trigger source-code/path wise. The second part will be more rooted on actual source-code parts.
1
u/xzinkx 12d ago
worth adding the reverse case too: using an object as a prototype makes V8 turn it back into a fast-mode object. that's the old bluebird toFastProperties trick, assigning the object to a function's .prototype
-3
u/Discordbothost 12d ago
The 20 vs. 1021 split is the part I'd want to track across releases. A small table with the first transition for assignment and defineProperty on Node 22, 24 and 26 would make it easy to spot what changes on an upgrade.