From: Feross Aboukhadijeh Date: Tue, 8 Apr 2014 07:36:05 +0000 (-0700) Subject: use fast .set() path for all typed arrays X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=1f4a8020b065cc538a720aa48949e934d2680d2e;p=buffer.git use fast .set() path for all typed arrays --- diff --git a/index.js b/index.js index 6ddb57c..99b8f63 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,7 @@ function Buffer (subject, encoding, noZero) { else if (type === 'string') length = Buffer.byteLength(subject, encoding) else if (type === 'object') - length = coerce(subject.length) // Assume object is an array + length = coerce(subject.length) // assume that object is array-like else throw new Error('First argument needs to be a number, array or string.') @@ -90,9 +90,11 @@ function Buffer (subject, encoding, noZero) { } var i - if (Buffer._useTypedArrays && typeof Uint8Array === 'function' && - subject instanceof Uint8Array) { - // Speed optimization -- use set if we're copying from a Uint8Array + if (Buffer._useTypedArrays && (subject instanceof Uint8Array + || subject instanceof Uint16Array || subject instanceof Int16Array + || subject instanceof Uint32Array || subject instanceof Int32Array + || subject instanceof Float32Array || subject instanceof Float64Array)) { + // Speed optimization -- use set if we're copying from a typed array buf._set(subject) } else if (isArrayish(subject)) { // Treat array-ish objects as a byte array @@ -933,7 +935,7 @@ function stringtrim (str) { var BP = Buffer.prototype /** - * Augment the Uint8Array *instance* (not the class!) with Buffer methods + * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods */ Buffer._augment = function (arr) { arr._isBuffer = true