Skip to content

Commit 43cf012

Browse files
committed
🎈 perf: Reduce overhead of nested effects (around 20x faster)
1 parent f97c801 commit 43cf012

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/effected.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ export class Effected<out E extends Effect, out R> implements Iterable<E, R, unk
409409

410410
return effected(() => {
411411
const iterator = this[Symbol.iterator]();
412+
let interceptIterator: typeof iterator | null = null;
412413
let terminated: false | "with-value" | "without-value" = false;
413414
let terminatedValue: unknown;
414415

@@ -420,7 +421,7 @@ export class Effected<out E extends Effect, out R> implements Iterable<E, R, unk
420421
...(terminated === "with-value" ? { value: terminatedValue } : {}),
421422
} as IteratorReturnResult<unknown>;
422423

423-
const result = iterator.next(...args);
424+
const result = (interceptIterator || iterator).next(...args);
424425

425426
const { done, value } = result;
426427
if (done) return result;
@@ -515,16 +516,17 @@ export class Effected<out E extends Effect, out R> implements Iterable<E, R, unk
515516
return { done: false, value: constructHandledEffect() } as never;
516517

517518
const it = handlerResult[Symbol.iterator]();
518-
const next = iterator.next.bind(iterator);
519-
iterator.next = (...args: [] | [unknown]) => {
520-
const result = it.next(...args);
521-
if (result.done) {
522-
iterator.next = next;
523-
return { done: false, value: constructHandledEffect() } as never;
524-
}
525-
return result as never;
519+
interceptIterator = {
520+
next: (...args: [] | [unknown]) => {
521+
const result = it.next(...args);
522+
if (result.done) {
523+
interceptIterator = null;
524+
return { done: false, value: constructHandledEffect() } as never;
525+
}
526+
return result as never;
527+
},
526528
};
527-
return iterator.next();
529+
return interceptIterator.next();
528530
}
529531

530532
return result;

0 commit comments

Comments
 (0)