Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 10, 2024
1 parent dc3948d commit ebbe73f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions benchmark/buffers/buffer-write-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const common = require('../common.js');
const bench = common.createBenchmark(main, {
encoding: [
'',
'', 'utf8', 'ascii', 'hex', 'utf16le', 'latin1',
],
args: [ '' ],
len: [1,8,16,32,128],
args: [ '', 'offset', 'offset+length' ],
len: [1, 8, 16, 2048],
n: [1e6],
});

Expand Down
14 changes: 9 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,8 @@ function _fill(buf, value, offset, end, encoding) {
Buffer.prototype.write = function write(string, offset, length, encoding) {
// Buffer#write(string);
if (offset === undefined) {
// Do nothing
offset = 0;
length = this.length;
// Buffer#write(string, encoding)
} else if (length === undefined && typeof offset === 'string') {
encoding = offset;
Expand All @@ -1108,16 +1109,19 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
}

const len = string.length;
if (len <= 16 && len <= length && (!encoding || encoding === 'ascii' || encoding === 'utf8')) {
if (
len <= 16 &&
len <= length &&
(!encoding || encoding === 'ascii' || encoding === 'utf8')
) {
let n = 0;
let pos = offset ?? 0;
while (true) {
const code = StringPrototypeCharCodeAt(string, n);
const code = string.charCodeAt(n);
if (code >= 128) {
break;
}

this[pos + n] = code;
this[offset + n] = code;
n++;

if (n === len) {
Expand Down

0 comments on commit ebbe73f

Please sign in to comment.