lexical environment optimizaton

🚧 under construction

JSconceptsscopelexical environment ⟩ JS engine optimization

  • in theory while a function is alive, all outer variables are also retained.

  • but in practice, JS engines try to optimize that, if they find that an outer variable is not used – it is removed.

An important side effect in v8 (Chrome, Edge, Opera) is that such variable will become unavailable in debugging.

replit:lexical environment: optimization

let value = "Surprise!";

function f() {
    
    // ⭐ `value` never used by `g`.
    // ⭐ v8 will optimize it out from the "lexical environment"!
    // ⭐ it's not accessable in the "debugger".
    let value = "the closest value";

    return function g() {
        // ⭐ in "console": type `alert(value)`.
        debugger;         // ❗ Surprise!
    }
}

let g = f();
g();

Last updated