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

时间:2016-04-10 05:26:08

标签: node.js performance timer scaling

我的应用程序在任何特定时刻都需要最多1000个计时器。定时器消耗了大量资源吗?部署多个计时器是一种公认​​的做法吗?或者我应该避免它?

1 个答案:

答案 0 :(得分:1)

通过 @ jfriend00 的建议,我在下面做了一个示例检查,这可能不准确(因为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');
&#13;
<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>
&#13;
&#13;
&#13;