1. Definition and usage
- The console.time()
method is used as the initial method of the calculator.
This method is generally used to test the duration of program execution. - The console.timeEnd()
method is the end method of the calculator and displays the execution time on the console.
If there are multiple places on a page that need to use the calculator, you can add tag parameters to set- Syntax: console.time(label)
- The label can be named arbitrarily, and the label in console.time and console.timeEnd must be consistent
Note: console.time and console.timeEnd should be used together
2. Code example
1. The time required to execute 100,000 times
console.time();
for (i = 0; i < 100000; i++) {
// 代码部分
}
console.timeEnd(); //此处打印结果为总耗时
2. Labeled parameters
console.time("test");
for (i = 0; i < 100000; i++) {
// 代码部分
}
console.timeEnd("test");