Node JS Best Practices — 2023

Kal
5 min readJan 20, 2023
  1. Don’t useJSON.parse and JSON.stringify

What? but I have used it for many years and it is very important. Yes I have also used it for many years and it indeed served me well but the problem starts when your input grows, it takes a very long amount of time and it can potentially block the event loop. The time complexity for these functions is O(n) and as the n grows the time it takes also grows. If your application takes and processes JSON objects, from a user, you should be cautious about the size of the objects or strings it takes.

Part of the reason why these functions are blocking is that they are processing the whole input and it works fine for small to medium size data but if you want to compute large data, you should consider other alternatives that use a stream to work with the given object or string chunk by chunk. There are npm packages that offer asynchronous JSON APIs such as:

2. Add a logger

An application can have different messages such as errors, warnings, information, user interaction data, etc. When the application gets bigger data and error management can be overwhelming and you…

--

--

Kal

I’m here to share my explorations, insights, and a touch of spice to keep things interesting.