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 ReferenceError
s. 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.