Note that there are some explanatory texts on larger screens.

plurals
  1. POIs the Monolithic God object in Javascript a performance concern?
    primarykey
    data
    text
    <h2>The way it is:</h2> <p>I have recently joined a webapp project which maintains, as a matter of standard, one single globally-available (ie, itself a property of <code>window</code>) object which contains, as properties or recursive sub-properties, all the functions and variables necessary to run the application — including stateful indicators for all the widgets, initialization aliases, generic DOM manipulation methods — <em>everything</em>. I would try to illustrate with simplified pseudo-code, but that defeats the point of my concern, which is that this thing is cyclopean. Basically nothing is encapsulated: everything single granular component can be read or modified from anywhere.</p> <h2>The way I'd like it:</h2> <p>In my recent work I've been the senior or only Javascript developer so I've been able to write in a style that uses functions, often immediately invoked / self-executing, for scoping discreet code blocks and keeping granular primitives as variables within those scopes. Using this pattern, everything is locked to its execution scope by default, and occasionally a few judiciously chosen getter/setter functions will be returned in cases where an API needs to be exposed.</p> <h2>…Is B more performant than A on a generic level?</h2> <p>Refactoring the code to functional parity from style A to style B is a gargantuan task, so I can't make any meaningful practical test for my assertion, but is the Monolithic God object anti-pattern a known performance monster compared to the scoped functional style? I would argue for B for the sake of legibility, code safety, and separation of concerns... But I imagine keeping everything in memory all the time, crawling though lookup chains etc would make it either an inherently performance-intensive exercise to access anything, or at least make garbage collection a very difficult task.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO"Is B more performant than A on a generic level?" - this question doesn't even make sense from a performance engineering standpoint. I don't claim to be an expert in the issue, but I know enough that you answer the question "Is X performant enough?" and "Is X more performant than Y?" by measuring X (and Y) and comparing the numbers, instead of eyeballing the code.
      singulars
    2. COConsider that your app is running in a browser, manipulating a complex DOM with *years* of cruft in it which probably takes up orders of magnitude more memory than your entire Javascript codebase. The slowest things it does are probably: AJAX calls, and redrawing. I'm guessing code layout / style changes won't even register, and the performance improvements you hope for are imaginary, especially since tracing JITs can probably take care of optimizing property lookups and such away.
      singulars
    3. CO@millimoose yes, I acknowledge that without providing side-by-side measurable code, there's no procedural true/false answer (I did acknowledge the problem of direct analysis in the question). I'm also aware that the really performance-taxing stuff lies in DOM query & manip, next to which internal memory management concerns are small potatoes. But the last line suggests a 'no' answer. Thanks for your contribution!
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload