Is there any limit to setTimeout?

前端 未结 2 1570
萌比男神i
萌比男神i 2020-12-06 10:50

Specifically talking about (server side) V8, and assuming I\'m not concerned about accuracy because I can detect and compensate for it, could I literally set up tho

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 11:15

    The only real world limit you may come up against is the amount of memory available to node. Use the following code to test. I successfully ran the example below using oneMillion and int32Max. When using int64Max, I received the following error from node. I'm using 64bit windows with 4gb of RAM.

    FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
    

    Node code to test:

    var util = require('util');
    var int64Max = 9007199254740992; 
    var int32Max = 2147483647;
    var oneMillion = 1000000;
    var tenThousand = 10000;
    var counter = 0;
    
    //Exchange the limiter with one of the above vars to test.
    for (var i = 0; i < oneMillion; i++){   
         setTimeout(log, 1);
         //Required as the timeout/callback method will not be called until the loop ends due 
         //to node/js being single threaded.
         util.log('loop:' + i);
    }
    
    function log(){
         util.log('callback: ' + counter++);
    }
    

提交回复
热议问题