Easy-to-read code is key to my problem here, so I will follow a modular approach, even if there is a slightly longer search chain for variables and functions. Any method seems to have pros and cons as it is.
On the one hand, you have a global scope, which from the very beginning contains several variables. If you put all of your code in a global scope, your list of variables in that scope will include your own as well as variables such as innerWidth and innerHeight. The list for searching with references to variables will be longer, but I think this is such a small amount of overhead, which is ridiculous to worry about.
On the other hand, you can add only one object to the global scope, which contains all the variables that you want to work with. From within this area, you can easily reference these variables and avoid searching for global variables such as innerWidth and innerHeight. Con is that when you access your encapsulated variables from the global scope, you will have a longer search chain, such as: globalscope.customscope.myvar instead of globalscope.myvar or just myvar.
When I look at it like this, it seems like a very trivial question. Perhaps the best way to do this is simply to encapsulate things that combine only for the declarative code, and focus on readability, rather than the uselessness of unreadable code, which is only slightly more efficient.
Frank source share