polymorphic and megamorphic
Describes code that sees objects of a few different shapes (polymorphic) or very many (megamorphic), which makes optimisation harder.
In more detail
Polymorphic code must check which of several shapes it has before each property read, and megamorphic code gives up on caching altogether. This is V8's meaning of the words, unrelated to polymorphism in object-oriented design.
Related terms
- monomorphic — Describes a place in the code that always sees objects of one single shape, which lets TurboFan optimise it best.
- hidden class — An internal record V8 keeps of an object's property names and their order, so it can find properties quickly.
- deoptimization — When a just-in-time compiler throws away optimised machine code because the assumptions it was built on no longer hold, and falls back to slower code.
Taught in
- V8 Engine & JIT Pipeline: Bytecode & Turbofan — Node.js Internals & Backend Services