解读生产环境为什么避免使用console.log

console.log:向web开发控制台打印一条消息,经常使用来在开发时调试分析。有时在开发时,须要打印一些对象信息,但发布时却忘记去掉console.log语句,这可能形成内存泄露。html

在传递给console.log的对象是不能被垃圾回收 ♻️,由于在代码运行以后须要在开发工具能查看对象信息。因此最好不要在生产环境中console.log任何对象。web

实例------>demos/log.html浏览器

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Leaker</title>
</head>

<body>
  <input type="button" value="click">
  <script>
    !function () {
      function Leaker() {
        this.init();
      };
      Leaker.prototype = {
        init: function () {
          this.name = (Array(100000)).join('*');
          console.log("Leaking an object %o: %o", (new Date()), this);// this对象不能被回收
        },

        destroy: function () {
          // do something....
        }
      };
      document.querySelector('input').addEventListener('click', function () {
        new Leaker();
      }, false);
    }()
  </script>
</body>

</html>

这里结合Chrome的Devtools–>Performance作一些分析,操做步骤以下:工具

⚠️注:最好在隐藏窗口中进行分析工做,避免浏览器插件影响分析结果性能

  1. 开启【Performance】项的记录
  2. 执行一次CG,建立基准参考线
  3. 连续单击【click】按钮三次,新建三个Leaker对象
  4. 执行一次CG
  5. 中止记录

clipboard.png

能够看出【JS Heap】线最后没有降回到基准参考线的位置,显然存在没有被回收的内存。若是将代码修改成:开发工具

!function () {
  function Leaker() {
    this.init();
  };
  Leaker.prototype = {
    init: function () {
      this.name = (Array(100000)).join('*');
    },

    destroy: function () {
      // do something....
    }
  };
  document.querySelector('input').addEventListener('click', function () {
    new Leaker();
  }, false);
}()

去掉console.log("Leaking an object %o: %o", (new Date()), this);语句。重复上述的操做步骤,分析结果以下:ui

clipboard.png

从对比分析结果可知,console.log打印的对象是不会被垃圾回收器回收的。所以最好不要在页面中console.log任何大对象,这样可能会影响页面的总体性能,特别在生产环境中。除了console.log外,另外还有console.dir、console.error、console.warn等都存在相似的问题,这些细节须要特别的关注。this

相关文章
相关标签/搜索