具有多个计时器的 NodeJS 应用程序如何扩展?
How does a NodeJS app with multiple timers scale?
我的应用在任何给定时刻最多需要 1000 个计时器。定时器会消耗大量资源吗?部署多个计时器是否是公认的做法?还是我应该避免它?
根据@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');
<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>
我的应用在任何给定时刻最多需要 1000 个计时器。定时器会消耗大量资源吗?部署多个计时器是否是公认的做法?还是我应该避免它?
根据@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');
<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>