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

5
  • Your question does not make sense, can you clarify a bit more Commented Feb 25, 2012 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 ? Commented Feb 25, 2012 at 14:03
  • 100 items in a hash table is nothing. Commented Feb 25, 2012 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 Commented Feb 25, 2012 at 14:07
  • Do what makes your code most clear/usable. Don't worry about performance in this case. Commented Feb 25, 2012 at 14:08

2 Answers 2

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.

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

2 Comments

hey :) thanks - the link is broken ? really interested to read a bit into this if you can fix it ?
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 :)
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.

1 Comment

ah right - yep, just an example thanks for the tip however. Generally, I was going to use a derivation of like myAppSettings etc

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.