⚡️Golang VS Node.js⚡️ - CSV Data Export
Tags: go, node, php, backend
Published: 2023-10-17 02:22:52

## Prologue
Three months ago, I encountered a challenge while exporting **CSV** data using **PHP**. The problem revolves around long duration and large memory consumption. Therefore, I started looking for alternative solutions to overcome this problem. This prompted me to conduct an experiment comparing data export speed using **Node.js** and **Golang**. I have created a project repository for this, which you can find at [https://github.com/rizkypujiraharja/benchmark-export-data-to-csv](https://github.com/rizkypujiraharja/benchmark-export-data-to-csv)


When it comes to exporting data in CSV format, efficiency and speed are key factors to consider. In this article, we will delve into how each programming language can be employed to accomplish this task and how to measure and compare their performance. By understanding the differences in approaches and performance outcomes among these two languages, you will be better equipped to make informed decisions when selecting the right tool for your next CSV data export project.

## Here we go!!
In this testing, I use a server with specifications of 8 GB RAM / 8 vCPU per day on [Railway](https://railway.app?referralCode=_eAGQJ). I have created a REST API to export data to CSV, which retrieves 50,000 records from MongoDB database.
To minimize memory usage and enhance efficiency, I fetched the data in chunks of 10,000 rows per query. This approach enables more efficient processing of large datasets, as data does not need to be fully loaded into memory all at once. 

I will conduct two rounds of testing: the first one will be manual testing, and the second will involve load testing using [https://github.com/tsenart/vegeta](https://github.com/tsenart/vegeta).
I hope this explanation helps you better understand the context of this testing.


### #Manual Testing
> Golang took 0.332430 seconds, while Node.js took 0.844 seconds. This means Golang is 3 times faster than Node.js.
![Golang manual test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2p606nmgfwncqxmfn8vx.png)![Node.js manual test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5rnwi7j2ecwkjxmucqp.png)


### #Load Testing
The testing was conducted by sending 5 concurrent requests every second for a duration of 10 seconds.

|         | Min       | Max      | Mean     | CPU Usage | Memory Usage |
|---------|----------|---------|----------|-----------|--------------|
| Golang  | 530.717ms | 984.27ms | 572.301ms | 0.2vCPU   | 317MB        |
| Node.js | 6.245s    | 18.586s | 22.872s   | 0.7vCPU   | 1.03GB       |

Vegeta Result
![Vegeta result golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bcob1ugblw4qh9g9viwy.png)![Vegeta result nodejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0z4kz1a1muyqztv0x03.png)

Usage Resource
![Resource test golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ty7i0ii7p4kecufhoxi.png)![Resource test nodejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7k1t60snaee9wtp8b7y.png)


It is evident that when testing concurrent requests, Golang outperforms Node.js.