11

I need to store relatively larget bit of JSON for global access in my web app.

Should I use jquery's $.data(document.body, 'some-reference-here', MyJsonObj); or a global?

I know binding $.data() to document.body is faster than to a jquery object, but how does this compare to global variable?

I'm interested the most efficient memory usage.

11
  • 2
    Woah, watch out son, those 2ms speed differences will make your application way too fast. Dont pre optimize, and when you do optimize, profile first and start with the slowest part. Commented Jun 7, 2011 at 8:48
  • I'm working on a mobile web-app, where the device's memory is in real short supply... Commented Jun 7, 2011 at 8:58
  • @Petah funny - I got flamed last week for daring to suggest that a 2ms page setup lag was fine when a harder-to-understand version could do it a hundred times faster... Commented Jun 7, 2011 at 8:59
  • 1
    @Haroldo using $.data() will result in a negligible amount of extra memory compared to a global variable, since ultimately all that $.data() does is store a reference to a variable (which could even be your global) in a DOM element. Commented Jun 7, 2011 at 9:00
  • 1
    @AlexeiAverchenko it doesn't appear to - I just tested Chrome and an HTML data attribute would set the initial value of .data() but changes to that data did not then appear in the HTML data attribute. Commented Jun 28, 2012 at 14:11

1 Answer 1

7

Global variable in browser JS means window.variable, so I think it would be much faster then $.data(document.body, 'some-reference-here', MyJsonObj); just because this is only one touch of the object's property instead of function call, getting property of document and much staff inside of the data call. But another problem is polluting global scope. Maybe it's better to store this data somewhere inside the local scope of your script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.