📜  Node.js console.profile() 方法

📅  最后修改于: 2022-05-13 01:56:46.543000             🧑  作者: Mango

Node.js console.profile() 方法

控制台模块提供了一个简单的调试控制台,由导出两个特定组件的 Web 浏览器提供:

  • 一个控制台类,可用于写入任何 Node.js 流。示例: console.log()、console.error()等。
  • 无需导入控制台即可使用的全局控制台。示例: process.stdout、process.stderr等。

console.profile()在 v8.0.0 中添加)方法是“控制台”模块的内置应用程序编程接口,除非在检查器中使用,否则不会显示任何内容。它使用可选标签启动 JavaScript CPU 配置文件,直到调用console.profile() 。然后将配置文件添加到检查器的配置文件面板中。

注意:全局控制台方法既不是始终同步也不是始终异步。

句法:

console.profile([label])

参数:此函数接受如上所述和如下所述的单个参数:

  • label < 字符串 > 它接受将在检查器中进一步使用的标签名称。

返回值:它不在控制台中打印任何内容,而是在 Inspector 中启动 JavaScript CPU 配置文件。

下面的示例说明了在 Node.js 中使用console.profile()方法。

示例 1:文件名:index.js

// Node.js program to demonstrate the 
// console.profile() Method
  
// Starting MyLabel console profile
console.profile('MyLabel');
  
// Doing some task
for (var i = 0; i < 4; i++) {
  
  // Printing some task
  console.log('Doing task no:', i);
}
  
// Finishing MyLabel profile
console.profileEnd('MyLabel');

使用以下命令运行index.js文件:

node index.js

控制台中的输出:

Doing task no: 0
Doing task no: 1
Doing task no: 2
Doing task no: 3

检查器中的输出:

检查器中的输出

示例 2:文件名:index.js

// Node.js program to demonstrate the 
// console.profile() Method
  
// New profile function
function newProfile(callback) {      
  try {
      // Doing some task
      for(var i = 1; i < 4; i++) {
         console.log('Working on task:', i);
         callback();      
      }
    } catch {
  
      // Prints if there is error
      console.error('error occured');
    }
}
  
// Starting newProfile() console profile
console.profile("newProfile()");
  
// Calling newprofile()
newProfile(function alfa() {
  
  // Finishing profile
  console.profileEnd();
});

使用以下命令运行index.js文件:

node index.js

控制台中的输出:

Working on task: 1
Working on task: 2
Working on task: 3

检查器中的输出:

检查器中的输出

参考: https://nodejs.org/api/console.html#console_console_profile_label