Skip to content

Commit

Permalink
Fix speed issue by switching to BufferList
Browse files Browse the repository at this point in the history
  • Loading branch information
bmpvieira committed Aug 16, 2014
1 parent 4476170 commit a7035fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/fasta-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
var through = require('through2')
var split = require('split')
var pumpify = require('pumpify')
var BufferList = require('bl')

module.exports = function() {
return pumpify(split(), parser())
}

function parser() {
var cacheBuf
var cacheBufLen = 8
var openID = new Buffer('{"id":"')
var closeIDOpenSeq = new Buffer('","seq":"')
var closeSeq = new Buffer('"}\n')
Expand All @@ -55,23 +55,24 @@ function parser() {
function transform(buf, enc, next) {
if (buf[0] === 62) { // If line starts with '>', this is an ID
if (cacheBuf) { // If a previous object is in cache, push it
cacheBuf = Buffer.concat([cacheBuf, closeSeq], cacheBufLen+3)
this.push(cacheBuf)
cacheBuf.append(closeSeq)
this.push(cacheBuf.slice())
}
var id = buf.toString().slice(1).trim().replace(/"/g, '\\"')
cacheBufLen = id.length + 16
cacheBuf = Buffer.concat([openID, new Buffer(id), closeIDOpenSeq], cacheBufLen)
cacheBuf = new BufferList()
cacheBuf.append(openID)
cacheBuf.append(id)
cacheBuf.append(closeIDOpenSeq)
}
else {
cacheBufLen += buf.length
cacheBuf = Buffer.concat([cacheBuf, buf], cacheBufLen)
cacheBuf.append(buf)
}
next()
}

function flush() {
cacheBuf = Buffer.concat([cacheBuf, closeSeq], cacheBufLen+3)
this.push(cacheBuf)
cacheBuf.append(closeSeq)
this.push(cacheBuf.slice())
this.push(null)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "MIT",
"dependencies": {
"bl": "^0.9.0",
"pumpify": "^1.3.0",
"split": "^0.3.0",
"through2": "~0.6.0"
Expand Down

0 comments on commit a7035fa

Please sign in to comment.