diff --git a/benchmark/buffers/buffer-write-string.js b/benchmark/buffers/buffer-write-string.js index 9d9e8a933917374..4628b315d5628f0 100644 --- a/benchmark/buffers/buffer-write-string.js +++ b/benchmark/buffers/buffer-write-string.js @@ -3,10 +3,10 @@ const common = require('../common.js'); const bench = common.createBenchmark(main, { encoding: [ - '', 'utf8', 'ascii', 'hex', 'utf16le', 'latin1', + '', ], - args: [ '', 'offset', 'offset+length' ], - len: [2048], + args: [ '' ], + len: [1,8,16,32,128], n: [1e6], }); diff --git a/lib/buffer.js b/lib/buffer.js index b204d0b63524b18..6d4790c04d54701 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1082,10 +1082,9 @@ function _fill(buf, value, offset, end, encoding) { Buffer.prototype.write = function write(string, offset, length, encoding) { // Buffer#write(string); if (offset === undefined) { - return this.utf8Write(string, 0, this.length); - } + // Do nothing // Buffer#write(string, encoding) - if (length === undefined && typeof offset === 'string') { + } else if (length === undefined && typeof offset === 'string') { encoding = offset; length = this.length; offset = 0; @@ -1109,15 +1108,16 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { } const len = string.length; - if (len <= 16 && len <= length) { + if (len <= 16 && len <= length && (!encoding || encoding === 'ascii' || encoding === 'utf8')) { let n = 0; + let pos = offset ?? 0; while (true) { const code = StringPrototypeCharCodeAt(string, n); if (code >= 128) { break; } - this[offset + n] = code; + this[pos + n] = code; n++; if (n === len) {