Handling large CSV files, especially when they are fetched from an API, can be tricky. Memory issues can arise when trying to process everything in-memory. Here's a general approach to handle this scenario efficiently:
Stream the API Data: Instead of fetching all the CSV data at once, use a streaming approach to read the CSV data from the API. This ensures that you're only processing a chunk of data at any given time, rather than loading everything into memory.
Write CSV Directly: Instead of collecting all CSV rows in memory, write them directly to the target CSV file as they're processed. This way, you're not storing large arrays in memory.
Filter and Write to a Second CSV: While writing to the main CSV file, you can also filter rows based on your criteria and write the filtered rows to a separate CSV file.
Handle Headers Properly: To avoid writing headers multiple times, check if the target CSV file already exists. If it doesn't, write the headers; otherwise, skip them.
Memory Issue: The "JavaScript heap out of memory" error indicates that your Node.js process is running out of memory. This can happen if you're trying to process a large amount of data all at once. The streaming approach should help mitigate this by processing data in chunks.
If you continue to encounter memory issues, consider increasing the heap size when starting your Node.js process\