Search code examples
javascriptrequireuserscriptstampermonkey

Should a library used solely by userscripts be minified?


In my userscripts, I often @require a self-written javascript library. I'm wondering how much load this generates. Is the library (re-)loaded every time a userscript is executed? Or is it only loaded the first time, and then cached? Would minifying it make a significant difference?

I looked at the tampermonkey docs, but they don't go into detail about this. They only state that the library "is loaded and executed before the script itself starts running".

How important is it to minify this library? Since I make changes to the library fairly often, I would prefer to avoid the extra step of minifying it every time. What are the advantages and disadvantages of minifying such a library?


Solution

  • Also, the precise details of how often a script is checked change. But here's how it usually/is supposed/used to work:

    1. When the script is installed, the @require library is fetched and saved to disk (nowadays stored as part of extension data in a LevelDB database).
    2. When the @require string is changed, the script is refetched.
    3. (¿Maybe?) the script is refetched if the script itself is updated (version changes)
    4. At one time, Tampermonkey may have been fetching the script at every script run?!
    5. Tampermonkey used to fetch @required scripts, with file:// URLs, at every run as an aid to developers. But this stopped working and not sure what the current status is.

    The point is that nominally, a required file (with an off-machine URL) should be run from disk or cache and be very fast.

    So trade-offs:

    Don't minimize because:

    • Normally makes no performance difference, in a userscript scenario.
    • Scripts are easier to debug.
    • Fewer steps in the develop/deploy process.

    Do minimize because:

    • The @required script has a large install base and hosting server load is a concern.
    • The file is large enough that minimizing can save significant space, say 100K.
    • The file is also used "normally" (EG by <script> tags) -- so bandwidth is much more critical. (Not everyone has high-speed connections, etc.)
    • You want to make it just a smidge harder for someone to "steal" your precious code. (This reason listed for completeness, I do not recommend it.)