split2
Advanced tools
Comparing version 2.2.0 to 3.0.0
11
bench.js
@@ -5,2 +5,3 @@ 'use strict' | ||
var bench = require('fastbench') | ||
var binarySplit = require('binary-split') | ||
var fs = require('fs') | ||
@@ -15,6 +16,14 @@ | ||
function benchBinarySplit (cb) { | ||
fs.createReadStream('package.json') | ||
.pipe(binarySplit()) | ||
.on('end', cb) | ||
.resume() | ||
} | ||
var run = bench([ | ||
benchSplit | ||
benchSplit, | ||
benchBinarySplit | ||
], 10000) | ||
run(run) |
38
index.js
/* | ||
Copyright (c) 2014-2016, Matteo Collina <hello@matteocollina.com> | ||
Copyright (c) 2014-2018, Matteo Collina <hello@matteocollina.com> | ||
@@ -19,14 +19,16 @@ Permission to use, copy, modify, and/or distribute this software for any | ||
var through = require('through2') | ||
var StringDecoder = require('string_decoder').StringDecoder | ||
const { Transform } = require('readable-stream') | ||
const { StringDecoder } = require('string_decoder') | ||
const kLast = Symbol('last') | ||
const kDecoder = Symbol('decoder') | ||
function transform (chunk, enc, cb) { | ||
this._last += this._decoder.write(chunk) | ||
if (this._last.length > this.maxLength) { | ||
this[kLast] += this[kDecoder].write(chunk) | ||
if (this[kLast].length > this.maxLength) { | ||
return cb(new Error('maximum buffer reached')) | ||
} | ||
var list = this._last.split(this.matcher) | ||
var list = this[kLast].split(this.matcher) | ||
this._last = list.pop() | ||
this[kLast] = list.pop() | ||
@@ -42,6 +44,6 @@ for (var i = 0; i < list.length; i++) { | ||
// forward any gibberish left in there | ||
this._last += this._decoder.end() | ||
this[kLast] += this[kDecoder].end() | ||
if (this._last) { | ||
push(this, this.mapper(this._last)) | ||
if (this[kLast]) { | ||
push(this, this.mapper(this[kLast])) | ||
} | ||
@@ -95,14 +97,10 @@ | ||
var stream = through(options, transform, flush) | ||
options.transform = transform | ||
options.flush = flush | ||
options.readableObjectMode = true | ||
// this stream is in objectMode only in the readable part | ||
stream._readableState.objectMode = true | ||
const stream = new Transform(options) | ||
// objectMode default hwm is 16 and not 16384 | ||
if (stream._readableState.highWaterMark && !options.highWaterMark) { | ||
stream._readableState.highWaterMark = 16 | ||
} | ||
stream._last = '' | ||
stream._decoder = new StringDecoder('utf8') | ||
stream[kLast] = '' | ||
stream[kDecoder] = new StringDecoder('utf8') | ||
stream.matcher = matcher | ||
@@ -109,0 +107,0 @@ stream.mapper = mapper |
{ | ||
"name": "split2", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "split a Text Stream into a Line Stream, using Stream 3", | ||
@@ -23,2 +23,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"binary-split": "^1.0.3", | ||
"callback-stream": "^1.1.0", | ||
@@ -28,8 +29,13 @@ "fastbench": "^1.0.0", | ||
"safe-buffer": "^5.1.1", | ||
"standard": "^10.0.0", | ||
"tap": "^10.0.0" | ||
"standard": "^11.0.0", | ||
"tap": "^12.0.0" | ||
}, | ||
"dependencies": { | ||
"through2": "^2.0.2" | ||
"readable-stream": "^3.0.0" | ||
}, | ||
"greenkeeper": { | ||
"ignore": [ | ||
"tap" | ||
] | ||
} | ||
} |
# Split2(matcher, mapper, options) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/mcollina/split2.svg)](https://greenkeeper.io/) | ||
[![build status](https://secure.travis-ci.org/mcollina/split2.svg)](http://travis-ci.org/mcollina/split2) | ||
@@ -8,3 +10,3 @@ | ||
and it is totally API compatible with it. | ||
However, it is based on [`through2`](https://github.com/rvagg/through2) by [@rvagg](https://github.com/rvagg) and it is fully based on Stream3. | ||
However, it is based on Node.js core [`Transform`](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) via [`readable-stream`](https://github.com/nodejs/readable-stream) | ||
@@ -27,3 +29,3 @@ `matcher` may be a `String`, or a `RegExp`. Example, read every line in a file ... | ||
is directly passed as a | ||
[Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform_1) | ||
[Transform](https://nodejs.org/api/stream.html#stream_new_stream_transform_options) | ||
option. | ||
@@ -68,5 +70,17 @@ | ||
# Benchmark | ||
```bash | ||
$ node bench.js | ||
benchSplit*10000: 1484.983ms | ||
benchBinarySplit*10000: 1484.080ms | ||
benchSplit*10000: 1407.334ms | ||
benchBinarySplit*10000: 1500.281ms | ||
``` | ||
Benchmark taken on Node 8.11.3, on a Macbook i5 2018. | ||
# License | ||
Copyright (c) 2014-2017, Matteo Collina <hello@matteocollina.com> | ||
Copyright (c) 2014-2018, Matteo Collina <hello@matteocollina.com> | ||
@@ -73,0 +87,0 @@ Permission to use, copy, modify, and/or distribute this software for any |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14370
331
95
7
+ Addedreadable-stream@^3.0.0
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedthrough2@^2.0.2
- Removedcore-util-is@1.0.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedxtend@4.0.2(transitive)