Skip to content

Reduce bundle size #112

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

Closed
davidlj95 opened this issue Dec 25, 2023 · 11 comments · Fixed by #150, #155, #160, #157 or #161
Closed

Reduce bundle size #112

davidlj95 opened this issue Dec 25, 2023 · 11 comments · Fixed by #150, #155, #160, #157 or #161
Assignees
Labels
enhancement New feature or request

Comments

@davidlj95
Copy link
Owner

davidlj95 commented Dec 25, 2023

Bundle size is quite too big due to lots of features being provided in there. Firstly, modules take way too much space (Open Graph, Twitter card) when they're essentially just setting some <meta> elements. That's preventing myself from using the library. Given I'd honestly prefer to just apply those using Angular's Meta service from a performance point of view.

For instance, each metadata setter given it's injectable, has some overhead. So what it could be just a call to the MetaService to update a meta tag is envolved with much code around. Which increases the bundle size.

For instance, let's look at the DescriptionStandardMetadata when gets compiled for production (beautified for visualisation purposes)

Uc = 'description',
Fw = (() => {
  let e = class e extends Ge {
    constructor(n) {
      super(Uc, Uc), (this.metaService = n)
    }

    set(n) {
      this.metaService.set(new Vt(Uc), n)
    }
  }
  ;(e.ɵfac = function (o) {
    return new (o || e)(p(V))
  }),
    (e.ɵprov = g({ token: e, factory: e.ɵfac }))
  let t = e
  return t
})(),

That's 256 characters when picking it raw from the bundle (no whitespaces / new lines). So 256 bytes.

When the actual call that matters: this.metaService.set(new Vt(Uc), n) takes 35 chars -> 35 bytes. That's 221 bytes extra per each metadata.

Those 221 bytes are dedicated to:

  • Class syntax
  • Constructor
  • Angular's injectable code

Which could be reduced to just 1 per each kind of metadata type (Open Graph, Twitter Card, Standard, ...)

In Open Graph, if just joining was enough, that would mean saving 6 times 221 bytes (there are 7 metadata). So 1,326B -> 1.3kB.

Let's reduce the bundle size. For instance, by joining all metadata setters in one class

See #150 to see how a reduction was possible whilst still allowing to configure individual metadata setters 🤓

@davidlj95 davidlj95 added the enhancement New feature or request label Dec 25, 2023
@davidlj95
Copy link
Owner Author

Another solution, though not perfect, could be to only import the library in the server side part when using SSR. So this way it doesn't get in the browser bundle. No metadata will be set for browser, but that metadata is often just needed for SEO purposes soo it's good enough. 🤔

@davidlj95 davidlj95 self-assigned this Dec 29, 2023
@davidlj95 davidlj95 changed the title Try out joining all Open Graph / Twitter card / standard metadata Reduce bundle size Jan 13, 2024
@davidlj95
Copy link
Owner Author

See #150 some things I learned about bundle size optimizations when using non-advanced minifiers. I'd prefer to stay away from those to keep the build pipeline as simple as possible and therefore ease maintenance

@davidlj95
Copy link
Owner Author

Also linked: #175

@davidlj95
Copy link
Owner Author

Also linked: #176 / #179

@davidlj95
Copy link
Owner Author

Aaand #181

@davidlj95
Copy link
Owner Author

Also #192

@davidlj95
Copy link
Owner Author

A bit of gain with #197 . Not the main purpose tho

@davidlj95
Copy link
Owner Author

Moaaar in core in #201

@davidlj95
Copy link
Owner Author

Moaaar in core in #202

@davidlj95
Copy link
Owner Author

davidlj95 commented Jan 18, 2024

And #204 just 20b

@davidlj95
Copy link
Owner Author

davidlj95 commented Jan 19, 2024

Aaaaaand we're done

We started from 14.307B and ended up with 6.018B (comparing Angular v17 sizes, but v15 & v16 are similar). ~58% shaved off 🎉 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment