r/javascript • u/QuarterSilver5245 • 8d ago
Every scenario that triggers V8's "slow mode" for objects
yuval.hashnode.devV8 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) - since then a lot of things have deepened:
Familiarity with V8 debugging/tracing tools, familiarity with other more specific design features and aspects of the internal V8 representation of a JS Object, the internal lifecycle of a JSObject, what are paths and decisions each lifecycle stage (creation, updating, deleting) "looks for" / tries to optimize for, as time passes - I give more informed answers to questions here (and expand on older ones - when relevant) - I still try to keep it 'abstract', because I'm a believer in "abstractism" as an efficient communication style - that bridge the gaps between different levels of prior knowledge.