开发者

What is the best way to deal with server side data saved as a javascript variable

What is the best way to handle global variables when they are necessary. For example JSON that is inserted server side with PHP into the web page. var data = <?php getData() ?>

There seems to be 3 options.

1 leave it for garbage collection

2 leave off the var initialization of the variable and delete开发者_运维问答 the variable

3 just set the variable to null and again leave it to GC.

Or there may be some other way that I haven't thought of I am open to suggestions. Though I would prefer not to use ajax in this situation. Also, it is not feasible to insert the variable data inside of the actual javascript code because I minify it.

EDIT: The reason I am asking is the data could be potentially large and I was wondering what is the best way to deal with it. Also, I am not sure how long garbage collection takes; so maybe it is really a non-issue. I don't know?


Imagine that var data = {...} was embedded in the JavaScript and did not come from PHP -- where would be an appropriate place to put that?

This doesn't change and generally should be "smallest applicable scope" which may very well be a global window property -- although I would generally argue against this as I favor "modules" or other designs.

There are some potential issues to watch out for wrt. closures, unintentional bindings and very (100MB+) large structures. If the data is loaded-once then not needed anymore then, by all means, let the object become unreachable (through scope termination -- my preferred approach -- or assigning null to the variable referencing it, etc) but for the most part:

We should forget about small efficiencies, say about 97% of the time -- Knuth

Happy coding.

Remember: delete does not destroy an object and variables are not GC'ed (they can, however, keep an object reachable). Objects are only GC'ed when:

  1. The object is no longer reachable and;
  2. the GC feels like it.


Since no global variables get recycled by the GC, even if not being used by any function, you will have to delete data; or delete window.data; if out of scope


If the requirement is that you have to ask the server for the whole chunk of information then you have three options.

1: You can use delete

2: you can write this info into a structured DOM element and have the JS get it within a particular scope (which would then be GC'ed later), i.e., div id="somefoo" class="invisible-div"><?php getData('somefoo'); ?></div>, then: var somefoo = $('#somefoo').text();

this solution requires you to parse this info and structure it before it gets to the JS though. Depending on your needs this may or may not be a good thing.

3: ajax call.

The meta concern is that you don't want this data floating around after you've done what you wanted with it, and that there's no memory leak.

Or perhaps this isn't even much of a concern? I would think that you would only run into GC concerns if getData() returns a huge amount of data. Usually js GC collection concerns have to do with the memory allocated in the course of running your script, not in the input data. You may want to use, for example, the code profiling tool in google chrome, which will tell you your js memory usage. -For example, just testing it out, it tells me that gmail's memory usage was 36MB. You should evaluate what the acceptable bounds of memeory usage is, how much getData() takes up and go from there. Who knows, you may not even have to GC it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜