News for hackers. Stuff that matters.
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Monday January 27, 2025 @08:43AM (#42841073)
My experiment on how to reduce javascript size of every web app by 30-50% : https://github.com/avodonosov/pocl

Working approach, but in the end I abandoned the project - I doubt people care about such js size savings.

Re:My failed attempt to shrink all NPM packages by 5% by hinkley on Monday January 27, 2025 @11:35AM (#42842900)
I got measurable decreases in deployment time by shrinking the node_modules directory in our docker images.

I think people forget that, when you’re copying the same images to dozens and dozens of boxes, any improvement starts to add up to real numbers.

Re:My failed attempt to shrink all NPM packages by 5% by syncsynchalt on Monday January 27, 2025 @01:00PM (#42843821)
I've not done it, but have you considered using `pnpm` and volume-mounting a shared persistent `pnpm-store` into the containers? It seems like you'd get near-instant npm installs that way.
Re:My failed attempt to shrink all NPM packages by 5% by hinkley on Monday January 27, 2025 @01:09PM (#42843924)
The only time npm install was on the critical path was hotfixes. It’s definitely worth considering. But I was already deep into doing people giant favors that they didn’t even notice, so I was juggling many other goals. I think the only thank you I got was from the UI lead, who had some soda straw internet connection and this and another thing I did saved him a bunch of hard to recover timeouts.
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Tuesday January 28, 2025 @04:41PM (#42858426)
In this approach the size of deployment bundles / images is not necessarily reduced.

Reduced is the size of javascript loaded into the end user's browser.

Re:My failed attempt to shrink all NPM packages by 5% by KTibow on Monday January 27, 2025 @10:48AM (#42842336)
I think this is called tree shaking and Vite/Rollup do this by default these days. Of course, it's easy when you explicitly say what you're importing.
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Monday January 27, 2025 @11:18AM (#42842672)
That's not tree-shaking.
Re:My failed attempt to shrink all NPM packages by 5% by KTibow on Monday January 27, 2025 @06:22PM (#42847004)
Oh, I guess you would need something like dynamic imports to not include uncommonly used functionality in the main bundle
Re:My failed attempt to shrink all NPM packages by 5% by philipwhiuk on Monday January 27, 2025 @10:47AM (#42842333)
How do you evaluate call usage?
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Tuesday January 28, 2025 @06:23PM (#42859485)
By instrumenting the code so that the function records the fact that it is being invoked.

Then the info about the called functions is sent back to the server.

(Only the functions never seen to be called are instrumented, the known active functions are not instrumented).

Re:My failed attempt to shrink all NPM packages by 5% by dagelf on Monday January 27, 2025 @08:51AM (#42841137)
Wdym?? 50% is a big deal
Re:My failed attempt to shrink all NPM packages by 5% by bluGill on Monday January 27, 2025 @08:59AM (#42841211)
50% size savings isn't important to the people who pay for it. They pay at most pennies for 100% savings (that is somehow all the functionality in zero bytes - not worth anything to those paying the bills)
Re:My failed attempt to shrink all NPM packages by 5% by tyre on Monday January 27, 2025 @09:16AM (#42841345)
Size savings translates to latency improvements which directly affects conversion rates. Smaller size isn’t about reducing costs but increased revenue. People care.
Re:My failed attempt to shrink all NPM packages by 5% by soared on Monday January 27, 2025 @09:45AM (#42841627)
Agreed - often a CTO of an ecom site is very very focused on site speed and has it as their #1 priority since it directly increases revenue.
Re:My failed attempt to shrink all NPM packages by 5% by fwip on Monday January 27, 2025 @10:46AM (#42842317)
Note that this proof-of-concept implementation saves latency on first load, but may add latency at surprising points while using the website. Any user invoking a rarely-used function would see a delay before the javascript executes, without the traditional UI affordances (spinners etc) to indicate that the application was waiting on the network. Further, these secretly-slow paths may change from visit to visit. Many users know how to "wait for the app to be ready," but the traditional expectation is that once it's loaded, the page/app will work, and any further delays will be signposted.

I'm sure it works great when you've got high-speed internet, but might break things unacceptably for users on mobile or satellite connections.

Re:My failed attempt to shrink all NPM packages by 5% by vlovich123 on Monday January 27, 2025 @12:24PM (#42843428)
> without the traditional UI affordances (spinners etc) to indicate that the application was waiting on the network.

This part is obviously trivially solvable. I think the same basic idea is going to at some point make it but it’ll have to be through explicit annotations first and then there will be tooling to automatically do this for your code based upon historical visits where you get to tune the % of visitors that get additional fetches. Also, you could probably fetch the split off script in the background anyway as a prefetch + download everything rather than just 1 function at a time (or even downloading related groups of functions together)

The idea has lots of merit and you just have to execute it right.

Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Tuesday January 28, 2025 @06:28PM (#42859522)
I agree with most of your comment, but don't get what you mean about explicit annotations.

Note that most of the unused code is located in libraries, not in the app code directly.

Re:My failed attempt to shrink all NPM packages by 5% by vlovich123 on Wednesday January 29, 2025 @03:01AM (#42862659)
split async function handle_button_press() { … }

This would cause the bundler to inject a split point & know how to hide that + know what needs bundling and what doesn’t. GWT pioneered almost 20 years ago although not a fan of the syntax they invented to keep everything running within stock Java syntax: https://www.gwtproject.org/doc/latest/DevGuideCodeSplitting....

Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Wednesday January 29, 2025 @03:29PM (#42870733)
Why do you want them explicit in the code?

The tooling producing such annotaion bwsed on historical visits whould conctantly change the annotated set of functions, I suspect, whith app versions evolving.

Note, the inactive code parts are very often in 3rd party libraries.

Some are also in your own libraries shared between your different application. So the same shared library whould need one set of split-annotated functions for one app, and another set for another app. So these conflicting sets of annotations can not live in the library source code simultaneously.

Re:My failed attempt to shrink all NPM packages by 5% by vlovich123 on Wednesday January 29, 2025 @05:00PM (#42871777)
For example the automated system could take your non-split codebase & inject splits at any async function & self-optimize as needed. The manual annotation support would be to get the ecosystem going as an initial step because I suspect targeting auto-optimizing right from the get go may be too big a pill to swallow.
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Tuesday January 28, 2025 @05:11PM (#42858746)
Only the first user who hits a rarely used execution point may experience the noticeable latency, if he also has slow internet, etc.

As soon as the user executes a rarely used function, the information is about this fact is sent to the server and it includes this function into the active set to be send to future users.

In the video I manually initiate re-generation of the "active" and the "rest" scripts, but the most primitive MVP was supposed to schedule re-generation of the scripts when receiving info that some previously unseen functions are executed in browser.

Obviously, if the idea is developed further, the first user's experience may also be improved - a spinner shown. Pre-loading the inactive set of functions in background may also be considered (pros: it allows to avoid latency for users ho invoke rare functionality, cons: we lose the savings of the traffic and browser memory and cpu for compiling the likely unneded code).

(BTW, further development of the idea includes to splitting the code more granularity than just "active" / "inactive". E.g. active in the first 5 seconds after opening the page loaded immediately, likely to be active soon, active but rarely called - the later two these parts definitely need to be loaded in background)

Re:My failed attempt to shrink all NPM packages by 5% by bluGill on Tuesday January 28, 2025 @08:26AM (#42852034)
Who? anyone who is on a slow internet connection or who has a slow device can tell you they don't care. Or maybe they do but features are far more important. I guess if things are slow on a top of the line device withia fast connection they would care.
Re:My failed attempt to shrink all NPM packages by 5% by avodonosov on Tuesday January 28, 2025 @06:11PM (#42859389)
Why big deal?

50% is just a big O of the original size :)

If we consider overall Internet traffic, which is dominated by video and images, the size of javascript transmitted is negligible.

Savings of memory and cpu for end user's browser? Maybe, but our software at all layers is so bloated, without need, just carelessly, that I'm not sure working of such javascript savings is useful - any resources saved will be eaten by something else immediately.

For an application developer or operator, the 50% savings of javascript size are probably not worth dealing with some tool that dynamically prunes the app code, raising questions about privacy and security. I though through the security and privacy questions, but who would want to even spend attention on these considerations?

As I mention in the README at github, a Microsoft researcher investigated the same approach earlier, but Microsoft haven't taken it anywhere.

There was a commercial company offering this approach of javascript minification as a product, complete product. As well as other optimizations, like image size, etc. Their proxy embedded special "agent" code into the app which inspected the device and reported to server what image sizes are optimal for user, what js functions are invoked. And the server prepared optimized versions of app for various devices. Javascript was "streamed" in batches of only functions needed by the app.

Now the company is dissolved - that's why the website is unavailable. Wikipedia says they were bought out by Akamai - https://en.wikipedia.org/wiki/Instart. But I don't see any traces of this approach in the today's Akamai offerings.

I contacted several companies, in CDN business and others, trying to interest them in the idea and get very modest funding for a couple more months of my time to work on this (I was working on this in the end of a long break from payed work and was running out of savings). Didn't find anyone ready to take part.

This all may be signs that possibility of such an optimization is not valuable enough for users.