how many javascript setTimeout/ setInterval call can be set simultaneously in one page?

前端 未结 4 496
别那么骄傲
别那么骄傲 2020-12-01 07:45

I have to use atleast 2 setTimeouts and 1 setInterval. Does this have any dependency on the browser or javascript engine being used?

4条回答
  •  独厮守ぢ
    2020-12-01 08:35

    tl;dr: Don't worry about the cost of timers until you're creating 100K's of them.

    I just did a quick test of timer performance by creating this test file (creates 100K timers over and over):

    
    

    I opened this file in Google Chrome (v54) on my circa ~2014 Macbook Pro, and went to the Timeline tab in Developer Tools and recorded the memory profile as the page loaded and ran thru 3-4 cycles of the test.

    Observations

    The timer creation loop takes 200ms. The page heap size starts at 3.5MB pre-test, and levels out at 3.9MB.

    Conclusion

    Each timer takes ~.002 msecs to set up, and adds about 35 bytes to the JS heap.

提交回复
热议问题