Uh oh!
There was an error while loading. Please reload this page.
util: add isArrayBufferDetached method - #45512
Conversation
anonrig
commented
Nov 18, 2022
CC @daeyeon, since he was the original author of this TODO. |
9f4da15 to
55484baComparenodejs-github-bot
commented
Nov 18, 2022
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
55484ba to
5d284cfComparetniessen
commented
Nov 19, 2022
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? |
anonrig
commented
Nov 19, 2022
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
81b13d1CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
81b13d1 to
4e614d5CompareUh oh!
There was an error while loading. Please reload this page.
ljharb
commented
Nov 19, 2022
If a universal solution is desired, and there's compelling use cases for it, a JS language proposal seems appropriate. |
4e614d5 to
82d44f4Comparemcollina
commented
Nov 20, 2022
What's the use case for this? The PR description is empty. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| } | ||
| function isArrayBufferDetached(value) { | ||
| if (ArrayBufferPrototypeGetByteLength(value) === 0) { |
There was a problem hiding this comment.
Should we do this before checking if value is an instance of ArrayBuffer?
There was a problem hiding this comment.
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).
jasnell
commented
Nov 20, 2022
There are a few cases (e.g. in web streams) where we are required to check if the |
mcollina
commented
Nov 20, 2022
If we need it only for our webstreams API, it would be better to:
|
LiviaMedeiros
commented
Nov 21, 2022
If performance cost of checking if
|
320ce8b to
b1bf33fCompareb1bf33f to
f207b32Compareanonrig
commented
Nov 21, 2022
I'm working with @ljharb on making a proposal to TC39 for This particular question is better answered with the original |
| } | ||
| function isArrayBufferDetached(value) { | ||
| if (value instanceof ArrayBuffer && ArrayBufferPrototypeGetByteLength(value) === 0) { |
There was a problem hiding this comment.
I wouldn't be surprised if that instanceof check had a dramatic impact on performance.
There was a problem hiding this comment.
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.
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.
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.
anonrig
commented
Dec 3, 2022
I'm closing this pull request in favor of |
CC @nodejs/streams @nodejs/util @nodejs/performance