The Express website comes
with a few more recommendations regarding performance.
Some we already took into account,
some will be implemented during this chapter.
Express recommends to cache request results
and in the next video we will implement that
in the chief of six fold performance improvement there.
We should not use synchronous functions.
Some functions come in the synchronous
and in a asynchronous form,
like fs.readFile and fs.readFilesync.
It's not just running a single thread,
this means that this NodeJS process, can not handle,
any more requests while the synchronous function runs.
While this does not matter if the function,
is called infrequently this can become a huge problem,
if your site becomes successful and gets more traffic.
A synchronous function utilized in NodeJS event loop
and will free the NodeJS process
to do other work until the function returns
with a promise or a callback.
Remember the decrypt function we used to hash passwords.
This is a good example, we used the asynchronous function
because otherwise each time I use the release change,
the hashing would have blocked NodeJS.
Express also recommends to not use console.log
because it's synchronous.
In this chapter we will use a,
dedicated asynchronous logger to replace console.log.
If errors aren't handled and exceptions aren't caught,
the node process might terminate with a crash,
even if we use a process manager
that then restarts the process.
This will of course impact performance.
NodeJS is as mentioned single threaded
and the search only utilizes one CPU.
By using a cluster module we can distribute requests
to multiple processes and utilize the CPU better.
We will do that in this chapter as well.
Now let's go ahead and implement some
of the optimizations we just talked about.