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

Web bundle size optimization / wasm build best practices #732

Closed
D1plo1d opened this issue Jan 8, 2023 · 3 comments
Closed

Web bundle size optimization / wasm build best practices #732

D1plo1d opened this issue Jan 8, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation web relating to the web renderer for dioxus

Comments

@D1plo1d
Copy link

D1plo1d commented Jan 8, 2023

According to the readme:

Simple "hello world" at about 65kb, comparable to React

I have not been able to find any documentation on how to achieve such a small bundle size.

My Results

I've written a simple hello world which compiles with trunk build --release, lto = true, and opt-level="z" down to 275 KB:

use dioxus::prelude::*;

fn main() {
    dioxus_web::launch(App);
}

fn App(cx: Scope) -> Element {
    cx.render(rsx! {
        div {
            "Hello world"
        }
    })
}

This is 4 times the size of the 65KB Readme bundle size. My build size also grows quite quickly to 600KB for a small single page app mockup with just a few simple pages, a router and without any IO.

What best practices are the Dioxus devs applying to get a 64KB build size?

@ealmloff ealmloff added documentation Improvements or additions to documentation web relating to the web renderer for dioxus labels Jan 8, 2023
@ealmloff
Copy link
Member

ealmloff commented Jan 8, 2023

Building using the nightly compiler, gzip, trunk wasm opt, and a few tricks from min-sized-rust you can get the wasm size of the above app to 143 KB, which is 62 KB GZIPed: https://github.com/Demonthos/tiny-dioxus

It would be great to either link to min-sized-rust, or provide a guide on how to get binary sizes smaller for WASM specifically in the future

@D1plo1d
Copy link
Author

D1plo1d commented Jan 8, 2023

I must be doing something wrong or perhaps there's still a further difference between our setups that I'm not aware of - I've cloned tiny-dioxus and built it with trunk build --release but I'm seeing a 205 KB wasm file in my dist directory. It's much better but not yet 143 KB for some reason.

Edit: I wasn't using the nightly compiler, I am now getting 143 KB. For any fellow Trunk newbies who stumble across this in the future you want to be using RUSTUP_TOOLCHAIN="nightly" trunk build --release

Edit 2: Copying the approach in tiny-dioxus to my SPA also brought my wasm down from 600 KB to 430 KB. This starts to feel more manageable at least for this small project. I'm a bit concerned that this might continue to grow linearly if I tried to build something larger - are there any examples of large Dioxus web apps and their bundle sizes?

@ealmloff
Copy link
Member

ealmloff commented Oct 1, 2023

This was documented in DioxusLabs/docsite#132

@ealmloff ealmloff closed this as completed Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation web relating to the web renderer for dioxus
Projects
None yet
Development

No branches or pull requests

2 participants