perf(search): avoid intermediate collections for one alias (#12252)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 06:11:52 -05:00
1 parent 7419328ea7
commit 51e48b8a28
2 files changed
+26 -7

No files matched your search

+16
View File
@@ -3,6 +3,7 @@ import { expect, it } from "vitest";
import {
analysisSearchPredicate,
literalSubstringPredicate,
matchProcedureNames,
} from "./analysisSearch.js";
it("uses Unicode simple case folding for literal substrings", () => {
@@ -85,3 +86,18 @@ it("keeps explicit regex semantics and invalid-pattern input errors", () => {
"search",
);
});
it("preserves single-alias matches, primary fallback and duplicate ordering", () => {
const row = { value: "qualified::name" };
const matches = (name: string) => name.includes("name");
expect(matchProcedureNames(row, ["other"], matches)).toBe(row);
expect(matchProcedureNames(row, [row.value], matches)).toBe(row);
expect(matchProcedureNames(row, ["name"], matches)).toEqual({
value: row.value,
matched_aliases: ["name"],
});
expect(matchProcedureNames(row, ["name", "name2", "name"], matches)).toEqual({
value: row.value,
matched_aliases: ["name", "name2"],
});
});
+10 -7
View File
@@ -55,12 +55,15 @@ export const matchProcedureNames = <Row extends { readonly value: string }>(
aliases: readonly string[],
matches: (value: string) => boolean,
): (Row & { readonly matched_aliases?: string[] }) | undefined => {
const matched =
aliases.length === 0
? []
: [...new Set(aliases)].filter(
(alias) => alias !== row.value && matches(alias),
);
if (matched.length > 0) return { ...row, matched_aliases: matched };
if (aliases.length === 1) {
const alias = aliases[0]!;
if (alias !== row.value && matches(alias))
return { ...row, matched_aliases: [alias] };
} else if (aliases.length > 1) {
const matched = [...new Set(aliases)].filter(
(alias) => alias !== row.value && matches(alias),
);
if (matched.length > 0) return { ...row, matched_aliases: matched };
}
return matches(row.value) ? row : undefined;
};