Skip to content

Commit efd85ed

Browse files
committed
buffer: optimize createFromString
PR-URL: nodejs#54324
1 parent 298ff4f commit efd85ed

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/buffer.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -442,21 +442,26 @@ function allocate(size) {
442442
}
443443

444444
function fromStringFast(string, ops) {
445-
const length = ops.byteLength(string);
445+
let length = string.length * 4; // max utf8 byte length
446+
447+
if (length >= (Buffer.poolSize >>> 1))
448+
length = ops.byteLength(string);
446449

447450
if (length >= (Buffer.poolSize >>> 1))
448451
return createFromString(string, ops.encodingVal);
449452

450453
if (length > (poolSize - poolOffset))
451454
createPool();
455+
452456
let b = new FastBuffer(allocPool, poolOffset, length);
453457
const actual = ops.write(b, string, 0, length);
454458
if (actual !== length) {
455-
// byteLength() may overestimate. That's a rare case, though.
456459
b = new FastBuffer(allocPool, poolOffset, actual);
457460
}
461+
458462
poolOffset += actual;
459463
alignPool();
464+
460465
return b;
461466
}
462467

0 commit comments

Comments
 (0)