diff options
author | Brian White <mscdex@mscdex.net> | 2020-01-24 02:44:15 -0500 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2020-01-26 19:16:05 -0500 |
commit | 59cba9a5c20c4a9fee34a3f6601025666aa6f9f6 (patch) | |
tree | 4ae1c1f71504a74b9df7be32bd2c370f5f004007 /lib/buffer.js | |
parent | 886965963ac55825e91887e973226ef010f8fc64 (diff) | |
download | node-new-59cba9a5c20c4a9fee34a3f6601025666aa6f9f6.tar.gz |
buffer: improve fill(number) performance
PR-URL: https://github.com/nodejs/node/pull/31489
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib/buffer.js')
-rw-r--r-- | lib/buffer.js | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index 01b9ea97f9..5a6b75c33e 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -34,9 +34,12 @@ const { ObjectCreate, ObjectDefineProperties, ObjectDefineProperty, + ObjectGetOwnPropertyDescriptor, + ObjectGetPrototypeOf, ObjectSetPrototypeOf, SymbolSpecies, SymbolToPrimitive, + Uint8ArrayPrototype, } = primordials; const { @@ -101,6 +104,13 @@ const { addBufferPrototypeMethods } = require('internal/buffer'); +const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype); + +const TypedArrayProto_byteLength = + ObjectGetOwnPropertyDescriptor(TypedArrayPrototype, + 'byteLength').get; +const TypedArrayFill = TypedArrayPrototype.fill; + FastBuffer.prototype.constructor = Buffer; Buffer.prototype = FastBuffer.prototype; addBufferPrototypeMethods(Buffer.prototype); @@ -1001,11 +1011,22 @@ function _fill(buf, value, offset, end, encoding) { return buf; } - const res = bindingFill(buf, value, offset, end, encoding); - if (res < 0) { - if (res === -1) - throw new ERR_INVALID_ARG_VALUE('value', value); - throw new ERR_BUFFER_OUT_OF_BOUNDS(); + + if (typeof value === 'number') { + // OOB check + const byteLen = TypedArrayProto_byteLength.call(buf); + const fillLength = end - offset; + if (offset > end || fillLength + offset > byteLen) + throw new ERR_BUFFER_OUT_OF_BOUNDS(); + + TypedArrayFill.call(buf, value, offset, end); + } else { + const res = bindingFill(buf, value, offset, end, encoding); + if (res < 0) { + if (res === -1) + throw new ERR_INVALID_ARG_VALUE('value', value); + throw new ERR_BUFFER_OUT_OF_BOUNDS(); + } } return buf; |