-
-
Save conartist6/bd85798fd91b0be425cadd87a67b7a35 to your computer and use it in GitHub Desktop.
Async of buffer perf test for mhofman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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