當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js console.profile()用法及代碼示例

控製台模塊提供了一個簡單的調試控製台,由Web瀏覽器提供,該控製台導出兩個特定的組件:

  • 一個控製台類,可用於寫入任何Node.js流。例如:控製台.log(),控製台.error()等。
  • 無需導入控製台即可使用的全局控製台。例如:process.stdout,process.stderr等。

console.profile()(在v8.0.0中添加)方法是“控製台”模塊的內置應用程序編程接口,除非在檢查器中使用,否則不會顯示任何內容。它會以可選標簽啟動JavaScript CPU配置文件,直到調用console.profile()。然後將配置文件添加到檢查器的“配置文件”麵板。

注意:全局控製台方法既不是始終同步的,也不是始終異步的。

用法:

console.profile([label])

參數:該函數接受如上所述和以下描述的單個參數:



  • label <string>:它接受在檢查器​​中進一步使用的標簽名稱。

返回值:它不會在控製台中打印任何內容,而是在Inspector中啟動JavaScript CPU配置文件。

以下示例說明了Node.js中console.profile()方法的使用。

示例1:Filename: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:Filename: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




相關用法


注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js console.profile() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。