- Don’t use
JSON.parse
andJSON.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:
- JSONStream, which has stream APIs.
- Stream-json
- Big-Friendly JSON, which has stream APIs as well as asynchronous versions of the standard JSON APIs.
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…