2

I was wondering whether there is any associated performance problems with using only one namespace or whether assigning more than one is better ?

By this I mean

(function (myApp, $, undefined) {

} (window.myApp = window.myApp || {}, jQuery));

And assign every function / object / property to the myApp namespace or whether it is more efficent from a performance perspective to have myApp for common functions etc and then have

(function (myAppSettings, $, undefined) {

} (window.myAppSettings = window.myAppSettings || {}, jQuery));

And so forth ?

Edit: to clarify the generic settings to myAppSettings per @pimvdb very correct comment

Andy
  • 18,723
  • 12
  • 46
  • 54
  • Your question does not make sense, can you clarify a bit more – Sarfraz Feb 25 '12 at 14:02
  • Hey thanks :) What I mean is - are there any performance issues assign 100 or more functions to the myApp namespace ? If yes, it is better to simply use multiple differing namespace ? Such as `myApp` and `settings` etc etc and assign functions to each respectively ? – Andy Feb 25 '12 at 14:03
  • 100 items in a hash table is nothing. – James M Feb 25 '12 at 14:04
  • @JamesMcLaughlin - but surely it would depend on the complexity of the functions. I have a pretty heavy JS app - with deep object and property tree's - I'm just wondering whether it's better to perhaps remove some from the current `myApp` namespace to another – Andy Feb 25 '12 at 14:07
  • Do what makes your code most clear/usable. Don't worry about performance in this case. –  Feb 25 '12 at 14:08

2 Answers2

1

Generally a shallow tree is better than a deep tree (citation from here). So, depending on the circumstances, it may be better to use more than one namespace. The cited article claims a single namespace is negligible in terms of perfomance though.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • hey :) thanks - the link is broken ? really interested to read a bit into this if you can fix it ? – Andy Feb 25 '12 at 14:04
  • great link. thanks heaps - generally it seems that its OK for now. I'm not `YAHOO` - so I dont think my tree's are as complex as theirs! The last paragraph answered my question :) – Andy Feb 25 '12 at 14:10
0

I don't think there is any difference from a performance point of view. Getting an object property is O(1) in today's implementations.

But a name like settings is so generic you'd better not make it collide with another library's settings. Since these settings are part of myApp, it makes most sense (and it is most safest) to put it in the myApp object.

If every library would be setting window.settings, that would be a big issue.

Community
  • 1
  • 1
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • ah right - yep, just an example thanks for the tip however. Generally, I was going to use a derivation of like `myAppSettings` etc – Andy Feb 25 '12 at 14:04