Skip to content

Instantly share code, notes, and snippets.

@conartist6
Created April 19, 2022 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conartist6/bd85798fd91b0be425cadd87a67b7a35 to your computer and use it in GitHub Desktop.
Save conartist6/bd85798fd91b0be425cadd87a67b7a35 to your computer and use it in GitHub Desktop.
Async of buffer perf test for mhofman
import { createReadStream } from 'fs';
import { arrayFromAsync } from 'iter-tools-es';
const COMMA = ','.charCodeAt(0);
const NL = '\n'.charCodeAt(0);
async function* csvParseChunks(document) {
let row = [];
let value = '';
for await (const buffer of document) {
for (let i = 0; i < buffer.length; i++) {
const charCode = buffer[i];
if (charCode === COMMA) {
row.push(value);
value = '';
} else if (charCode === NL) {
row.push(value);
value = '';
yield row;
row = [];
} else {
value += String.fromCharCode(charCode);
}
}
}
row.push(value);
yield row;
}
console.time('time');
const rows = await arrayFromAsync(csvParseChunks(createReadStream('./test.csv')));
console.log('rows:', rows.length);
console.timeEnd('time');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment