JS / NodeJS Tips & Tricks- You’d love to use in your code

JS / NodeJS Tips & Tricks- You’d love to use in your code

There may be several ways to optimise your code / increase your productivity, but this article summarises 5 Simple & Magical JavaScript / Node JS Tricks that you may use it frequently while writing your code.

Variable Declaration Methods

In Node JS, While receiving the requests, Most of the beginner developers used to code in the following way.

This list of variables goes on increasing, equivalent to the number of parameters sent in the request, so as the code lines too.

Instead, It can be written as

Thats it..! This single line of code does the same thing. :)
So Code lines got reduced. Looks very Simple. Isn’t it?

Use PM2 as Process Manager

Beginner NodeJS dev may face an inconvenient situation, while frequently using Control + C to exit the running instance and then running it again using node <filename.js> or npm start for every minor/major change made to file. It is really frustrating..!!

The only solution is to use Process Managers. It will monitor your app for changes and redeploy them. Any of the Process Managers (Nodemon, PM2, Strong-PM, Forever) can be used. But, It is recommended to use PM2 for its following advanced features, rather than just running your script.

* PM2 can be started by the desired timeout.

* Despite automatic restart over exit or crash, PM2 also allows us to manually restart your application.

* Auto Restart Based on Memory Usage

* Auto Restart Based on Cron

\Monitoring features* — CPU, Memory Usage, Request latency, console logs etc..,

*Log Management features — Display all log data / Specific app’s log Data, Flush (Clear Logs), log rotation etc ..,

( The Question “Difference b/w PM2 & npm start command?” is commonly raised in technical interviews 😉)

Use Promise.all Method

Many would’ve come across code jump issues, there come the Promise concurrency methods to help us better.

It takes an iterable of promises as input and returns a single Promise. This returned promise gets fulfilled when all promises fulfilled (including when an empty iterable is passed).

In short,Promise.all wait for all fulfillments (or the first rejection).

There are other several promise concurrency methods like
* *Promise.allSettled (fulfills, when all promise settle),
\
*
Promise.any (fulfills when any of the promises fulfills; rejects when all of the promises reject.)
\
*
Promise.race* (Settles when any of the promises settles first).

(Will Cover these topics in detail, in the upcoming blog)

Bonus Tip:
Promise.all() would be very much efficient in looping scenarios. By Coding in the following way, none of the iterations gets skipped.

Use Array Methods

Avoid using logical operators to specify multiple conditions in an if statement.

IF With Multiple conditions may keep going on, based on the requirements, But it can be rewritten in a simple manner using Array Includes() method.
The includes() method checks if an array contains a specified element or not.

syntax: arrayName.includes(valueToFind)

Pro Tip :

It is recommended to use Array Includes () method to find lesser ones. For a larger Find, usage of Set.has() in your code has a great impact on the performance which enhances the performance.
For Example: In an array of 100k users, looking for 1k online users, Array.includes() takes 99.5 ms, whereas Set.has() takes 3.6 ms;

Error Handling with Optional Chaining

Generally, To fetch data from an object, Dot Notation is widely been used. when you’re not sure whether an optional property exists, it may throw an error & stop executing your code further.

While dealing with API data, when you’re not sure whether an optional property exists, you can go for Optional Chaining.

Furthermore, Optional Chaining + Nullish Coalescing (??) is a great option to provide fallback values.

I hope this article would help you to learn a few tips related to JS & Node JS. Happy coding!