Our Global Presence
Canada
57 Sherway St,
Stoney Creek, ON
L8J 0J3
India
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
USA
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
When building a web application, there’s something you absolutely need to take account: performance. Your app may be greatly designed or have some killer features, nobody will use it if it’s not performant. Here are some important aspects which will make an app more performant.
This is the most easiest thing and one of the most common mistakes you can find out there. When integrating images on an app, you’ve been told to use the pictures with the highest resolution to have the best quality images. That’s great. What you haven’t been told is that the images you use from Unsplash or Pexels often have huge dimensions and are not meant to be displayed on smaller screens.
It’s like watching a 10GB 4K movie on an old tiny computer. 4K or not, you won’t see many differences. That’s the same for images on a website. Thus, you must reduce the quality and resize the images in order to make them smaller.
There are plenty of apps that allows you to do so like squoosh.app. For example, you can find below an example of an image whose size was reduced on squoosh:
Sure the image on the right is a little bit blurry and less detailed than the one of the left but at this zoom level, it’s still looking good to me and it’s also 96% smaller which means your browser loads 5.2MB less.
If you’re building single-page applications (SPA) with client-side rendering (as with create-react-app or Vue CLI for example), this tip will also boost your app with little efforts.
In fact, client-side rendering means you are basically fetching on the server a bare bons HTML document with a JavaScript file (called a bundle) containing your whole application logic. This approach makes your app longer to load initially but more rich in terms of interaction and route changes. It’s a problem since a long initial load will be perceived badly by your users.
In fact, you will ship to the client this big JavaScript file even if your users don’t need to use every features of the application (typically a settings page when you can change your name or modify your password).
To solve this problem, you can use code splitting: “lazy-load” just the things that are currently needed by the user.
One common strategy is to split your code based on your different routes. Let’s take the example of a fake store app:
import { BrowserRouter as Router, Route, Switch } from "react-router-dom" import React, { Suspense, lazy } from "react" const Products = lazy(() => import("./Products")) const Cart = lazy(() => import("./Cart")) const Settings = lazy(() => import("./Settings")) const App = () => ( <Router> <Suspense fallback={<div>Loading...</div>}> <Switch> <Route exact path="/" component={Products} /> <Route path="/cart" component={Cart} /> <Route path="/settings" component={Settings} /> </Switch> </Suspense> </Router> )
With that approach, if the users want to browse your products, they won’t need to load the code associated to the Cart or the Settings page.
It can make your initial load way faster by saving hundreds and hundreds of KB, especially if your other pages use heavy third party libraries, which bring us to the third and last point.
We saw how to reduce the bundle size at the initial load. Here we will see how to reduce it but globally this time.
A great tool to do so is webpack-bundle-analyzer or source-map-explorer for Create React App users. We’ll show you an example with CRA here. There are few steps to follow:
source-map-explorer
:npm install --save source-map-explorer
analyze
script to your package.json
file:{ "scripts": { "analyze": "source-map-explorer 'build/static/js/*.js'" } }
This will run source-map-explorer
on the JS files generated by npm run build
.
npm run build npm run analyze
By running the script on one of your projects, you should get something similar to that:
You can see in the results that the bigger a dependency is, the more it takes up place visually. That’s a great way to spot the heaviest dependencies.
We ran source-map-explorer on a small project so you can see the bundle is not that big in our case. You’ll likely get a heavier bundle and more dependencies on bigger projects.
Once you have the results, it’s up to you to see which dependencies are the heaviest and decide if you must find alternatives for them. To find the cost of a package in your bundle, We recommend bundlephobia. It’s great for comparing packages cost and making you wonder if you really need to install that dependency.
For example, we used to use moment.js for date manipulation. But it has a pretty big cost on your package:
In fact, moment
is not modular. If you need to manipulate dates, then you need to create a Moment
instance that gives you access to a lot of methods even if you use just one or two of them.
Now, We use date-fns which is way more modular and lighter too:
You can follow the same logic for a lot of dependencies. However, be pragmatic when making these choices:
these three tips gave you the will to optimize your apps (and create more stellar experiences for your users!). Of course, there are plenty of other performance tips like:
We highly encourage you to explore all these options and see what they can bring to you performance-wise. However, don’t be obsessed with that. When it comes to building apps, there are always trade-offs.
For more information and to develop web application using Front-End technology, Hire Front-End Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”. To develop any custom web apps using Js, please visit our technology page.
Content Source:
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
© 2024 — HK Infosoft. All Rights Reserved.
© 2024 — HK Infosoft. All Rights Reserved.
T&C | Privacy Policy | Sitemap