𝗛𝗼𝘄 𝗩𝟴 𝗠𝗮𝗸𝗲𝘀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝗮𝘀𝘁
JavaScript is dynamic. Types change. Objects change shape. This usually slows things down.
V8 makes it fast.
V8 uses JIT compilation. It does not compile everything at once. It runs code and watches it.
Two tools do the work:
- Ignition: Starts the code fast. It collects data.
- TurboFan: Takes the hot code. It makes it run fast.
V8 uses hidden classes. It treats objects like they have a fixed shape. This helps the CPU find data quickly.
It also uses inline caches. The engine remembers where a property lives. It does not look for it every time.
Problems happen when you change types. This is deoptimization.
V8 assumes a variable is a number. You change it to a string. V8 throws away the fast code. It goes back to the slow path.
You want your code to be predictable.
- Keep types the same.
- Keep object shapes the same.
- Avoid mixing types in arrays.
Predictable code runs faster.