On my server side I use writable stream to record the changing data of all the clients that are connected to the server. Here is what I wrote:
function updateLoop () {
var data = {'timeStep': timeStep, 'playerInfo': playerInfo};
var text = JSON.stringify(data);
writeStram.write(text + '\n');
timeStep += 1;
}
...
updateTimer = setInterval(updateLoop, 50);
So the function updateLoop() is called every 50ms.
It works when ‘playerInfo’ is small (less than 1KB), for example, after the server side running for 20s, there are 20*1000/50 = 400 lines of data in the output file
But when ‘playerInfo’ became larger, like 6KB or more, after the server side running for 20s, there only 220 lines of data in the output file. The larger playerInfo is, the lesser lines of successfully recorded data in the output file there is.
I am wondering if there is some limit to the calling rate of the method write()
Thanks a lot for your guys’ help!
The issue likely isn’t specifically with
write, but withsetIntervaland how timers work in JavaScript.With the larger data,
updateLoopis probably taking longer to finish executing than the set delay of 50 ms. If it take long enough that the intervals overlap each other, where one would be added to the event queue when another is already waiting, the newer interval is skipped (or “dropped”).writeshouldn’t explicitly throttle performance, but it can be limited by the overall performance Node can achieve on your computer (affected by CPU, RAM, etc.).You’ll have to benchmark and compare the performance of
updateLoopwith varying amounts ofplayerInfo. There are a few packages you can use for this, includingben: