-
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 isArrayBufferDetached method #45512
Conversation
CC @daeyeon, since he was the original author of this TODO. |
9f4da15
to
55484ba
Compare
55484ba
to
5d284cf
Compare
Adding to @addaleax's point, if there is justification for such a public API in Node.js, isn't there justification for such an API in any JavaScript runtime, in which case it would be better to find a compatible approach across runtimes? |
I'm +0 on the public API. But I don't think there is a better & compatible approach across runtimes (for now), since WasDetached is not currently supported at Deno (using V8 10.9.194.1) |
255977c
to
81b13d1
Compare
81b13d1
to
4e614d5
Compare
If a universal solution is desired, and there's compelling use cases for it, a JS language proposal seems appropriate. |
4e614d5
to
82d44f4
Compare
What's the use case for this? The PR description is empty. |
lib/internal/util/types.js
Outdated
@@ -54,8 +57,16 @@ function isBigUint64Array(value) { | |||
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigUint64Array'; | |||
} | |||
|
|||
function isArrayBufferDetached(value) { | |||
if (ArrayBufferPrototypeGetByteLength(value) === 0) { |
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.
Should we do this before checking if value
is an instance of ArrayBuffer
?
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.
If the user passes something that's not an ArrayBuffer
, instead of returning false
the function would throw a TypeError
, which is surprising. We should at least document this behavior if we want to keep it (another option is to not expose it publicly and keep it internal, or expose a slower version that first checks if the parameter is an ArrayBuffer
before calling the internal util).
There are a few cases (e.g. in web streams) where we are required to check if the |
If we need it only for our webstreams API, it would be better to:
|
If performance cost of checking if
|
320ce8b
to
b1bf33f
Compare
b1bf33f
to
f207b32
Compare
I'm working with @ljharb on making a proposal to TC39 for This particular question is better answered with the original |
@@ -54,8 +58,16 @@ function isBigUint64Array(value) { | |||
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'BigUint64Array'; | |||
} | |||
|
|||
function isArrayBufferDetached(value) { | |||
if (value instanceof ArrayBuffer && ArrayBufferPrototypeGetByteLength(value) === 0) { |
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.
I wouldn't be surprised if that instanceof
check had a dramatic impact on performance.
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.
Referencing: https://github.com/RafaelGSS/nodejs-bench-operations/blob/main/RESULTS-v19.md#comparison-using-instanceof
I can change it to value != null && ...
, and expect .byteLength
to be the differentiator.
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.
I think the best course of action is to make an internal isArrayBufferDetached
without any instanceof check, and if we have a public one, it would return value instanceof ArrayBuffer && internal.isArrayBufferDetached(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.
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.
also, instanceof is brittle and can be forged, and doesn’t work cross-realm. In JS the way to check this is using the byteLength getter in a try/catch.
I'm closing this pull request in favor of |
CC @nodejs/streams @nodejs/util @nodejs/performance