Last active
February 28, 2018 17:05
-
-
Save carbonrobot/260946ab72322b5a16c52d2617b5f9ed to your computer and use it in GitHub Desktop.
Comparing performance of CSV parser libs on very large files
This file contains hidden or 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
const chalk = require('chalk'); | |
const marky = require('marky'); | |
const parse = require('csv-parse'); | |
const fs = require('fs'); | |
const es = require('event-stream'); | |
const csv = require('fast-csv'); | |
// ------------------------- | |
// Using raw event-stream | |
// This is the reference throughput | |
// and does no parsing and delimiting of values | |
// ------------------------- | |
let count2 = 0; | |
var lineSplitter = es.split(); | |
lineSplitter | |
.on('data', data => { count2++; }) | |
.on('end', data => { | |
const ttl = marky.stop('splitter').duration; | |
console.log(count2, 'lines', ttl); | |
}); | |
marky.mark('splitter'); | |
const file2 = fs.createReadStream('./big.csv'); | |
file2.pipe(lineSplitter); | |
// ------------------------- | |
// Using csv-parse lib | |
// ------------------------- | |
const csvOptions = { | |
auto_parse: false, | |
escape: '\\', | |
trim: true | |
}; | |
const parser = parse(csvOptions); | |
let count = 0; | |
parser | |
.on('data', data => { count++; }) | |
.on('end', data => { | |
const ttl = marky.stop('csv-parse').duration; | |
console.log(count, 'lines', ttl); | |
}); | |
marky.mark('csv-parse'); | |
const file = fs.createReadStream('./big.csv'); | |
file.pipe(parser); | |
// ------------------------- | |
// Using fast-csv lib | |
// ------------------------- | |
let count3 = 0; | |
const k = csv() | |
.on('data', data => { count3++; }) | |
.on('end', () => { | |
const ttl = marky.stop('fast-csv').duration; | |
console.log(count3, 'lines', ttl); | |
}); | |
marky.mark('fast-csv'); | |
const file3 = fs.createReadStream('./big.csv'); | |
file3.pipe(k); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment