owid / owid-grapher

A platform for creating interactive data visualizations
https://ourworldindata.org
MIT License
1.35k stars 227 forks source link

chore: initialize some module-scope variables lazily #3740

Open marcelgerber opened 4 days ago

marcelgerber commented 4 days ago

I noticed a bunch of module-scope function calls; these get executed every time the corresponding module (and thereby owid.mjs) is loaded.

Many of these values are not needed at all, and some are only needed in "rare" cases. There are probably more such things, I didn't do a full-on search of everything. A rough regex to find those is ^(export )?(const|let) [\w_.]+ = [^(]*\w\( (although that won't find multiline ones like entities [newline] .filter(...)).

I also discovered that the result of some of these, especially the ones in CustomSchemes, are never accessed at all. By marking them with @__NO_SIDE_EFFECTS__, Rollup can eliminate (i.e. tree-shake) the call entirely from the bundle.


I think there is no way to do "proper" lazy properties, i.e. ones that are not a function call, without wrapping them in a class. Am I right? I would love to be proven otherwise :)

owidbot commented 4 days ago
Quick links (staging server): Site Admin Wizard

Login: ssh owid@staging-site-lazy-inits

SVG tester: Number of differences (default views): 0 ✅ Number of differences (all views): 0 ✅

Edited: 2024-06-24 17:47:29 UTC Execution time: 1.15 seconds