From: Matthieu Monsch Date: Fri, 8 Jan 2016 05:28:12 +0000 (-0800) Subject: Speed up buffer creation. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=12a3318ecbdf3f5155741f662dcf4376683cda61;p=buffer.git Speed up buffer creation. Since 1d20f50f8b4c62779c0dbfb1fa401168ab52dce0 (#40), instances already gained all the `Buffer`'s methods via the prototype re-assignment. Skipping the `_augment` call yields significant speedups: ~50% faster small buffer creation: new(16) before: 595,757 ops/sec ±0.92% (91 runs sampled) new(16) after: 894,462 ops/sec ±0.69% (87 runs sampled) ~20% faster slice: slice before: 2,176,194 ops/sec ±1.59% (89 runs sampled) slice after: 2,624,209 ops/sec ±0.42% (99 runs sampled) Other benchmarks aren't significantly different. --- diff --git a/index.js b/index.js index 76bfbbe..33f07b4 100644 --- a/index.js +++ b/index.js @@ -186,10 +186,11 @@ function fromArrayBuffer (that, array) { if (Buffer.TYPED_ARRAY_SUPPORT) { // Return an augmented `Uint8Array` instance, for best performance array.byteLength - that = Buffer._augment(new Uint8Array(array)) + that = new Uint8Array(array) + that.__proto__ = Buffer.prototype } else { // Fallback: Return an object instance of the Buffer class - that = fromTypedArray(that, new Uint8Array(array)) + that = fromTypedArray(that, array) } return that } @@ -223,6 +224,7 @@ function fromJsonObject (that, object) { if (Buffer.TYPED_ARRAY_SUPPORT) { Buffer.prototype.__proto__ = Uint8Array.prototype + Buffer.prototype._set = Uint8Array.prototype.set // For `copy` below. Buffer.__proto__ = Uint8Array } else { // pre-set for values that may exist in the future @@ -233,12 +235,11 @@ if (Buffer.TYPED_ARRAY_SUPPORT) { function allocate (that, length) { if (Buffer.TYPED_ARRAY_SUPPORT) { // Return an augmented `Uint8Array` instance, for best performance - that = Buffer._augment(new Uint8Array(length)) + that = new Uint8Array(length) that.__proto__ = Buffer.prototype } else { // Fallback: Return an object instance of the Buffer class that.length = length - that._isBuffer = true } var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 @@ -421,6 +422,10 @@ function slowToString (encoding, start, end) { } } +// Even though this property is private, it shouldn't be removed because it is +// used by `is-buffer` to detect buffer instances in Safari 5-7. +Buffer.prototype._isBuffer = true + Buffer.prototype.toString = function toString () { var length = this.length | 0 if (length === 0) return '' @@ -798,7 +803,8 @@ Buffer.prototype.slice = function slice (start, end) { var newBuf if (Buffer.TYPED_ARRAY_SUPPORT) { - newBuf = Buffer._augment(this.subarray(start, end)) + newBuf = this.subarray(start, end) + newBuf.__proto__ = Buffer.prototype } else { var sliceLen = end - start newBuf = new Buffer(sliceLen, undefined) @@ -1322,6 +1328,9 @@ var BP = Buffer.prototype /** * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods + * + * This function is also used externally by `typedarray-to-buffer`. + * */ Buffer._augment = function _augment (arr) { arr.constructor = Buffer