Skip to content
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

perf(primordials): Check the need for SafeArrayIterator each time #80

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions core/00_primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@
});

const {
ArrayIteratorPrototype,
ArrayPrototype,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypeMap,
Expand All @@ -305,15 +307,21 @@
PromisePrototype,
PromisePrototypeThen,
SymbolIterator,
TypedArrayPrototype,
TypedArrayPrototypeJoin,
} = primordials;

// Because these functions are used by `makeSafe`, which is exposed
// on the `primordials` object, it's important to use const references
// to the primordials that they use:
const createSafeIterator = (factory, next) => {
const createSafeIterator = (factory, next, check) => {
class SafeIterator {
constructor(iterable) {
// https://github.com/denoland/deno_core/issues/6
if (check?.(iterable) === true) {
return iterable;
}

this._iterator = factory(iterable);
}
next() {
Expand All @@ -329,11 +337,19 @@
return SafeIterator;
};

const SafeArrayIterator = createSafeIterator(
const OriginalArrayPrototypeSymbolIterator = ArrayPrototype[SymbolIterator];
const OriginalTypedArrayPrototypeSymbolIterator =
TypedArrayPrototype[SymbolIterator];
const OriginalArrayIteratorPrototypeNext = ArrayIteratorPrototype.next;
const SafeArrayIterator = primordials.SafeArrayIterator = createSafeIterator(
primordials.ArrayPrototypeSymbolIterator,
primordials.ArrayIteratorPrototypeNext,
(iterable) =>
(iterable[SymbolIterator] === OriginalArrayPrototypeSymbolIterator ||
iterable[SymbolIterator] ===
OriginalTypedArrayPrototypeSymbolIterator) &&
ArrayIteratorPrototype.next === OriginalArrayIteratorPrototypeNext,
);
primordials.SafeArrayIterator = SafeArrayIterator;
primordials.SafeSetIterator = createSafeIterator(
primordials.SetPrototypeSymbolIterator,
primordials.SetIteratorPrototypeNext,
Expand Down