JavaScript has become one of the most popular
programming languages of all time. It is used by almost 96% of websites all
over the world according to W3Tech. One key fact you
should know about the web is that you have no control over the hardware
specifications of the devices your user would access your website on. The
end-user may access your website on a high-end or a low-end device with either
great or poor internet connection. This means that you have to make sure your
website is optimized as much as possible for you to be able to satisfy the
requirements of any user.
1-Define Variables Locally
Whenever
you call a certain function, the variables that are used to define that
function are stored inside. Variables can be categorized into two types.
Local
variable: –
Variables that are defined only within themselves.
Global
variable: – Variables that are used throughout the script.
At the
time when you’re calling a function, the browser does an activity which is
termed as scope lookup. With the increase in the number of scopes in the scope
chain, there’s also an increase in the amount of time taken to access variables
that are outside the current scope.
2-Remove Unused Code and Features
Sometimes,
you might include features that are not used at all. It is better to keep this
extra code only in the development environment, and not to push it for
production so that you would not burden the client’s browser with unused code.
You can remove unused code manually or by using
tools such as Uglify or Google’s
Closure Compiler. You can even use a technique called tree shaking which removes unused
code from your application.
3-Keep
codes light and small
While
you’re in a development stage, you must ask yourself these questions below.
Do I
really need this module?
Can I do
this in a simpler manner?
Why am I
using this framework?
Is this
worth the overhead?
4. Avoid Memory Leaks
Even though garbage collection is performed automatically in JavaScript, there can be certain instances where it will not be perfect. In JavaScript ES6, Map and Set were introduced with their “weaker” siblings. This “weaker” counterpart known as WeakMap and WeakSet hold “weak” references to objects. They enable unreferenced values to be garbage collected and thereby prevent memory leaks. You can read more about WeakMaps.
No comments:
Post a Comment
Feel free to ask me for any query regarding my post