Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript performance: global variable vs jquery's $.data()

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.

like image 273
Haroldo Avatar asked Jun 07 '11 08:06

Haroldo


1 Answers

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.

like image 54
bjornd Avatar answered Oct 05 '22 23:10

bjornd