0

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.

yqlim
  • 6,898
  • 3
  • 19
  • 43
  • 5
    “Supposedly if the code is exactly the same every time it runs, it should use exact same amount of time to execute.” According to whom? This just isn’t true. – Ry- Mar 01 '17 at 04:36
  • can u tell what is the statement you are executing between time and time end functions ? – Saurabh Verma Mar 01 '17 at 04:36
  • It will changed based on the capacity of browser. If browser occupy only 50 mb of ram, the code fill execute quickly. If it occupy 300 mb of ram, it will execute slowly – Sagar V Mar 01 '17 at 04:38
  • There are hundreds of other processes running on you PC, so it is really unlikely that the result is the same. Event if your javascript interpreter would be the only process on your PC the execution time could vary. – t.niese Mar 01 '17 at 04:41
  • This is only true if you run javascript directly on your CPU without any OS or other programs running. For OSes like Windows, MacOS and Linux it is not enough to stop all other programs running. There will still be services and device drivers that run periodically that can affect your timings. You will need to remove the OS itself or run under a real-time OS like VXworks or RTkernel. Interestingly, since DOS is not really an OS in the modern sense of the term OS you can also run under DOS to get consistent timing – slebetman Mar 01 '17 at 04:48
  • 1
    @slebetman even if code would run directly on the cpu without any os the time could vary. The differences will become smaller but they will still exists, thermal differences or fluctuations in voltage can also affect the execution time. It will be just shifted into a smaller domain. – t.niese Mar 01 '17 at 05:03
  • @t.niese: Thermal and voltage fluctuations will affect execution time the same way they affect the clock (indeed, it is the clock that is affected) so the timing will be consistent. It just won't be consistent with your stopwatch but what's printed on stdout will be consistent since each time you execute will take the same number of clock cycles (it's just the clock cycles get stretched and squished) – slebetman Mar 01 '17 at 07:14

2 Answers2

0

To understand why it is so, first of all you should know JS works in conurrency model. There is event loop in Javascript which is responsible for executing the JS code. So this reference will definitely help to answer this question.

Lily
  • 1
-2

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.

Jack Pilowsky
  • 2,275
  • 5
  • 28
  • 38
  • 2
    Even if the computer would have an endless amount of memory to access the execution time would still vary. A certain amount of memory is required so that the execution of code is not slowed down because of page swapping. But after that point more memory would not make the code run faster if you add more memory. – t.niese Mar 01 '17 at 04:47