Skip to content

Commit 03dbfe6

Browse files
committed
perf(v5): reuse unmatched objects between branches
1 parent 4366ab0 commit 03dbfe6

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/match.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type MatchState<output> =
3131
| { matched: true; value: output }
3232
| { matched: false; value: undefined };
3333

34-
const defaultMatchState: MatchState<never> = {
34+
const unmatched: MatchState<never> = {
3535
matched: false,
3636
value: undefined,
3737
};
@@ -48,13 +48,14 @@ const defaultMatchState: MatchState<never> = {
4848
class MatchExpression<input, output> {
4949
constructor(
5050
private input: input,
51-
private state: MatchState<output> = defaultMatchState
51+
private state: MatchState<output> = unmatched
5252
) {}
5353

5454
with(...args: any[]): MatchExpression<input, output> {
5555
if (this.state.matched) return this;
5656

57-
const handler = args[args.length - 1];
57+
const handler: (selection: unknown, value: input) => output =
58+
args[args.length - 1];
5859

5960
const patterns: Pattern<input>[] = [args[0]];
6061
const predicates: ((value: input) => unknown)[] = [];
@@ -90,7 +91,7 @@ class MatchExpression<input, output> {
9091
this.input
9192
),
9293
}
93-
: { matched: false as const };
94+
: unmatched;
9495

9596
return new MatchExpression(this.input, state);
9697
}
@@ -107,7 +108,7 @@ class MatchExpression<input, output> {
107108
this.input,
108109
matched
109110
? { matched: true, value: handler(this.input, this.input) }
110-
: { matched: false, value: undefined }
111+
: unmatched
111112
);
112113
}
113114

0 commit comments

Comments
 (0)