Brams wrote:
> Now I'm faced with two options on where to store this huge JSON array:
>
> 1- In a JavaScript global variable
> 2- Or using the $.data function and store the array under some DOM element
> i.e. $('body').data("my_array", {.....});
>
> Which is one is better? are they the same? is one slower over the other?

There would definitely be a performance hit to using jQuery.data.
This is not likely to be a big deal unless you are accessing it a
great number of times.  But it has no advantages I can see.

> What's the best practice in this case?

The namespace suggestions listed so far are definitely better than
making this a unique global variable.  A third option, though, when
possible, would be to store it along with the functions that need
access to it in a closure.

var BRAMS = BRAMS || {};

  (function() {
    var myBigArray;

    BRAMS.loadArray = function() {
      // load array from whereever
      myBigArray = // that new array
    };

    BRAMS.func1 = function() {
      // use myBigArray here
    };

    BRAMS.func2 = function() {
      // use myBigArray here
    };

    // ... etc.
  }());

Then you have the necessary public functions stored on your one global
object, but the array is not available to any functions which don't
need to know about it.

Depending on your use cases, this might be overkill, but for me,
reducing the scope of variables is quite important.

  -- Scott

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to