Search code examples
node.jsperformancetimerscaling

How does a NodeJS app with multiple timers scale?


My app will require up to 1000 timers at any given moment. Do timers consume a lot of resources? Is it an accepted practice to deploy multiple timers? Or should I avoid it?


Solution

  • By @jfriend00's suggestion, i made a sample check below, this might be not accurate (cuz of dom manipulations), but hope it gives you concept

    // let's use 2 measurings, window.performance.now and console.time
    
    // start console.time, usually it gives same result as window.perf.now
    // but window.perf.now uses 10 decimal points
    console.time('timer_perf_test');
    var now1 = window.performance.now();
    
    // our count, this test really lags when 100,000
    // CHANGE THIS 
    var count = 10000;
    
    // some dom elements for text
    var elem = document.querySelector('img');
    var counter = document.querySelector('#counter');
    var perf = document.querySelector('#perf');
    
    // how smooth our image gonna rotate?
    var rotate = function(degree){
        elem.style.transform = 'rotate(' + degree +'deg)';
        counter.textContent = 'timers executed: ' + degree;
    }
    
    // create bunch of timers with different timeout
    var timer = function(added_time){
        setTimeout(function(){
        	rotate(added_time);
        }, 1000 + (added_time * 10));
    }
    // test begins
    for (var i = 0; i < count; i++) {
        timer(i)
    }
    
    // check results
    var now2 = window.performance.now();
    perf.textContent = now2 - now1 + ' MS required to create ' + count + ' timers';
    console.timeEnd('timer_perf_test');
    <img src="https://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT4211/HT4211-ios7-activity_icon-001-en.png" width="100" height="100">
    <p id="counter"></p>
    <p id="perf"></p>