Reactive updates are taking too long


#1

hello everyone,

i have been working on a server for a board game i created for a few monthes now.
each single game is stored in a doc inside the collection “games”.
when a player makes a move, it calls a method that applies the logic of the game and changes the database doc.
this change triggers an autorun function inside each of the player’s clients, that function draws the new things that were added to the board. now, this whole process of making one move is taking too long.
i planted console logs with (new Date).getTime()); to get a grip of where exactly the whole thing is getting stuck.
*note - i am running the app on my local host on a pretty new and decent pc.
the whole process is taking 600 ms. one thing that immediately got this down to 400-500 ms is to place the whole makeTurn method (that is placed in the server folder) inside an if (Meteor.isServer). before then, the method will first run in the client of whoever made the turn and just then start running on the server. (i don’t really understand the idea behind that behavior) anyway having fixed that, for the rest of the process. from the moment the player pressed where ever he wanted to make his move, it only takes up to 10ms until the server gets the call and start running the method, which takes up to 40ms to finish at where it updates the games collection once with all the new data(the last server console log is right after that). the autorun that draws new elements into the canvas takes up to 100 ms. that’s 150 ms altogether which is already quite alot and means i need to optimize it all. but the thing that takes the majority of the time(350 ms) is from the moment the server finished running the method until the autorun in the clients only begins to run. i have no idea why this delay in which nothing is being ran is happening, i also tested other methods for example the chat messages method where there is no such delay at all (only 10ms from when the server finishes running the method until the message is already shown on chat for everybody), although the chat isn’t using an autorun but only helpers to stack all relevant messages from the messages collection into the html.
another interesting fact is that on firefox the 350ms delay is doubled to 700. (i’m using chrome normally)

i was hoping someone could help me figure out the cause for the delay, or link some tools that can help me figure it out.
thanks in advance!

edit: i just made another experiment by subscribing to the games collection on a different page and using a helper that displays the amount of turns that were played so far in the game(that number is updated (++) to the database in the end of each turn. that helper takes no time to reactively update after a turn is made on the game page! that means that the problem is solely caused by the autorun function which is only triggered after a long delay.