Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分析代码的执行时间 #26

Open
hfc-advance opened this issue Oct 27, 2018 · 0 comments
Open

分析代码的执行时间 #26

hfc-advance opened this issue Oct 27, 2018 · 0 comments
Labels

Comments

@hfc-advance
Copy link
Owner

console.time()和console.timeEnd()方法

Chrome等浏览器自带一个console.time()和console.timeEnd()方法,能够用更简单的代码实现上述功能。

当需要统计一段代码的执行时间时,可以使用console.time方法与console.timeEnd方法,其中console.time方法用于标记开始时间,console.timeEnd方法用于标记结束时间,并且将结束时间与开始时间之间经过的毫秒数在控制台中输出。这两个方法的使用方法如下所示。

console.time(label)
console.timeEnd(label)

这两个方法均使用一个参数,参数值可以为任何字符串,但是这两个方法所使用的参数字符串必须相同,才能正确地统计出开始时间与结束时间之间所经过的毫秒数。

console.time("Array initialize");
var arr = new Array(1000000),
    len = arr.length,
    i;

for (i = 0; i < len; i++) {
    arr[i] = new Object();
};
console.timeEnd("Array initialize"); // 输出: Array initialize: 266.000ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant