Storing All Loaded Data Onto One Javascript Object

Go To StackoverFlow.com

1

I just need some advice on if this is wise. I'm loading my site data largely through AJAX requests. To save load time and server calls I'm saving all data loaded to global objects and just recycling that data if the user selects the page again.

I wondered if it is wise to cache an entire site onto ONE object, e.g.:

var page = ['level 1','level 2','level 3','level 4'];
var mysiteobj = [];
if (page[0] == 'level 1')
   if (!mysiteobj['level 1'])
      $.ajax(/*load data*/).done(function(data){ mysiteobj['level 1'] = data; /*display data*/ });
   else /*display mysiteobj['level 1']*/
   if (page[1] == 'level 2')
      if (!mysiteobj['level 1']['level 2'])
         $.ajax(/*load data*/).done(function(data){ mysiteobj['level 1']['level 2'] = data; /*display data*/ });
      else /*display mysiteobj['level 1']['level 2']*/
 ...etc

...and so forth. To clarify the rather odd notation, 'level 1' is all the geographic areas a user might look up, 'level 2' is all the departments in the geo the user might look it, 'level 3' would be the categories in the department in the geo, and say 'level 4' would be the product or entry listed in the category in the department in the geo.

Right now, my method is, I have an object with all the geographic areas, a second object is overwritten with a new department list every time a new geo loaded, and a third object holds my categories and fourth my listings/products.

If this makes sense, please advise. I guess the question simplified is, how large can you make an object before it becomes unwieldy and bogs down the browser?

2012-04-04 01:23
by Phil Tune


2

Why not just load the data as needed, if not loaded already.

For example, you can load level 1.

Then, when the user selects level 2, load that if not already there, and continue.

So, you can keep what was already loaded, but just inform them that you are retrieving the data, if it takes time, otherwise people may understand that you are loading it.

Personally, I would show the user that there is a load request, so they know what is going on.

Preloading the entire site is a waste, IMO.

2012-04-04 01:28
by James Black
you can "preload" everything when the page or user is idle though. like say when browsing page 1, load the others in the background - Joseph 2012-04-04 01:29
@Joseph - You can, but then you are making many calls that may not have any benefit to the user, and if they are on a mobile device, and paying for their bandwidth, then it would be a problem. Also, this will be using up memory, again, if the user only looks at one part in level 2, then loading everything from level 2 and down when the majority isn't going to be used would be annoying - James Black 2012-04-04 01:33
good point there. i never thought about mobile - Joseph 2012-04-04 01:36
No, I don't mean preloading the whole site, I mean storing the data onto a single object instead of using multiple objects or overwriting data. The data would "cache" onto a single object instead. Is this recommended - Phil Tune 2012-04-04 02:41
@philtune - I wouldn't put it into one object, as traversing it will be slower. I would use a separate array on each level, so if you have four levels, then four arrays of objects. And store an object with the name, or identifier of that data, and then the data itself - James Black 2012-04-04 12:15
Okay, that's the answer I was looking for. Thanks @JamesBlack - Phil Tune 2012-04-04 15:22


0

Considering that it's never wise make ajax requests returning large data sets....

I think you can storage this data into a Redis/Memcached server and make a javascript function that catch exactly what you want. It's not wise put that amount of data into a global variable and make your user pay for that.

2012-04-04 01:35
by Igor Escobar
That's what I meant. It will cache data the user has already loaded into one global object. I don't know if this is wise. Are there limitations to object size? The alternative was making separate objects or allowing data to be overwritten on the object (meaning it won't cache EVERYTHING) - Phil Tune 2012-04-04 11:36
Ads