I have to write a lot of scripts. A lot of scripts. Oftentimes this is because there are a lot of tasks that are too complex to do by hand, or too massive to be done by hand. So I turn to writing scripts for everything that takes too long. It reduces errors and increases performance. As my peers hate me for always saying;
If you do it more than once, write a script.
Some common tasks are:
- find matches or x, y, z in these giant arrays.
- remove all instances of this value, but only under these conditions.
- compare x and y and then output z.
And I know most programmers are thinking how well Python or even C# or Go would be suited to these tasks, but there are three key reasons why I always choose NodeJS.
It is not I/O blocking
By default, NodeJS can read a file, parse the content into the database, and write new content to the file, without even thinking about it. While there are libraries for Python and similar to add asynchronous abilities, by default Python is not asynchronous, one function must always execute before the next function can occur.
NodeJS doesn’t suffer from this problem, as it is designed to handle requests flowing constantly from web traffic, and also designed to handle everything in the background (file serving, mail etc).
This makes it perfect for my kind of tasks — I often extract the information I’m after, and store it in a database for later recall. I do this to both persist my data, and to free up memory by not having to hold the entire array. I can then be calling information back from the database, manipulating it and altering it, all the while it is running away in the background, storing more information in the database.
I/O requests are also handled on a separate thread in NodeJS from the main thread of execution. What this means, is that a particularly large or troublesome file being read in will not hinder the performance of the main program (providing the content of the file is not immediately necessary of course).