For a rather large framework I'm wondering what I should do with the requiring of a lot of files/modules. The framework should support iOS and Android (duh?) so I have no global scope (because that does not work on android, right?)
I will be requiring a lot of libraries, utility modules etc. I see two options here:
I require 1 file in 1 var (let's call it 'Iz') which on it's own requires everything I need, all my utility modules and libraries, so for instance my ArrayUtil module is accessible through the Iz var:
var Iz = require("/js/Iz"); Iz.utils.ArrayUtil.someFunction();pros: Easier for me and colleague developers to use because you don't need to worry about which module is available in what component. You just require the main Iz at the top of the module and you're ready to go.
cons: I hope none, but what if for instance I have a custom TableViewRow component which needs some functions from this library, every instance of that TableViewRow will then require this file (which will requires all the libraries every time). I'm not sure if this causes a lot of overhead or if the require caching prevents that.
require what I need for every component separately. So if I need my ArrayUtil module I'll have to require this first:
var ArrayUtil = require("/js/utils/ArrayUtil"); ArrayUtil.someFunction();pros: more efficient if the caching does not work as I hope.
cons: this leads to typing lots of
require('this'); require('that'); at the top of every module.
In my understanding of the caching of required modules, option 1 should not be an issue. So I'd prefer that. I would like some opinions or maybe a better option 3. How do you guys handle this?
I like Option 1 if the number of library files is reasonable. I'm with you, and I find it extremely messy to have require() calls all over the place for this library and that library.
Ultimately, you're going to take one hit per module to parse it. After that, subsequent calls to require() won't result in a full parse. I don't know how much overhead is involved in a require() for a previously required module.
But if you have your primary module load in the others, then each module in your application can make that one require() call. It certainly cuts down on the superfluous require() calls.
But it does "front load" the parse time. You just have to decide if your startup time is too slow. My TitanUp library works this way. It loads about 7 library files, and then can "lazy load" another 4. I don't think it impacts my apps' startup time too negatively.
To address your Option 1 con: the only way that every instance of your TableViewRow will require() the file is if you do the require inside the constructor of the TableViewRow, or inside some other function that is run by every instance. If the require() is up top, the require will only be run the first time your TableViewRow's module is required by another code module.
I will prefer Option 1 as after i can see all the options available to me after single require, so easy for me to use, and there is no such performance issue involved also.
Think you can help? Login to answer this question!