Skip to content

Commit e53104a

Browse files
committed
perf: pre-index interactive element schemas by tag name at module load
1 parent 9c9ee94 commit e53104a

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

lib/rules/template-no-interactive-element-to-noninteractive-role.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,24 @@ const { isNativeElement } = require('../utils/is-native-element');
3838

3939
const interactiveRoleSet = INTERACTIVE_ROLES;
4040

41-
const elementRoleEntries = [...elementRoles];
42-
43-
// Schemas (element name + attribute constraints) whose role list contains at
44-
// least one interactive role.
45-
const interactiveElementRoleSchemas = elementRoleEntries
46-
.filter(([, rolesArr]) => [...rolesArr].some((r) => interactiveRoleSet.has(r)))
47-
.map(([schema]) => schema);
41+
// Pre-index interactive element schemas by tag name at module load. aria-query's
42+
// elementRoles is static data; bucketing by tag turns the per-node O(N) scan
43+
// over all schemas into a direct O(1) Map lookup followed by checking only the
44+
// 1–5 schemas for that specific tag.
45+
const INTERACTIVE_SCHEMAS_BY_TAG = (() => {
46+
const index = new Map();
47+
for (const [schema, rolesArr] of elementRoles) {
48+
if ([...rolesArr].some((r) => interactiveRoleSet.has(r))) {
49+
if (!index.has(schema.name)) {
50+
index.set(schema.name, []);
51+
}
52+
index.get(schema.name).push(schema);
53+
}
54+
}
55+
return index;
56+
})();
4857

49-
const tagsWithInteractiveElementRoleEntry = new Set(
50-
interactiveElementRoleSchemas.map((schema) => schema.name)
51-
);
58+
const tagsWithInteractiveElementRoleEntry = new Set(INTERACTIVE_SCHEMAS_BY_TAG.keys());
5259

5360
// AX-fallback tag set — tags whose AXObject list is entirely widget AND which
5461
// have no interactive `elementRoles` entry. Excludes `canvas` per rationale
@@ -162,10 +169,7 @@ function isInteractiveElement(node) {
162169

163170
// Primary signal: elementRoles with at least one interactive role + schema
164171
// constraints match the node's attributes.
165-
for (const schema of interactiveElementRoleSchemas) {
166-
if (schema.name !== tag) {
167-
continue;
168-
}
172+
for (const schema of INTERACTIVE_SCHEMAS_BY_TAG.get(tag) ?? []) {
169173
if (!attributesMatchSchema(schema, node)) {
170174
continue;
171175
}

0 commit comments

Comments
 (0)