Skip to content

Commit e232d35

Browse files
committed
perf(linter/plugins): recycle objects in token methods (#16068)
Follow-on after #16044 and #16045. `getTokenOrCommentBefore` and `getTokenOrCommentAfter` both delegate to other methods, passing an options object. Instead of creating a new object on every call, reuse a single object which is cached at top level.
1 parent a9cf44a commit e232d35

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

apps/oxlint/src-js/plugins/tokens.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export interface TemplateToken extends BaseToken {
133133
type: 'Template';
134134
}
135135

136+
// `SkipOptions` object used by `getTokenOrCommentBefore` and `getTokenOrCommentAfter`.
137+
// This object is reused over and over to avoid creating a new options object on each call.
138+
const INCLUDE_COMMENTS_SKIP_OPTIONS: SkipOptions = { includeComments: true, skip: 0 };
139+
136140
// Tokens for the current file parsed by TS-ESLint.
137141
// Created lazily only when needed.
138142
export let tokens: Token[] | null = null;
@@ -803,7 +807,10 @@ export function getTokenBefore(
803807
* @returns `Token`, or `null` if all were skipped.
804808
*/
805809
export function getTokenOrCommentBefore(nodeOrToken: NodeOrToken | Comment, skip?: number): Token | null {
806-
return getTokenBefore(nodeOrToken, { includeComments: true, skip });
810+
// Equivalent to `return getTokenBefore(nodeOrToken, { includeComments: true, skip });`,
811+
// but reuse a global object to avoid creating a new object on each call
812+
INCLUDE_COMMENTS_SKIP_OPTIONS.skip = skip;
813+
return getTokenBefore(nodeOrToken, INCLUDE_COMMENTS_SKIP_OPTIONS);
807814
}
808815

809816
/**
@@ -1002,7 +1009,10 @@ export function getTokenAfter(
10021009
* @returns `Token`, or `null` if all were skipped.
10031010
*/
10041011
export function getTokenOrCommentAfter(nodeOrToken: NodeOrToken | Comment, skip?: number): Token | null {
1005-
return getTokenAfter(nodeOrToken, { includeComments: true, skip });
1012+
// Equivalent to `return getTokenAfter(nodeOrToken, { includeComments: true, skip });`,
1013+
// but reuse a global object to avoid creating a new object on each call
1014+
INCLUDE_COMMENTS_SKIP_OPTIONS.skip = skip;
1015+
return getTokenAfter(nodeOrToken, INCLUDE_COMMENTS_SKIP_OPTIONS);
10061016
}
10071017

10081018
/**

0 commit comments

Comments
 (0)