Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Decrease overall bundle size #536

Closed
AdiRishi opened this issue Jun 28, 2024 · 5 comments · Fixed by #633
Closed

[Feature]: Decrease overall bundle size #536

AdiRishi opened this issue Jun 28, 2024 · 5 comments · Fixed by #633
Labels
enhancement New feature or request

Comments

@AdiRishi
Copy link
Owner

Current stats - Total Upload: 89.19 KiB / gzip: 25.73 KiB

Decreasing bundle size has a meaningful impact on performance in Cloudflare Workers. (Serverless runtimes in general)

How can we do this?

  • Remove / replace Zod
  • Remove / replace Hono
@AdiRishi AdiRishi added the enhancement New feature or request label Jun 28, 2024
@AdiRishi
Copy link
Owner Author

This is the current makup of the bundle.

Treemap:
Screenshot 2024-08-18 at 5 51 46 PM

Sunburst Chart:
Screenshot 2024-08-18 at 5 53 06 PM

Screenshot 2024-08-18 at 5 53 19 PM

Flame Chart:
Screenshot 2024-08-18 at 5 53 31 PM

@ItsWendell
Copy link
Contributor

ItsWendell commented Aug 19, 2024

Saw this issue in my feed, something relatively easy to reduce the bundle size could be to use hono/tiny (https://hono.dev/docs/api/presets#hono-tiny) and replacing Zod with Valibot (https://valibot.dev/guides/comparison/#comparison-with-zod), which also has a validator for Hono.

Edit: P.s. what tool did you use to annalyze bundle size here?

@ItsWendell
Copy link
Contributor

I ran this through bundlejs.com - includes config / results? and got result of Bundle size is 32.4 kB -> 11.2 kB (gzip), which is already more than 50/60% decrease. I think the we can decrease this indeed even more by removing hono / vlalibot and writing simple parser / routers ourselves but we'll lose the benefit of readability / maintainability.

@AdiRishi
Copy link
Owner Author

Hey @ItsWendell , thank you for the PR!

Edit: P.s. what tool did you use to annalyze bundle size here?
I ran esbuild manually to bundle the worker (this is what wrangler does internally). I then uploaded the metafile into esbuild's bundle size analyzer

esbuild src/index.ts --bundle --outfile=out/index.js --minify --external:cloudflare:workers --metafile=out/meta.json

I think your PR is a great first step. Switching zod to Valibot is a great choice, I was thinking the same.

hono/tiny is a good intermediate step. I was actually thinking of switching to itty-router - https://itty.dev/
Feels more condusive to a small bundle size. Although I'm still trying to evaluate if this is a good decision. We can skip this for now.

Did you want to take the PR through all the way to completion? Very happy to review it and work with you. If not, let me know and I can take over.

@ItsWendell
Copy link
Contributor

@AdiRishiHappy to help you bring the PR through completion. Glad you were eyeing valibot too! I don't have a lot of experience with itty-router, but at first glance, the DX of hono feels a lot cleaner. Honestly, not sure if switching and rewriting a huge chunk of your code to shave off even more bytes is worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants