-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: add inspect suffix to BigInt64Array elements #21499
util: add inspect suffix to BigInt64Array elements #21499
Conversation
This commit updates `util.inspect` to add an `n` suffix to BigInts that appear in BigInt64Arrays. BigInts are formatted with an `n` suffix in most cases, but this did not occur in BigInt64Arrays due to an apparent oversight where the implementation of `inspect` for typed arrays assumed that all typed array elements are numbers.
5f86252
to
7e000dc
Compare
lib/util.js
Outdated
@@ -897,8 +901,12 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { | |||
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); | |||
const remaining = value.length - maxLength; | |||
const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); | |||
const elementFormatter = | |||
types.isBigInt64Array(value) || types.isBigUint64Array(value) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof value[0]
will be waaaaaay faster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would result in elementFormatter
having the wrong value when the array is empty, although admittedly it wouldn't matter in that case because elementFormatter
would never be called. I'll change it to use typeof
.
lib/util.js
Outdated
@@ -897,8 +901,11 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { | |||
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); | |||
const remaining = value.length - maxLength; | |||
const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); | |||
const elementFormatter = typeof value[0] === 'number' ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change it to:
const formatter = len !== 0 && typeof value[0] === 'number' ?
formatNumber :
formatBigInt;
Otherwise the OOB access for an empty TypedArray slows down all following calls.
@@ -897,8 +901,11 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { | |||
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); | |||
const remaining = value.length - maxLength; | |||
const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); | |||
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ? |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Landed in 80496a5 |
This commit updates `util.inspect` to add an `n` suffix to BigInts that appear in BigInt64Arrays. BigInts are formatted with an `n` suffix in most cases, but this did not occur in BigInt64Arrays due to an apparent oversight where the implementation of `inspect` for typed arrays assumed that all typed array elements are numbers. PR-URL: #21499 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesThis commit updates
util.inspect
to add ann
suffix to BigInts thatappear in BigInt64Arrays. BigInts are formatted with an
n
suffix inmost cases, but this did not occur in BigInt64Arrays due to an apparent
oversight where the implementation of
inspect
for typed arrays assumedthat all typed array elements are numbers.