DEV Community

Discussion on: JavaScript Tips to Improve Your Webpage Performance

Collapse
lukeshiru profile image
LUKESHIRU

A few things about the topics mentioned:

  • Local Variables: The important thing about avoiding global variables is not so much performance, but actually security.
  • Webpack: There are bundlers way better than WebPack if we care about FCP, like Rollup. But, the bundler will have no effect whatsoever if the app is coded poorly (not helping treeshacking, for example), or the bundler is misconfigured.
  • Location: Having the async and defer attributes, the location of the scripts can be on the header.
  • Access to DOM: If you will not interact with the DOM from JS, then why do you even have JS to begin with?

Also, I think some other things weren't mentioned:

  • Avoid JS when possible: JS will never be faster than just HTML+CSS, so if your site will be static and only have some animations, then you can skip JS altogether.
  • Take stuff away from the main thread: When possible, use workers, they allow you to take processes to a new thread or even handle the caching and serving of resources, which actually makes your app load faster.
  • Use CDNs: CDNs such as CloudFlare will make loading your site in remote places way faster than just serving it yourself directly.

Cheers!

Collapse
explosion profile image
--Explosion--

I disagree with "Avoid JS when possible". Server side rendered sites can be good, but they often take a long time to render before sending the page to you, while a skeleton loader is much more preferable. I do agree with it to an extent though, because using excess javascript can slow down the page. For animations and styles it's a lot better to skip JS altogether, but don't go to crazy with the "ONLY CSS" stuff. For me the best way to build a site is through something like Nuxt.js, which statically generates your site. Making it lightning fast.

Collapse
lukeshiru profile image
LUKESHIRU

Take a look at Astro or Remix, that's the type of tool I think of when I say "avoid JS when possible". They keep JS to a minimum, do a lot of SSR and they are blazing fast compared to any SPA.