From 67c61181b938b17d10dbfc0a545f713b8bd59de8 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 30 Jun 2015 16:04:58 -0700 Subject: [PATCH] optimize Buffer#toString() From https://github.com/nodejs/io.js/commit/8350f3a3a291fcd19df3261c3db3fe07c 7590bb7 --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 -- 2.34.1