Skip to content

Commit a79af0a

Browse files
committed
perf(linter): replace for (... of ...) loops (#13913)
Perf optimization to linter plugins code. Basic `for` loops are cheaper than `for (... of ...)`.
1 parent 5d3ba00 commit a79af0a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ async function loadPluginImpl(path: string): Promise<string> {
5959
// TODO: Use a validation library to assert the shape of the plugin, and of rules
6060
const pluginName = plugin.meta.name;
6161
const offset = registeredRules.length;
62-
const ruleNames = [];
62+
const { rules } = plugin;
63+
const ruleNames = Object.keys(rules);
64+
const ruleNamesLen = ruleNames.length;
6365

64-
for (const [ruleName, rule] of Object.entries(plugin.rules)) {
65-
ruleNames.push(ruleName);
66+
for (let i = 0; i < ruleNamesLen; i++) {
67+
const ruleName = ruleNames[i];
6668
registeredRules.push({
67-
rule,
69+
rule: rules[ruleName],
6870
context: new Context(`${pluginName}/${ruleName}`),
6971
});
7072
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,16 @@ export function addVisitorToCompiled(visitor: Visitor): void {
204204
}
205205

206206
// Exit if is empty visitor
207-
const keys = Object.keys(visitor);
208-
if (keys.length === 0) return;
207+
const keys = Object.keys(visitor),
208+
keysLen = keys.length;
209+
if (keysLen === 0) return;
209210

210211
hasActiveVisitors = true;
211212

212213
// Populate visitors array from provided object
213-
for (let name of keys) {
214+
for (let i = 0; i < keysLen; i++) {
215+
let name = keys[i];
216+
214217
const visitFn = visitor[name];
215218
if (typeof visitFn !== 'function') {
216219
throw new TypeError(`'${name}' property of visitor object is not a function`);
@@ -295,14 +298,19 @@ export function finalizeCompiledVisitor() {
295298

296299
// Merge visit functions for node types which have multiple visitors from different rules,
297300
// or enter+exit functions for leaf nodes
298-
for (const typeId of mergedLeafVisitorTypeIds) {
301+
for (let i = mergedLeafVisitorTypeIds.length - 1; i >= 0; i--) {
302+
const typeId = mergedLeafVisitorTypeIds[i];
299303
compiledVisitor[typeId] = mergeVisitFns(compiledVisitor[typeId] as unknown as VisitFn[]);
300304
}
301-
for (const typeId of mergedEnterVisitorTypeIds) {
305+
306+
for (let i = mergedEnterVisitorTypeIds.length - 1; i >= 0; i--) {
307+
const typeId = mergedEnterVisitorTypeIds[i];
302308
const enterExit = compiledVisitor[typeId] as CompilingNonLeafVisitorEntry;
303309
enterExit.enter = mergeVisitFns(enterExit.enter as VisitFn[]);
304310
}
305-
for (const typeId of mergedExitVisitorTypeIds) {
311+
312+
for (let i = mergedExitVisitorTypeIds.length - 1; i >= 0; i--) {
313+
const typeId = mergedExitVisitorTypeIds[i];
306314
const enterExit = compiledVisitor[typeId] as CompilingNonLeafVisitorEntry;
307315
enterExit.exit = mergeVisitFns(enterExit.exit as VisitFn[]);
308316
}

0 commit comments

Comments
 (0)