3 ms·
The specific situation I've encountered is that it's a file that's bigger than I want to hold in memory and I want to read it and process it a line at a time, b
by apendleton 8y ago
The specific situation I've encountered is that it's a file that's bigger than I want to hold in memory and I want to read it and process it a line at a time, but I don't want to do anything fancy or threaded or concurrent. Read a line, do something to it, write a line. Bog-standard ETL stuff.
And yeah, async/await helps, but is unambiguously ergonomically worse than:
import csv
incsv = csv.reader(open('file1.csv'))
outcsv = csv.writer(open('file2.csv', 'w'))
for row in incsv:
outcsv.writerow([row[0], row[1] + ' blah'])
and also just requires understanding a lot more stuff before you can be productive if you're new to the language.
I'm not saying it's not possible to do it in JS, just that it's not a task that plays to JS's ergonomic strengths, just like it's possible to write a linked list in Rust but kind of sucks.
- jdlshore 8y agoI would call it a weakness of JS's ecosystem, not an ergonomic problem. I believe JS could allow nearly the same code. Haven't tried it, though: const fs = require("fs"); const csv = require("some-csv-package"); const incsv = csv.reader(fs.createReadStream("file1.csv")); const outcsv = csv.writer(fs.createWriteStream("file2.csv")); for await (row of incsv) { await outcsv.writeRow([ row[0], `${row[1]} blah` ]); } Granted, that package doesn't exist, and the popular CSV package for Node [1] makes my eyes cross. As for me, none of the scripting I do with Node involves files that exceed Node's 1GB memory limit, so I take the cheap and easy way out and just readFile(). Which I suppose proves your point, although, again, I would argue it's an ecosystem problem, not a ergonomic problem. [1] https://csv.js.org/project/examples/ https://csv.js.org/project/examples/