News for hackers. Stuff that matters.
Re:JavaScript Bloat in 2024 by hnarn on Friday February 23, 2024 @03:34AM (#39478290)
I'm not a JS developer but I imagine that the amount of JavaScript code isn't the most relevant part if most of it isn't being called. I mean, if you have some particularly heavy code that only runs when you click a button, is that really parsed and causes overhead before the button is clicked?
Re:JavaScript Bloat in 2024 by guappa on Friday February 23, 2024 @04:32AM (#39478655)
It is parsed and loaded in memory, and not executed.
Re:JavaScript Bloat in 2024 by lifthrasiir on Friday February 23, 2024 @05:02AM (#39478852)
"Loaded" part is no longer true in major JS implementations (for example, [1]).

[1] https://v8.dev/blog/preparser

Re:JavaScript Bloat in 2024 by acdha on Friday February 23, 2024 @08:07AM (#39480079)
That’s a broader claim than that page supports. They describe how they avoiding fully generating the internal representation and JITing it, but clearly even unused code is taking up memory and CPU time so you’d want to review your app’s usage to make sure that it’s acceptable levels of work even on low-end devices and also that your coding style doesn’t defeat some of those optimizations.
Re:JavaScript Bloat in 2024 by jitl on Friday February 23, 2024 @11:22AM (#39482588)
Depending on how the code is loaded, yes.

If all 10mb is in a single JS file, and that file is included in a normal script tag in the page’s HTML, then parsing the 10mb will block UI interaction as the page loads.

Once the browser parses 10mb, it’ll evaluate the top level statements in the script, which are the ones that would set up the click event handler you’re referencing.

If the entire page is rendered by JavaScript in the browser, then even drawing the initial UI to the screen is blocked by parsing JS.

The solution to this for big apps is to split your build artifact up into many separate JS files, analogous to DLLs in a C program. That way your entry point can be very small and quick to parse, then load just the DLLs you need to draw the first screen and make it interactive. After that you can either eagerly or lazily initialize the remaining DLLs depending on performance tradeoff.

I work on Notion, 16mb according to this measurement. We work hard to keep our entry point module small, and load a lot of DLLs to get to that 16mb total. On a slow connection you’ll see the main document load and become interactive first, leaving the sidebar blank since it’s a lower priority and so we initialize it after the document editor. We aren’t necessarily using all 16mb of that code right away - a bunch of that is pre-fetching the DLLs for features/menus so that we’re ready to execute them as soon as you say, click on the settings button, instead of having awkward lag while we download the settings DLL after you click on settings.