我可以关闭优化功能,以免关闭中的范围内变量“被优化掉"吗? [英] can I turn off optimization, so in-scope variables from closures aren't "optimized out"

查看:170
本文介绍了我可以关闭优化功能,以免关闭中的范围内变量“被优化掉"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为现代浏览器完成代码优化的副产品,在调试时,您无法看到"实际上"在范围内的所有变量.这是众所周知的,并且已经在上一个问题中得到解决在SO 上.此功能虽然在生产中最肯定有用,但在开发过程中却使我很烦,但它使我放慢了速度(这很明显.)

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.

更新/编辑

这是部分解决方案,应归功于Paul1365972.

Here is a partial solution, credit to Paul1365972.

您必须使用特殊选项从命令行启动chrome浏览器,如下所示:

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

  1. 完全关闭Chrome浏览器
  2. 使用"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --js-flags="--allow-natives-syntax"从控制台运行Chrome 这适用于Windows其他类似的操作系统.
  3. 打开开发人员控制台并执行"%GetHeapUsage()".如果您使用该选项正确启动了Chrome,则会在控制台上记录一个数字,否则会出现语法错误.
  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.

使用此命令行标志,您可以使用以%开头的命令与V8引擎对话",这是普通JavaScript中的语法错误.保罗的答案中给出了 .

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.

该列表上有%NeverOptimizeFunction(),这看起来就像我只需要调用并完成的事情.不幸的是,如下一个屏幕截图所示,该功能无法实现我希望的功能.

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.

((((( Paul的答案的其他链接(v8-natives节点模块)在这种情况下对我们而言并不重要,它所做的只是将%"函数调用包装成一行,这样代码就不会使非v8的浏览器崩溃.)))

(((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.)))

(((((我记得有一段时间该优化(尚未发明/实现这种优化).我不知道多久了.十年?十五年?类似的东西.最后一个Chrome是什么?版本(如果有的话)以及您可以执行的最后一个Firefox版本(请确保在这里存在)是什么?如果您碰巧知道并发布了它,它不会给您带来赏金,但会给您带来赞誉.作为答案.)))

(((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.)))

解决方案

感谢您的SRNICEK

新问题

尽管Petr的解决方案有很大帮助,但这并不完美.这个问题太长了,所以我发布了

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.)

推荐答案

您可以通过将调试器语句包装在如下这样的eval中来访问所有变量:eval("debugger;");.但是,这种骇人听闻的解决方案在调用堆栈中添加了另一个匿名函数,对于在DevTools中手动设置的断点,它显然毫无用处.

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.

这篇关于我可以关闭优化功能,以免关闭中的范围内变量“被优化掉"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆