I have a small game in HTML5 that uses socket.io to communicate with a node.js server. Now my problem is that, ever since I did my last update on it it seems to have something "chunk up" in the background making it laggier and laggier the longer it runs. In the update were a few temporary local variables being defined with var(you know, variables that are only used during one function and then not needed anymore) alongside with a lot of other changes. The update actually featured a new jumping system that allowed players to jump higher when holding down the jump button, and that's it. I am not even sure if this update or something else is causing this. Might the "var" have caused it? Or what other reasons might this strange complication have?

EDIT: Some stuff that I think might be the reason for this and some details:

  • Temporary variables defined with var.
  • I prerender some graphics in the background, however that only happens once, and that's when the game starts.
  • The map is infinite, being generated on as you move through it. When it then receives a chunk from the server it keeps that chunk stored in the client, however it also seems to get laggier and laggier even when you stay in the same chunk, not loading any new chunks from the server.
  • It has a chat which basically puts a new piece of text into a div every time it receives a message, however it also got progressively laggier when I was alone on the server(in other words: I received no chat messages)
  • 1
    \$\begingroup\$ Post your code or else we won't be able to answer beyond wild speculation (which will probably all be wrong) \$\endgroup\$ – doppelgreener Jul 10 '12 at 11:34
  • \$\begingroup\$ You sure about it? It's 32 KB of code, I really dont want to "force" anyone to read that much code. What I acctually wanted was some sort of list or something of things that cause such behaviour in general. \$\endgroup\$ – Wingblade Jul 10 '12 at 12:03
  • \$\begingroup\$ "eversince I did my last update on it" - you could start with, what you changed in this update \$\endgroup\$ – Maik Semder Jul 10 '12 at 12:35
  • \$\begingroup\$ You could post the source and direct us to exactly what you changed (assuming you don't mind us looking at it). \$\endgroup\$ – Tharwen Jul 10 '12 at 16:37
  • 3
    \$\begingroup\$ Profile your code. Google Chrome has a useful profiler built in, and FireBug is a great set of developer tools available for FireFox. \$\endgroup\$ – notlesh Jul 10 '12 at 16:39

As Jonathan said, this is purely speculation. When I heard about "things getting laggier and laggier" the first thing that came to mind was a memory leak. I don't know how applicable this may be to your specific application, but my guess is that it is some sort of data leakage - data not being cleared when it should be.

  • \$\begingroup\$ Err, what about the garbage collector? (AFAIK, a memory leak only happens when allocated memory doesn't have a reference to it, so the programmer would have to release it manually, unlike in higher-level languages like Javascript, which automatically clear unreferenced memory.) \$\endgroup\$ – jcora Jul 10 '12 at 17:14
  • 1
    \$\begingroup\$ @Bane, even memory with references can be classed as leaks in Javascript. These are caused by a circular reference that often ends up being impossible to delete because often you've gone out of scope. Useful link on memory leaks ibm.com/developerworks/web/library/wa-memleak \$\endgroup\$ – John Jul 10 '12 at 18:32

This is something you simply have to test:

  1. Find a working revision.

    • Start by reverting your version to the one before the update or the one where the roblem doesn't appear (you're using version control right, then this is very easy) and verify that the earlier version of the code doesn't have this problem.
    • Never assume anything, always verify
  2. Pin-point the revision to blame

    • Once verified, go through the versions until you find the one where the issue appears. If you as you say, have only one new version, that one is likely to blame. If not and it's hard to guess which revision caused the issue, use a binary search approach ^^
  3. Find the code-change in the blamed revision to blame

    • Revert to the previous version and start adding the changes from the next version again, one by one, until the problem appears again. Either do it file by file, function by function or in any other suitable chunks you can think of that still runs.

Once you find the file or chunk you can then narrow it down to a function or in some cases a single line of code.

Now figure out why that part of the code causes the problem and fix it - it could be a memory leak or thousands of other things, but it's almost impossible to guess without homing in on where the issue appeared.

And then post that part of the code ^^


Another wild guess: perhaps you are not removing objects correctly and your collision detection gets out of control. Brute force collision detection scales very poorly.

  • \$\begingroup\$ what do you mean by brute force collision detection? \$\endgroup\$ – Wingblade Jul 10 '12 at 16:58
  • \$\begingroup\$ Maybe you're checking every object against another one - twice. That's what most new people are doing. It's O(N^2), and thus is kinda inefficient, although I doubt that will be a problem until you have like 50+ objects. \$\endgroup\$ – jcora Jul 10 '12 at 17:16
  • \$\begingroup\$ Well I have objects, which are players. Those don't need collisons yet, so they dont have any. Then I got my Platforms, which are described by 2 x values and one y value, and I basically check if a player "collides" with the line described by those values my platforms have. \$\endgroup\$ – Wingblade Jul 10 '12 at 17:37
  • \$\begingroup\$ Again, you should use a profiler, as I suggested above. You'd be able to figure out what's wrong in far less time than you've spent asking questions here. Beyond that, it might be useful to track the number of objects (and other interesting stats, like number of collision checks) vs. the current framerate. \$\endgroup\$ – notlesh Jul 10 '12 at 17:59
  • \$\begingroup\$ I can try that, however answers I get on here can be useful for my later projects or in case I don't manage to improve anything after profiling. \$\endgroup\$ – Wingblade Jul 10 '12 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.