3

SO i totally understand why we should namespace in javascript, but is it mainly for organizational purposes and to keep the global namespace uncluttered?. Does namepacing actually help in performance of the browser/JS engine. Just wondering what your thoughts were. Thanks

Moak
  • 12,596
  • 27
  • 111
  • 166
29er
  • 8,595
  • 12
  • 48
  • 65
  • possible duplicate of http://stackoverflow.com/questions/2102591/namespacing-technique-in-javascript-recommended-performant-issues-to-be-aware – Tim Abell Jun 15 '12 at 13:39

2 Answers2

4

It technically hinders performance but not much, depending on how deep you start going. Silobox (http://www.silobox.com/) is a JavaScript performance benchmarking tool and we wrote a test that tests this very thing. We found that the deeper an object was nested, the longer accessing those properties took.

So for optimum speed, I recommend adding global shortcut functions to your code.

Assume you have:

var mycompany.myproj.Something = function(){ ... };

It's good practice to include

var MCSomething = mycompany.myproj.Something;

That way, when the JS engine looks up MCSomething, it doesn't have to climb down any trees. Google Maps uses this approach.

Mat Ryer
  • 3,797
  • 4
  • 26
  • 24
  • thanks for great answers. i will definitely utilize the global shortcut functions :) – 29er May 07 '10 at 16:57
3

There shouldn't be any noticeable affect on performance from using namespaces. The primary reasons are just what you mentioned: keeping the global namespace clean (to avoid naming conflicts) and organization.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129