I strongly recommend to call all your JavaScript from compilers. By strongly associating your JavaScript to the elements it works on, you can prevent memory leaks as fragments get swapped in and out of the DOM repeatedly.
When your JavaScript bundle is so massive that you cannot load it all up front, I would recommend to load large libraries from the compilers that need it. Compilers are also a good place to track whether the library has been loaded before. Including the same <script> tag more than once would probably lead to memory leaks or duplicate event handlers being registered by your lib.
In our work we mostly load all JavaScript up front, since our bundles are small enough. We recently had a case where we had a very large geo map library that we only wanted to load when it is used. The code we used for that looked something like this:
var hugeLibraryLoaded;
up.compiler('.map', function($map) {
hugeLibraryLoaded = hugeLibraryLoaded || $.getScript('/huge-lib.js')
hugeLibraryLoaded.then(function() {
HugeLibrary.init($map)
})
})
Note how the library will only get loaded once even after compiling multiple <div class="map"> elements.
Best regards
Henning