How do I get the value of a variable that has been optimized?

Some variables can be "optimized" at runtime Javascript. Therefore, the values โ€‹โ€‹of such variables are not available for checking during debugging ( User Documentation ). The variable view displays a (optimized off) message and the console throws the following error if a variable tries to be evaluated:

Error: variable has been optimized out

      

Is there a way to force-evaluate such a variable or disable this optimization in Firefox?

+3


source to share


2 answers


Use the variable in a way that is not optimized.



function NOP() {}

// then in the optimised code

    NOP(myvar);
    // debugging here should now show `myvar`

      

+3


source


When a variable has been "optimized" it simply means that it does not change in the context of the current scope. Hence, the JavaScript engine has done some optimization magic and is currently hiding this variable. For example, let's say you are using lodash to iterate over some collection.

var parentThingy = [];
var childThingy = [];
_.each (collectionThingy, function(data){

    // parentThingy is not being modified inside this callback
    // so it will be "optimized away" while you are inside this
    // function scope.

    var transformed;
    if (data.someFlag) {
        transformed = transformDataSomehow(data);
    }

    // childThingy is being modified, so you will be able to
    // see its value in the debugger.

    if (transformed) {
        childThingy.push(transformed);
    }
});

// Now that you've exited the callback scope, you will be able to see
// the value of parentThingy again.

if (childThingy.length > 1){
   parentThingy.push(childThingy);
}

      



You can use the NOP clause to make parentThingy visible in the callback scope, but since you are not modifying parentThingy inside that callback, you may not need to see it. He has not changed and will not change. This does not apply to the code you are currently debugging. As soon as you exit the callback area, parentThingy will be visible again to the debugger.

FYI: This is not a Firefox thing. Chrome does the same thing, it just uses different wording to indicate that the variable is not relevant to the current scope.

+1


source







All Articles