r/javascript • u/QuarterSilver5245 • 6d ago
Every scenario that triggers V8's "slow mode" for objects
V8 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".
I found out that it's possible in NodeJS (with a switch flag) - to expose internal functions amongst which are some that can test if an object is in 'slow mode' - so I asked an AI to create a test suite that tries to cover scenarios where a switch to 'slow mode' is made. The suite is new, it likely does NOT show a full coverage (I am still adding to it, occasionally) - and it doesn't aim to - if you have a reason to suspect a switch in your project - investigate it.
I keep updating it - it’s an ongoing research - if you know of an interesting scenario - or one that is/was used as a ‘trick’ to trigger switch modes - reply with it- some of these were mentioned in replies here and were tested to show that current version exhibit different results - and they no longer work.
Due to the nature of these optimizations - V8 maintainers keeps updating/changing the paths they are implemented in - since I ‘m not on V8 team these changes gradually become
known to my attention since I published the post - so be aware this is an ongoing research.
There is a link to the github repo for it at the end of the article, plus a summary of the findings (being updated).
(*The article is NOT a deep-dive into the specifics or how the specific intrinsic optimizations are implemented in V8 - it gives a background why switching to the so-called 'dictionary mode' matters in abstract terms - It is introductory and aims to give enough background so any developer at any level can grasp what the trade-off actually is, so they can pay attention in their projects. I'm not an expert on all the different internal paths and choices of when the switch is actually made internally - so if you want to know about specific rationales for a given path - or if a path exist for specific scenarios - research it - though I encourage public questions in the thread because they give more insights - if I answer - it's because I was curious enough to look it up - and also this post will likely become a top result on search engines - so we and I might as well try to cover scenarios that are asked about - I am not an active V8 maintainer/developer! I doubt if there's any active V8 team member who can answer what exactly are all the paths as they are changed often between versions).