From: Feross Aboukhadijeh Date: Tue, 30 Jun 2015 23:04:58 +0000 (-0700) Subject: optimize Buffer#toString() X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=67c61181b938b17d10dbfc0a545f713b8bd59de8;p=buffer.git optimize Buffer#toString() From https://github.com/nodejs/io.js/commit/8350f3a3a291fcd19df3261c3db3fe07c 7590bb7 --- diff --git a/index.js b/index.js index 5f66a3f..fac60da 100644 --- a/index.js +++ b/index.js @@ -336,8 +336,7 @@ Buffer.byteLength = byteLength Buffer.prototype.length = undefined Buffer.prototype.parent = undefined -// toString(encoding, start=0, end=buffer.length) -Buffer.prototype.toString = function toString (encoding, start, end) { +function slowToString (encoding, start, end) { var loweredCase = false start = start | 0 @@ -380,6 +379,13 @@ Buffer.prototype.toString = function toString (encoding, start, end) { } } +Buffer.prototype.toString = function toString () { + var length = this.length | 0 + if (length === 0) return '' + if (arguments.length === 0) return utf8Slice(this, 0, length) + return slowToString.apply(this, arguments) +} + Buffer.prototype.equals = function equals (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') if (this === b) return true