JS game variable holding big data

Recommended Posts

I'm making an HTML5 game where I declare a global variable

var route = [];

that is used to hold mouse coordinates in form of array of objects. After couple of minutes of playing there are already thousands of objects there. I didn't notice any lags or slowing down but maybe that's because the game is very simple without complex graphics. Should I worry about that variable? What is a general rule in game design when it comes to variables that are constantly growing. I cannot clear this variable because I need all of thiese coordinates throughout a game. I wouldn't want to end up with a game that has a "bug" that makes it impossible to play after a while due to memory overload.

Share this post

Link to post
Share on other sites

It depends how precise you want the mouse data history. Perhaps there could be a greater interval between every record of mouse info so you will end up with less entries in the array.


You also mentioned that there are 'thousands of objects' within a couple of minutes. I'm not sure if you mean object by just an entry or object as in an instantiated object/class with more info than, perhaps, actually needed.

What i mean is if you only need some small numbers of, for example, mouse coordinate numbers, you could also use one of the TypedArrays.

It seems that you append 'route' with 'New MyClassNameWithMousePosInfo()' objects which seems a bit overkill. If you tend to keep situations like these as small and simple as possible you will probably have less problems with memory or performance issues. Even tho you don't have any of those now they may certainly be present on systems with less computing power/resources or when you project grows further.


Good luck tho ;)

Share this post

Link to post
Share on other sites

Thanks a lot for feedback. I need to think whether I really need all of the coordinates to be stored there. In the meantime I separated the module of this game and put it in JSfidle:

You can see in a console  after you uncomment the


at the bottom how it grows rapidly after you draw some lines.

Share this post

Link to post
Share on other sites

Using some run-length compression on the coordinates (one string for x, another for y) may result in some memory savings.

And / or storing the mouse movement delta (rather than the actual coordinate) may reduce further.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.