Skip to content

Commit

Permalink
buffer: optimize createFromString
Browse files Browse the repository at this point in the history
PR-URL: nodejs#54324
  • Loading branch information
ronag committed Aug 12, 2024
1 parent 298ff4f commit efd85ed
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit efd85ed

Please sign in to comment.