Mixing strategies in one app
Every fork in the engine ships as several strategies, picked at construction, so no single strategy pays a branch on the hot path. That holds while an app uses one strategy per kind. Mix several strategies of the same kind in one process and the engine's shared code starts seeing all of them at once.
What a shared call site sees
V8 speeds up a property read or a method call by remembering what it saw there before: the hidden class of the object, or the function it called. That memory is per call site, not per collection. Two collections built with different strategies still run the same engine code, so a site inside the engine sees both strategies.
- One hidden class at the site is monomorphic: the fastest case.
- Two to four is polymorphic. V8 still caches every one of them and can
still inline the call. Four is V8's own limit:
node --v8-optionslists--max-valid-polymorphic-map-countwith a default of 4. - Five or more is megamorphic. V8 drops the site's own cache, looks each call up in one cache shared by the whole process, and stops inlining through the site.
The cost lands on every collection that passes through the site, including the ones that use only one strategy.
Which strategies count
A strategy counts when it is an object or a function the engine calls from code shared with the other strategies of its kind:
- slot release policies, the
IOIMSlotReleasePolicystrategies; - set-driven and array-driven buckets and indexes;
- entity writers,
createOIMInPlaceEntityWriterandcreateOIMReplacingEntityWriter, and entity insert strategies,TOIMEntityInserter; - the
sameValueGuardof a computed (TOIMSameValueGuard); - slot emptiness checks,
IOIMSlotEmptinessPolicy; - channel schedulers,
TOIMChannelScheduler.
What to do
Use one strategy per kind per app unless a task really needs two. Two or three strategies of one kind keep a site polymorphic, and that is fine. Five different release policies in one process is not.
What has been measured
One pair measures the polymorphic side of this, on the slot write. A single write function handed the three shipped slot shapes, against the same function handed three slots minted with one unified shape: 8 ns against a floor of 7 ns (1.12×). Three shapes at one site cost about the same as one. That pair is why up to four strategies of a kind is fine. Its date and how far apart its two timings came out are on its card in Shipped against the alternatives.
No pair yet measures a site with five or more shapes, so the megamorphic side of this page rests on V8's documented behaviour, not on a figure from this engine.