DA.
DA.

Reputation: 40673

Rule of thumb regarding large JS variables and keeping them 'in memory'?

This may be a naive question. But I hope not. :)

I'm working on an JS web app (eventually to be placed into PhoneGap). It's a one page game.

It's a word game so I have a few variables that are large(ish?) arrays to store dictionary content in for look ups.

My questions:

  1. Is there any rule of thumb as to what is 'too big' of an array to keep in memory? My biggest array is 3000 items (consisting of small text strings). Seems to run fine. I'm guessing that's not considered all that big.

  2. If it is too big (or when a variable is too big) is there a proper way in JS to 'unload' that variable from memory until it's needed later?

I have a hunch that my real-world example of 3000 item array isn't a cause for concern and is me worrying too much about micro-optimization but I am still interested from a general theoretical standpoint: When variables are too large to practically keep in memory, is there a proper way to load/unload them as needed in JS?

UPDATE:

I'm thinking now this question is perhaps naive the more I think about it. :)

Is it fair to say that if I declare a variable, no matter where in my JS code, it, by default, is already 'in memory' as all my JS has to be in memory to run. As such, there really is no loading/unloading of variables. They're either there or they are not?

Upvotes: 1

Views: 138

Answers (2)

Atif
Atif

Reputation: 1220

You can use localStorage because it saves your value in cache sqlite and if it unload by memory warning then it can be reload from sqlite. Plus when you restart your app its comes again in mobile platform.

http://www.w3schools.com/html/html5_webstorage.asp

Upvotes: 1

user1329482
user1329482

Reputation: 569

I'm not sure I know what the limits of each environment are likely to be, but as regards to loading new data, you can use JSON/P (or some variant, depending on whether you have an active server-side component) to bring in new data as required. Unloading old data is as simple as setting the current reference to null.

If you don't have an active server-side component you can use iframes to load data.

Upvotes: 1

Related Questions