diff --git a/lib/buffer.js b/lib/buffer.js index 4e6031afdb3919..72e663e8e82563 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -442,21 +442,26 @@ function allocate(size) { } function fromStringFast(string, ops) { - const length = ops.byteLength(string); + let length = string.length * 4; // max utf8 byte length + + if (length >= (Buffer.poolSize >>> 1)) + length = ops.byteLength(string); if (length >= (Buffer.poolSize >>> 1)) return createFromString(string, ops.encodingVal); if (length > (poolSize - poolOffset)) createPool(); + let b = new FastBuffer(allocPool, poolOffset, length); const actual = ops.write(b, string, 0, length); if (actual !== length) { - // byteLength() may overestimate. That's a rare case, though. b = new FastBuffer(allocPool, poolOffset, actual); } + poolOffset += actual; alignPool(); + return b; }