Nodejs performance tips.
In many cases such as when dealing with API response in JSON format, working on JavaScript assets embedded on websites, and so on, we keep seeing 13-digit long numbers very frequently and we probably know it is epoch timestamp originating from code like new Date().getTime()
, however, as a human we just can not quickly tell what date and time it represents. There is a convenient website named Epoch Converter that can help convert it to human-readable format easily but most of the time for developers, there are even quicker ways than opening the website, which is by using the interactive shell
that comes with many programming languages.
When dealing with large text files such as parsing log files, it is always preferred to load/iterate the content line by line instead of reading everything into memory for efficiency and performance reasons. In Nodejs, it has always been a hassle because the way of reading line by line looks hacky and too low-level from a daily user's perspective. However, that's not the case any more thanks to a PR merged into the latest version of v18.11.0
, there is now FileHandle.prototype.readLines
builtin which makes it very convenient to use.
In other programming languages such as Java, Ruby and Python, there are Thread.sleep()
, sleep
, and time.sleep()
methods to pause the execution of the current thread and wait after specified time to continue the execution, and when used together with loops, it can help address race condition issues for example. In node.js, and generally JavaScript, the way to pause and wait may not seem very obvious and frequently seen due to the nature of single-threaded, event-driven, and asyncronous architecture. However, since the use of wait
and retry
is still needed and helpful under certain circumstances, it is possible to simulate similar functions and effects in nodejs or JavaScript.
In Nodejs when dealing with non-text files (e.g. images) IO operations such as transferring via network or reading from disk, there is a big chance to receive the content as stream.Readable
, and before we can process the complete data in memory such as calculating the bytes size or image dimensions, we need save the stream
to buffer
and here are a few ways.
Following Upgrade socket.io from v2 to v3, this post is about upgrading socket.io
and its associated packages to the latest versions as of when this post is written and the only setting changes seem to be from the redis-adapater
and the way to import client io
this time (for our specific use case).
socket.io and its related libraries such as socket.io-redis-adapter and socket.io-redis-emitter had a few breaking changes in either functions or behaviors since v3 which might cause issues during the v2-to-v3 migration. This post lists what was encountered and how to solve it for specific use cases based on hands-on experience.
Since I find myself writing code more and more in JavaScript nowadays regardless of backend or frontend development, I figure out I should at least give it a look seriously, the first time in more than 10 years, and here are the resources I find useful while diving into this long overlooked programming language.
How to quickly generate random secure token with nodejs
nodenv is a great tool to manage multiple NodeJS versions and here are some of its commonly used commands summarized for quick reference.