Search code examples
javascriptgoogle-chromeoptimizationconsolev8

can I turn off optimization, so in-scope variables from closures aren't "optimized out"


As a byproduct of code optimization done by modern browsers, while debugging, you can't "see" all variables which "factually" are in scope. This is well known and has been addressed in a previous question here on SO. This feature, while most certainly useful in production is annoying me a lot during development, it slows me down (that should be obvious.)

Now my question is, is there any way to turn off this behavior? Can I edit some configuration file, or is there a browser plugin, or maybe there is a "special build version for developers" of the browser executable? I love typing my code into the console right away when I'm writing new code, so this is really bugging me.

visualSummaryIffalseConsoleLog

UPDATE / EDIT

Here is a partial solution, credit to Paul1365972.

You have to start the chrome browser from the command line, with special options, like so:

  1. Close Chrome completely
  2. Run Chrome from console with "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --js-flags="--allow-natives-syntax" that's for windows other OS similar.
  3. Open developer console and execute "%GetHeapUsage()". If you properly started Chrome with the option, a number will be logged to the console, otherwise you'll get a syntax error.

With this command line flag, you can 'talk to the V8' engine with commands starting with %, which are syntax errors in plain JavaScript. A list to available V8 commands of this kind was given in Paul's answer.

There is %NeverOptimizeFunction() on that list, which is something which looked like the thing I'd just have to call and be done with it. Unfortunately, that function does not do what I hoped for, as demonstrated in next screenshot.

lorem still not defined

(((The other link from Paul's answer (v8-natives node module) is of no importance to us here in this context. All it does is it wraps one-liners around the "%" function calls so the code doesn't crash browsers which are not v8.)))

(((I remember a time when this worked (when this optimization wasn't invented/implemented yet). I don't know how long ago. Ten years? 15 years? Something like that. What was the last Chrome version (if any) and what was the last firefox version (more sure here that it exists) where you could do? It won't get you the bounty, but it will get you an upvote, if you happen to know and post it as an answer.)))

THE SOLUTION

THANK YOU PETR SRNICEK

hacky fix

NEW QUESTION

While Petr's solution helps a lot, it is not perfect. This question is getting too long, so I posted a new question on how Petr's solution can be improved. (I could of course edit this question here, but that would feel "unhistorical", if you know what I mean.)


Solution

  • You can get access to all variables by wrapping the debugger statement in an eval like this: eval("debugger;");. This hacky solution adds another anonymous function to the call stack though and it is obviously of no use for breakpoints that are set manually in DevTools.

    This does not seem to be a very good solution, but since it is the only one that achieves the intended behaviour so far, I am posting it as an answer.