ยท hands on
Save memory with TypeScript generators!
Memory usage is a crucial metric when developing applications in TypeScript. It's frequently ignored until the "JavaScript heap out of memory" error appears. This error commonly occurs when loading large datasets in an application. In this tutorial, we will learn how to load big datasets and iterate over them while minimizing our memory usage.
Testing Scenario
I prepared a 184 MB CSV data file to compare the memory consumption of the iterable ReadableStream
API with traditional synchronous file loading. The aim is to iterate over the first line of the data and assess the memory consumption required for this operation. With process.memoryUsage(), it's feasible to log the results in bytes. Here's how it appears in the initial implementation:
Synchronous Execution
After the first call of fs.readFileSync
, the memory consumption quickly reached around 197 MB, indicating that the entire CSV file had been loaded into memory. Splitting the data nearly doubles the memory usage as the split result is also stored in memory. Iterating over the first chunk and exiting using the break
keyword has minimal impact on memory consumption. With roughly 338.70 MB being used, the memory consumption is quite high for such a small amount of business logic. Increasing the test data from 184 MB to 843 MB actually crashed the application with the following error message:
FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal
Iterable Streams
To enhance memory consumption and reduce crash rates, I restructured the business logic by making use of the iterable ReadableStream
API:
Observing the numbers above is quite impressive because the memory consumption remains quite stable. It begins at just 4.5 MB and only rises to a maximum of 4.6 MB. Surprisingly, even when handling the 843 MB sample data, there was not a significant increase in memory usage. In contrast to the 338.70 MB used in the previous scenario, this iterable code only consumes approximately 1.36% of the memory, potentially saving you up to 98.64 MB of RAM.