console.log 会降低 JavaScript 执行性能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11426185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:47:33  来源:igfitidea点击:

Will console.log reduce JavaScript execution performance?

javascriptperformance

提问by Sudantha

Will use of the debugging feature console.logreduce JavaScript execution performance? Will it affect the speed of script execution in production environments?

使用调试功能会console.log降低 JavaScript 执行性能吗?会不会影响生产环境中脚本的执行速度?

Is there an approach to disable console logs in production environments from a single configuration location?

是否有一种方法可以从单个配置位置禁用生产环境中的控制台日志?

采纳答案by Ramesh

If you are going to have this on a public site or something, anyone with little knowledge on using the developer tools can read your debug messages. Depending on what you are logging, this may not be a desirable behavior.

如果您打算在公共站点或其他地方使用它,任何对使用开发人员工具知之甚少的人都可以阅读您的调试消息。根据您正在记录的内容,这可能不是理想的行为。

One of the best approaches is to wrap the console.login one of your methods, and where you can check for conditions and execute it. In a production build, you can avoid having these functions. This Stack Overflow questiontalks in details about how to do the same using the Closure compiler.

最好的方法之一是将 包装console.log在您的方法之一中,您可以在其中检查条件并执行它。在生产版本中,您可以避免使用这些功能。这个Stack Overflow 问题详细讨论了如何使用Closure 编译器来做同样的事情。

So, to answer your questions:

所以,回答你的问题:

  1. Yes, it will reduce the speed, though only negligibly.
  2. But, don't use it as it's too easy for a person to read your logs.
  3. The answers to this questionmay give you hints on how to remove them from production.
  1. 是的,它会降低速度,虽然只是微不足道。
  2. 但是,不要使用它,因为人们很容易阅读您的日志。
  3. 这个问题的答案可能会给你一些关于如何将它们从生产中删除的提示。

回答by tomekwi

Actually, console.logis a lot slower than an empty function. Running this jsPerf teston my Chrome 38 gives stunning results:

实际上,console.log比空函数慢很多。在我的 Chrome 38 上运行这个 jsPerf 测试给出了惊人的结果:

  • when the browser console is closed, calling console.logis about 10 000 times slowerthan calling an empty function,
  • and when the console is open, calling it is as much as 100 000 times slower.
  • 当浏览器控制台被关闭时,呼叫console.log慢大约10万次比调用空函数,
  • 当控制台打开时,称它是慢高达100万次

Not that you'll notice the performance lag if you have a reasonable number of console.…calls firing once (a hundred will take 2 ms on my install of Chrome – or 20 ms when the console is open). But if you log stuff to the console repeatedly – for instance, hooking it up through requestAnimationFrame– it can make things janky.

并不是说如果您有合理数量的console.…调用一次触发,您不会注意到性能延迟(在我安装 Chrome 时,100 次需要 2 毫秒 - 或者在控制台打开时需要 20 毫秒)。但是,如果您反复将内容记录到控制台 - 例如,将其连接起来requestAnimationFrame- 它会使事情变得混乱。

Update:

更新:

In this testI've also checked out the idea of a custom “hidden log” for production – having a variable which holds log messages, available on demand. It turns out to be

在这个测试中,我还检查了用于生产的自定义“隐藏日志”的想法——有一个保存日志消息的变量,可按需使用。原来是

  • about 1 000 timesfaster than the native console.log,
  • and obviously 10 000 times faster if the user has his console open.
  • 大约比原生快1000 倍console.log
  • 如果用户打开控制台,显然会快 10 000 倍。

回答by sq2

If you create a shortcut to the console in a common core script, eg:

如果您在通用核心脚本中创建控制台的快捷方式,例如:

var con = console;

and then use con.log("message") or con.error("error message") throughout your code, on production you can simply rewire con in the core location to:

然后在整个代码中使用 con.log("message") 或 con.error("error message"),在生产中,您可以简单地将核心位置的 con 重新连接到:

var con = {
    log: function() {},
    error: function() {},
    debug: function() {}
}

回答by Sergey Guns

const DEBUG = true / false
DEBUG && console.log('string')

回答by holydragon

Will use of the debugging feature console.log reduce JavaScript execution performance? Will it affect the speed of script execution in production environments?

使用调试功能 console.log 会降低 JavaScript 执行性能吗?会不会影响生产环境中脚本的执行速度?

Of course, console.log()will reduce your program's performance since it takes computational time.

当然,console.log()会降低程序的性能,因为它需要计算时间。

Is there an approach to disable console logs in production environments from a single configuration location?

是否有一种方法可以从单个配置位置禁用生产环境中的控制台日志?

Put this code at the beginning of your script to override the standard console.log function to an empty function.

将此代码放在脚本的开头以将标准 console.log 函数覆盖为空函数。

console.log = function () { };

回答by Petah

Any function call will slightlyreduce performance. But a few console.log's should not have any noticeable effect.

任何函数调用都会略微降低性能。但是少数console.log的应该不会有任何明显的影响。

However it will throw undefined errors in older browsers that don't support console

但是它会在不支持的旧浏览器中抛出未定义的错误 console

回答by Paul

The performance hit will be minimal, however in older browsers it will cause JavaScript errors if the users browsers console is not open log is not a function of undefined. This means all JavaScript code after the console.log call will not execute.

性能影响很小,但是在旧浏览器中,如果用户浏览器控制台未打开,则会导致 JavaScript 错误log is not a function of undefined。这意味着在 console.log 调用之后的所有 JavaScript 代码都不会执行。

You can create a wrapper to check if window.consoleis a valid object, and then call console.log in the wrapper. Something simple like this would work:

您可以创建一个包装器来检查是否window.console为有效对象,然后在包装器中调用 console.log。像这样简单的事情会起作用:

window.log = (function(console) {
    var canLog = !!console;
    return function(txt) {
        if(canLog) console.log('log: ' + txt);
    };
})(window.console);

log('my message'); //log: my message

Here is a fiddle: http://jsfiddle.net/enDDV/

这是一个小提琴:http: //jsfiddle.net/enDDV/

回答by J?rgen

I made this jsPerf test: http://jsperf.com/console-log1337

我做了这个 jsPerf 测试:http://jsperf.com/console-log1337

It doesn't seem to take any longer than other function calls.

它似乎不需要比其他函数调用更长的时间。

What about browsers that doesn't have a console API? If you need to use console.log for debugging, you might include a script in your production deployment to override the console API, like Paul suggests in his answer.

没有控制台 API 的浏览器呢?如果您需要使用 console.log 进行调试,您可以在生产部署中包含一个脚本来覆盖控制台 API,就像 Paul 在他的回答中建议的那样。