Back

The Temporal Dead Zone, or why the TypeScript codebase is full of var statements

2 days ago vincentrolfs.dev

Story Summary Story

Last updated: 14 hours ago

Modern JavaScript provides const and let for variable declarations, offering block scoping and a Temporal Dead Zone (TDZ). The TDZ prevents access to a variable before its initialization, acting as a safeguard against referencing uninitialized variables and causing ReferenceErrors. Conversely, the older var keyword lacks block scoping, allowing variables to be accessed before declaration, resulting in undefined rather than errors.

Surprisingly, the TypeScript codebase extensively uses var. This is due to performance considerations. The interpreter's effort to track and enforce the TDZ introduces overhead. By reverting to var, which bypasses TDZ checks, the TypeScript project achieved an 8% performance improvement in benchmarks, deeming the TDZ's runtime cost significant for their large codebase.

Comments Summary Comments