简体   繁体   English

为什么 javascript 执行时间每次都不同?

[英]Why is javascript execution time different every time?

If the code is exactly the same every time it runs, why didn't it use exact same amount of execution time?如果代码每次运行都完全相同,为什么不使用完全相同的执行时间?

For example, code as simple as:例如,代码很简单:

console.time();
console.timeEnd();

would yield different result every time I run it.每次运行都会产生不同的结果。

To understand why it is so, first of all you should know JS works in conurrency model.要理解为什么会这样,首先你应该知道 JS 在并发模型中工作。 There is event loop in Javascript which is responsible for executing the JS code. Javascript 中有事件循环,负责执行 JS 代码。 So this reference will definitely help to answer this question. 所以这个参考肯定有助于回答这个问题。

There are lots of things happening that use memory in your computer while you have your browser open and your script is running. 打开浏览器并运行脚本时,发生了很多事情,这些事情会占用计算机中的内存。 Those things affect how much memory your browser has access to. 这些都会影响您的浏览器可以访问的内存量。 The more memory your browser has available at that time, the faster your script will run. 那时浏览器可用的内存越多,脚本将运行得越快。

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

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