代码之家  ›  专栏  ›  技术社区  ›  manidos

具有多个定时器的NodeJS应用程序如何扩展?

  •  0
  • manidos  · 技术社区  · 8 年前

    我的应用程序在任何给定时刻都需要多达1000个计时器。计时器消耗大量资源吗?部署多个计时器是公认的做法吗?还是我应该避免?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Medet Tleukabiluly    8 年前

    通过 @j朋友00 根据我的建议,我在下面做了一个示例检查,这可能不准确(因为dom操作),但希望它能给你概念

    // 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>