A Fastify Tale of Shapes
summary
A bug in Fastify v3 makes request/reply objects become megamorphic because plugin encapsulation creates a new object.create prototype chain for every registration, producing different V8 shapes and forcing deoptimizations. Fixes: eagerly add decorator properties on construction and avoid creating new prototypes unless a decorator is actually used, so objects keep identical hidden classes. The change yields about 10% throughput improvement in Fastify v4 and is verifiable with V8 optimization flags.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 1:39Fastify Overview
- 2:35Request Lifecycle & Plugins
- 5:03V8 and the Event Loop
- 6:22Optimization Compilers
- 9:05Object Shapes and Hidden Classes
- 11:58The Bug and Debugging
- 13:46Initial Fix Attempt
- 17:08Encapsulation Issues
- 20:21The Final Fix
- 21:48Q&A and Best Practices








