• Node.js Video Tutorials

NodeJS - console.timeLog() 方法



Node.js 的console.timeLog() 方法是 node.js Console 類 的內建方法。此方法用於列印計時器的經過時間和其他資訊。

要使用此方法,我們首先需要呼叫 console.time() 方法來啟動計時器,然後使用 console.timeLog() 方法可以在操作或函式的特定階段建立檢查點。如果在迴圈內使用此方法,則會更有效。

語法

以下是 Node.js console.timeLog() 方法的語法:

console.timeLog([label][, …data]);

引數

此方法僅接受兩個引數。下面將討論這兩個引數。

  • 該方法可以接受帶有特定名稱的引數 label。不同的標籤可用於不同的操作。如果沒有將 label 作為引數傳遞,則將使用預設標籤。

  • 第二個引數data 可以是任何型別的任何資料型別或資料結構。

返回值

此方法將返回從計時器啟動到宣告 Node.js console.timeLog() 方法所花費的時間。

示例

在下面的示例中:

  • 我們正在從輸入字串變數列印隨機元素,直到達到指定的級別。

  • 然後我們使用 Node.js console.time() 方法啟動計時器。

  • 然後我們在 for 迴圈內使用 Node.js console.timeLog() 方法。因此,這將給出每次迭代所花費的時間。

function RandomNum(times) {
const text = "0123456789";
var times = 5;
console.time("Timer");
for(var index = 0; index < times; index++){
      console.log(text[Math.floor(Math.random() * 10)]);
      console.timeLog("Timer");
   }
}
RandomNum();

輸出

0
/home/cg/root/63a050f8537e1/main.js:9
   console.timeLog("Timer");
   ^
   
TypeError: console.timeLog is not a function
   at RandomNum (/home/cg/root/63a050f8537e1/main.js:9:13)
   at Object. (/home/cg/root/63a050f8537e1/main.js:12:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

注意 - 要檢視實際結果,請在本地執行以上程式碼。

正如我們在下面的輸出中看到的,我們得到了迴圈每次迭代所花費的時間。

2
Timer: 3.623ms
9
Timer: 4.082ms
4
Timer: 4.38ms
9
Timer: 4.671ms
7
Timer: 5.502ms

示例

在下面的示例中:

  • 我們正在建立一個函式,並在函式內建立一個 string 陣列,並在其上執行元素的新增和刪除操作。

  • console.timeLog() 方法可以接受兩個引數 ([label][,…data]),在此示例中,我們還在方法中使用了第二個引數。

function func(){
   var value = "seconds";
console.time("Time taken upto adding an  element");
   var array = ["Mahishmati", "Bahubali", "Devsena"];
   array.push("Katappa", "Shivgaami");
console.timeLog("Time taken upto adding an  element", value);
   array.pop();
   console.log(array);
}
func();

輸出

/home/cg/root/63a050f8537e1/main.js:7
console.timeLog("Time taken upto adding an  element", value);
   ^
   
TypeError: console.timeLog is not a function
   at func (/home/cg/root/63a050f8537e1/main.js:7:9)
   at Object.<anonymous> (/home/cg/root/63a050f8537e1/main.js:12:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

注意 - 要檢視實際結果,請在本地執行以上程式碼。

正如我們在下面的輸出中看到的,我們得到了從計時器啟動到向陣列新增元素所花費的時間。

Time taken upto adding an  element: 0.051ms seconds
[ 'Mahishmati', 'Bahubali', 'Devsena', 'Katappa' ]

示例

在下面的示例中:

  • 我們正在建立一個函式,並在其中執行一個簡單的 for 迴圈,在迴圈內部,我們使用了 console.timeLog() 方法來列印控制檯每次迭代所花費的時間。

  • 在函式外部,我們啟動另一個計時器,並在函式呼叫後結束它。因此,它給出了程式總共花費的時間。

console.time("Total time to complete");

function func(){
   console.time("Time taken for every iteration");
   for(var i=0; i<5; i++){
      console.timeLog("Time taken for every iteration");
   }
};
func();
console.timeEnd("Total time to complete");

輸出

/home/cg/root/63a050f8537e1/main.js:6
   console.timeLog("Time taken for every iteration");
   ^
   
TypeError: console.timeLog is not a function
   at func (/home/cg/root/63a050f8537e1/main.js:6:13)
   at Object. (/home/cg/root/63a050f8537e1/main.js:9:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

注意 - 要檢視實際結果,請在本地執行以上程式碼。

正如我們在下面的輸出中看到的,我們得到了 for 迴圈每次迭代以毫秒為單位所花費的時間以及程式總共花費的時間。

Time taken for every iteration: 0.047ms
Time taken for every iteration: 3.823ms
Time taken for every iteration: 4.045ms
Time taken for every iteration: 4.204ms
Time taken for every iteration: 4.357ms
Total time to complete: 4.584ms
nodejs_console_module.htm
廣告
© . All rights reserved.