Benchmarking Popular NodeJS Logging Libraries  
Tags: webdev, javascript, node
Published: 2019-06-20 15:00:13

---
title: Benchmarking Popular NodeJS Logging Libraries  
published: true
description: Sometimes developers are hesitant to include logging due to performance concerns, but is this justified and how much does the choice of library affect performance? 
tags: webdev, javascript, node
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4sln7j2frop2fa14dauh.png
canonical_url: https://www.loggly.com/blog/benchmarking-popular-node-js-logging-libraries/
---
Sometimes developers are hesitant to include logging due to performance concerns, but is this justified and how much does the choice of library affect performance? 

Let’s run some benchmarks to find out! We ran a series of performance tests on some of the most popular NodeJS libraries. These tests are designed to show how quickly each library processed logging and the impact on the overall application.

## The Contenders

For this test, we investigated some of the most commonly used NodeJS logging libraries:

*   [Log4js](https://www.npmjs.com/package/log4js) 4.0.2
*   [Winston](https://www.npmjs.com/package/winston) 3.2.1
*   [Bunyan](https://www.npmjs.com/package/bunyan) 1.8.12

We also used the following additional libraries:

*   [winston-syslog](https://www.npmjs.com/package/winston-syslog) 2.0.1 for syslog logging with Winston
*   [bunyan-syslog](https://www.npmjs.com/package/bunyan-syslog) 0.3.2 for syslog logging with Bunyan

We benchmarked these libraries to test their performance, sending their logs to a console and file system. We also tested sending log info to a local rsyslog server over both TCP and UDP since it is common and probably wise to offload logs in a production environment.

These tests were run using NodeJS 8.15.1.

## Methodology

Our goal was to compare the performance between the logging libraries. Each library was run on its default configuration and the same system was used across all libraries and tests.

Our test application logged a total of 1,000,000 log events of the phrase “Hello, world!” and it’s available on GitHub at [https://github.com/codejamninja/node-log-benchmarks](https://github.com/codejamninja/node-log-benchmarks). We strictly processed logs to create an isolated control group. 

We measured the results dedicating either a single logical processor or eight (4 cores with hyperthreading) to simulate a larger production server. NodeJS is often considered a single threaded program, but technically it's just the event loop that is single threaded. There are many NodeJS tasks that take place on parallel threads, such as garbage collection. It's also worth noting that the tty (terminal) was doing a bunch of work printing the logs to the screen, which would have most definitely executed on a separate thread. That’s why is so important to test with multiple CPUs typically found on production systems.

Also, the NodeJS file system writes are nonblocking (asynchronous). The _unblocked_ time lets us know when the code used to schedule the filesystem writes is finished and the system can continue executing additional business logic. However, the file system will still be asynchronously writing in the background. So, the _done_ time lets us know how long it took to actually write the logs to the filesystem.

The hardware we used is from Amazon AWS.

<table>
  <tr>
   <td><strong>Name</strong>
   </td>
   <td><strong>Spec</strong>
   </td>
  </tr>
  <tr>
   <td>Processors
   </td>
   <td>Intel Core i7-7700 @ 2.80GHz (4 cores, 8 threads)
   </td>
  </tr>
  <tr>
   <td>Memory
   </td>
   <td>32GB Ram
   </td>
  </tr>
  <tr>
   <td>Operating System
   </td>
   <td>64-bit Ubuntu 18.04.2 LTS Server
   </td>
  </tr>
  <tr>
   <td>NodeJS
   </td>
   <td>8.15.1 LTS
   </td>
  </tr>
</table>


## Test Results

For all tests, the results are measured in milliseconds. The smaller bars are better because it means the logs took less time to process.


### Console

For the first set of test results, we benchmarked the performance of the libraries when logging to the console.

![Console Log Benchmarks](https://thepracticaldev.s3.amazonaws.com/i/4sln7j2frop2fa14dauh.png "Console Log Benchmarks")

From these results, we can see additional CPUs had a significant effect on the amount of time it took NodeJS to log to the console. Winston is the clear winner for speed in multithreaded systems; however, Bunyan performed slightly better in a single-threaded system.

### Filesystem

For the second set of test results, we benchmarked the performance of the libraries when writing the logs to the filesystem. Again, notice that each test result contains two times, _unblocked_ and _done_. This is because the libraries sometimes asynchronously send the logs to syslog. The total time to log is the sum of these two times.

![Filesystem Log Benchmarks](https://thepracticaldev.s3.amazonaws.com/i/97mdvefpj6qgtex8wjjm.png "Filesystem Log Benchmarks")

After seeing how much additional CPUs affected console logs, I was very surprised to see that logging to the filesystem performed roughly the same with additional CPUs. This is most likely because the work required to write files is much less than the work required to print to a tty device, so there was less multithreaded activity happening.

Log4js seemed to have the worst results writing to a filesystem, sometimes taking over 5 times the amount of time to write to the filesystem. Winston unblocked the event loop the fastest, but Bunyan finished writing to the filesystem the fastest. So, if you're choosing a log library based on filesystem performance, the choice would depend on whether you want the event loop unblocked the fastest or if you want the overall program execution to finish first.

### Syslog UDP

For the third set of test results, we benchmarked the performance of the libraries when sending the logs to syslog over UDP.

![Syslog UDP Log Benchmarks](https://thepracticaldev.s3.amazonaws.com/i/0sjwg9evzzieboih1jy9.png "Syslog UDP Log Benchmarks")

![Syslog UDP Log Drop Rate](https://thepracticaldev.s3.amazonaws.com/i/wcndba1ifhrpmrk2qa4i.png "Syslog UDP Log Drop Rate")

Log4js and Bunyan both finished around the same time when using multiple CPUs; however, Log4js unblocked the event loop much sooner and performed better on a single CPU.

Log4js also successfully sent all of its logs to syslog without dropping a single one. Although Bunyan had a low drop rate, it still managed to drop a few logs. I would say Log4js is a clear winner when sending logs to syslog over UDP.

I had a terrible experience getting Winston to work with syslog over UDP. When it did work it took well over a minute to unblock the event loop, and took over two minutes to finish sending the logs to syslog. However, most of the times I tested it, I ran out of memory before I could finish. I am assuming that when using UDP, the library aggregates all the logs in the heap before sending them to syslog, instead of immediately streaming the logs over to syslog. At any rate, it sends the logs over to syslog over UDP in a way that does not work well when slammed with a million logs.

## Syslog TCP

For the fourth set of test results, we benchmarked the performance of the libraries when sending the logs to syslog over TCP. Again, notice that each test result contains two times, _unblocked_ and _done_. This is because the libraries sometimes asynchronously send the logs to syslog.

![Syslog TCP Log Benchmarks](https://thepracticaldev.s3.amazonaws.com/i/hhvp9ne0axsnd4p8jzk6.png "Syslog TCP Log Benchmarks")

![Syslog TCP Log Drop Rate](https://thepracticaldev.s3.amazonaws.com/i/1w7v7r8ecw7j9kays5yv.png "Syslog TCP Log Drop Rate")

Since Bunyan was the only library that successfully sent logs to syslog over TCP without dropping any of them, it is the clear winner. Despite its somewhat slow performance when multiple CPUs were introduced, it still was relatively fast.

Sadly I was not able to get Log4js to send logs to syslog over TCP. I believe there is a bug in their library. I consistently received the following error.

`
(node:31818) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'trace' of undefined`

Winston was relatively fast when sending logs to syslog over TCP, however, it had a  horrific log drop rate. Most of the logs were either dropped or corrupted. Below is an example of one of the corrupted logs syslog received from Winston. You can see that the message was cut off.

`Mar 17 19:21:42 localhost /home/codejamninja/.nvm/versions/node/v8.15.1/bin/node[22463]: {"mes`

The log was supposed to look like this:

`Mar 17 19:21:42 localhost /home/codejamninja/.nvm/versions/node/v8.15.1/bin/node[22463]: {"message": "92342: Hello, world!"}`

Bunyan performed relatively well when sending logs to syslog over TCP. It did not drop a single log and unblocked the event loop very quickly. One thing that did surprise me though is that additional CPUs consistently performed worse than running on a single CPU. I am baffled by that, though this is the only scenario in which that happened.


## Conclusion

These results really took me by surprise. I was thinking there would be an overall winner, but each library performed best in different areas under different conditions.


<table>
  <tr>
   <td><strong>Output type</strong>
   </td>
   <td><strong>Winner</strong>
   </td>
  </tr>
  <tr>
   <td>Console
   </td>
   <td>Winston
   </td>
  </tr>
  <tr>
   <td>File
   </td>
   <td>Winston and Bunyan tied
   </td>
  </tr>
  <tr>
   <td>Syslog UDP
   </td>
   <td>Log4js
   </td>
  </tr>
  <tr>
   <td>Syslog TCP
   </td>
   <td>Bunyan
   </td>
  </tr>
</table>


Winston performed best when logging to the console. Winston and Bunyan both performed best in their own ways when logging to the file system. Log4js performed the best when sending logs to syslog over UDP. Bunyan had the best results when sending logs to syslog over TCP.

If you care more about throughput for syslog, then Log4js with UDP is the best output type. If you only care unblocking the code then Winston writing to a filesystem is the best. In this case, logging averaged 0.0005 ms per log event which is blazing fast. If your typical response latency is 100 ms, then it's only 0.0005% of your total response time. That’s faster than running console.log(). As long as you don’t go overboard with too many log statements, the impact is very small.


<table>
  <tr>
   <td>
   </td>
   <td>Console
   </td>
   <td>File
   </td>
   <td>Syslog TCP
   </td>
   <td>Syslog UDP
   </td>
  </tr>
  <tr>
   <td>Log4js
   </td>
   <td>24385 ms
   </td>
   <td>31584 ms
   </td>
   <td>N/A
   </td>
   <td><strong>1195 ms </strong>
   </td>
  </tr>
  <tr>
   <td>Winston
   </td>
   <td>10756 ms
   </td>
   <td>7438 ms
   </td>
   <td>9362 ms 
   </td>
   <td>142871 ms
   </td>
  </tr>
  <tr>
   <td>Bunyan
   </td>
   <td>15062 ms 
   </td>
   <td>4197 ms 
   </td>
   <td>24984 ms 
   </td>
   <td>12029 ms
   </td>
  </tr>
</table>


Overall, I would recommend using Log4js with UDP library for the best performance. This will have a negligible impact on your overall response time. Tools like [Loggly](http://loggly.com/) will store and organize those logs for you. It will alert you when the system encounters critical issues so you can deliver a great experience to your customers.