开发者

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

开发者 https://www.devze.com 2023-03-10 18:10 出处:网络
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 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 o开发者_如何学Pythonbject, but how does this compare to global variable?

I'm interested the most efficient memory usage.


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.

0

精彩评论

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

关注公众号