測量 JavaScript 程式碼區塊性能的 tip
如果要快速的測量 JavaScript 程式碼區塊性能的話,我們可以使用 console 函式像是
console.time(label)
和 console.timeEnd(label)
。
console.time("Array initialize");
var arr = new Array(100),
len = arr.length,
i;
for (i = 0; i < len; i++) {
arr[i] = new Object();
};
console.timeEnd("Array initialize"); // 輸出:陣列初始化:0.711ms
更多資訊: Console object、 Javascript benchmarking。
範例:jsfiddle - codepen(在瀏覽器 console 下輸出)。
注意:根據 Mozilla 建議,不該使用在上線環境下使用此方法,只能使用在開發環境。
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW