- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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
