| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ded1757 commit 59196af
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const Writable = require('stream').Writable; | ||
| 5 | + | ||
| 6 | + const bench = common.createBenchmark(main, { | ||
| 7 | + n: [2e6] | ||
| 8 | + }); | ||
| 9 | + | ||
| 10 | + function main(conf) { | ||
| 11 | + const n = +conf.n; | ||
| 12 | + const b = Buffer.allocUnsafe(1024); | ||
| 13 | + const s = new Writable(); | ||
| 14 | + s._write = function(chunk, encoding, cb) { | ||
| 15 | + cb(); | ||
| 16 | + }; | ||
| 17 | + | ||
| 18 | + bench.start(); | ||
| 19 | + for (var k = 0; k < n; ++k) { | ||
| 20 | + s.write(b); | ||
| 21 | + } | ||
| 22 | + bench.end(n); | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,23 +194,18 @@ function writeAfterEnd(stream, cb) { | |||
| 194 | 194 | process.nextTick(cb, er); | |
| 195 | 195 | } | |
| 196 | 196 | ||
| 197 | - // If we get something that is not a buffer, string, null, or undefined, | ||
| 198 | - // and we're not in objectMode, then that's an error. | ||
| 199 | - // Otherwise stream chunks are all considered to be of length=1, and the | ||
| 200 | - // watermarks determine how many objects to keep in the buffer, rather than | ||
| 201 | - // how many bytes or characters. | ||
| 197 | + // Checks that a user-supplied chunk is valid, especially for the particular | ||
| 198 | + // mode the stream is in. Currently this means that `null` is never accepted | ||
| 199 | + // and undefined/non-string values are only allowed in object mode. | ||
| 202 | 200 | function validChunk(stream, state, chunk, cb) { | |
| 203 | 201 | var valid = true; | |
| 204 | 202 | var er = false; | |
| 205 | - // Always throw error if a null is written | ||
| 206 | - // if we are not in object mode then throw | ||
| 207 | - // if it is not a buffer, string, or undefined. | ||
| 203 | + | ||
| 208 | 204 | if (chunk === null) { | |
| 209 | 205 | er = new TypeError('May not write null values to stream'); | |
| 210 | - } else if (!(chunk instanceof Buffer) && | ||
| 211 | - typeof chunk !== 'string' && | ||
| 212 | - chunk !== undefined && | ||
| 213 | - !state.objectMode) { | ||
| 206 | + } else if (typeof chunk !== 'string' && | ||
| 207 | + chunk !== undefined && | ||
| 208 | + !state.objectMode) { | ||
| 214 | 209 | er = new TypeError('Invalid non-string/buffer chunk'); | |
| 215 | 210 | } | |
| 216 | 211 | if (er) { | |
@@ -224,13 +219,14 @@ function validChunk(stream, state, chunk, cb) { | |||
| 224 | 219 | Writable.prototype.write = function(chunk, encoding, cb) { | |
| 225 | 220 | var state = this._writableState; | |
| 226 | 221 | var ret = false; | |
| 222 | + var isBuf = (chunk instanceof Buffer); | ||
| 227 | 223 | ||
| 228 | 224 | if (typeof encoding === 'function') { | |
| 229 | 225 | cb = encoding; | |
| 230 | 226 | encoding = null; | |
| 231 | 227 | } | |
| 232 | 228 | ||
| 233 | - if (chunk instanceof Buffer) | ||
| 229 | + if (isBuf) | ||
| 234 | 230 | encoding = 'buffer'; | |
| 235 | 231 | else if (!encoding) | |
| 236 | 232 | encoding = state.defaultEncoding; | |
@@ -240,9 +236,9 @@ Writable.prototype.write = function(chunk, encoding, cb) { | |||
| 240 | 236 | ||
| 241 | 237 | if (state.ended) | |
| 242 | 238 | writeAfterEnd(this, cb); | |
| 243 | - else if (validChunk(this, state, chunk, cb)) { | ||
| 239 | + else if (isBuf || validChunk(this, state, chunk, cb)) { | ||
| 244 | 240 | state.pendingcb++; | |
| 245 | - ret = writeOrBuffer(this, state, chunk, encoding, cb); | ||
| 241 | + ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); | ||
| 246 | 242 | } | |
| 247 | 243 | ||
| 248 | 244 | return ret; | |
@@ -291,11 +287,12 @@ function decodeChunk(state, chunk, encoding) { | |||
| 291 | 287 | // if we're already writing something, then just put this | |
| 292 | 288 | // in the queue, and wait our turn. Otherwise, call _write | |
| 293 | 289 | // If we return false, then we need a drain event, so set that flag. | |
| 294 | - function writeOrBuffer(stream, state, chunk, encoding, cb) { | ||
| 295 | - chunk = decodeChunk(state, chunk, encoding); | ||
| 296 | - | ||
| 297 | - if (chunk instanceof Buffer) | ||
| 298 | - encoding = 'buffer'; | ||
| 290 | + function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { | ||
| 291 | + if (!isBuf) { | ||
| 292 | + chunk = decodeChunk(state, chunk, encoding); | ||
| 293 | + if (chunk instanceof Buffer) | ||
| 294 | + encoding = 'buffer'; | ||
| 295 | + } | ||
| 299 | 296 | var len = state.objectMode ? 1 : chunk.length; | |
| 300 | 297 | ||
| 301 | 298 | state.length += len; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments