NodeJS versus BunJS
In addition to Sarkar's response, another option is to utilize the node:fs
library (which is fully compatible with bun).
There is an existing discussion on this topic in a node environment: Dan Dascalescu responds to Alex C's inquiry (Read a file one line at a time in node.js?).
Performance Evaluation
There are noticeable discrepancies in terms of performance. I conducted a comparison between Sarkar's bunjs solution using bun:stream
and Dan Dascalescu's nodejs solution using node:stream
. Additionally, two scenarios were tested where files were read as bulk and then split line by line (node:bulk
and bun:bulk
). The following are the results of the analysis.
Versions used: node v20.11.1
, bun v1.1.6
📋 Results Run 1 (file: 5 MB)
⏱ node:stream = 24 ms
⏱ node:bulk = 4 ms
⏱ bun:stream = 8 ms
⏱ bun:bulk = 3 ms
📋 Results Run 2 (file: 368 MB)
⏱ node:stream = 470 ms
⏱ node:bulk = 204 ms
⏱ bun:stream = 716 ms
⏱ bun:bulk = 727 ms
📋 Results Run 3 (file: 5.5 GB)
⏱ node:stream = 18057 ms
⏱ node:bulk = 40004 ms
⏱ bun:stream = 25658 ms
⏱ bun:bulk = 35215 ms
📋 Results Run 4 (file: 12.8 GB)
⏱ node:stream = 46784 ms
⏱ bun:stream = 44367 ms
Conclusive Remarks
Unsurprisingly, stream-based file reading proves to be more efficient for larger files (approximately over 1GB), while bulk file reading exhibits better efficiency for smaller files (allowing for adaptability based on file size). When comparing node and bun solutions, it seems that bun's solution is notably slower than the node:fs solution (particularly evident for files > 100MB) up until files reach around 10GB in size. For larger files though, bun's performance starts catching up to node:fs.
This comparison could be further expanded upon (considering additional file sizes, impact of file structure - long lines vs short lines, etc.) in order to develop a smart switch mechanism for utilizing the most effective method based on specific file sizes.