简体   繁体   English

为什么 console.time 显示的时间比实际时间短得多?

[英]Why does console.time show much lower time than the actual time?

I am testing a snippet of JavaScript's speed using the console.time();我正在使用console.time(); method in Chrome and Chromium-based Edge. Chrome 和基于 Chromium 的 Edge 中的方法。 When I'm running the snippet in the browser, it takes at least 2 seconds, but the displayed result of time is around milliseconds, What is happening here?当我在浏览器中运行代码片段时,至少需要 2 秒,但显示的time结果大约是毫秒,这里发生了什么?

 var list = [...Array(50000000).keys()]; console.time("time"); var x = 0; for (let i = 0, n= list.length; i<n;i++) { x++; } console.timeEnd("time");

I've tested this with performance.now() and I get the same result as time , which is really confusing however it works perfectly fine in Firefox.我已经用performance.now()对此进行了测试,得到的结果与time相同,这确实令人困惑,但是它在 Firefox 中运行良好。
Is this a Chrome/Edge bug?这是 Chrome/Edge 错误吗? if it is, How can one submit an issue for chrome?如果是,如何提交 chrome 问题?

Update更新

Thanks to @vlaz and @Jax-p I found out that I've missed the array generation time :感谢@vlaz 和@Jax-p,我发现我错过了数组generation time

 console.time("time");
    var list = [...Array(50000000).keys()];

    var x = 0;
    for (let i = 0, n= list.length;   i<n ;i++) {
      x++;
    }
    console.timeEnd("time");

Creating and spearding array with [...Array(50000000).keys()];使用[...Array(50000000).keys()];创建和扩展数组takes over 1.5s in my Chrome but you have timer start after that (You had. Before you change the question.) .在我的 Chrome 中占用了 1.5 秒,但在那之后你有计时器启动(你有。在你改变问题之前。) I am getting same (a little bit higher) results in Firefox.我在 Firefox 中得到相同的结果(稍微高一点)。 Can it be it?可以吗?

Suppose there is also a delay before the browser even gets to execute JavaScript.假设在浏览器执行 JavaScript 之前也有延迟。

 console.time("time"); var list = [...Array(50000000).keys()]; console.timeEnd("time"); console.time("time2"); var x = 0; for (let i = 0, n= list.length; i<n;i++) { x++; } console.timeEnd("time2");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM