Skip to content

Commit ce538c7

Browse files
committed
perf(linter/plugins): load methods of globals into local vars (#14073)
Small perf optimization. All the code in `apps/oxlint` loads methods of global object e.g. `Object`, `Array` into local vars, for faster access. Continue that convention in these files.
1 parent 6cfce80 commit ce538c7

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Context } from './plugins/context.ts';
22
import type { CreateOnceRule, Plugin, Rule } from './plugins/load.ts';
33
import type { BeforeHook, Visitor, VisitorWithHooks } from './plugins/types.ts';
44

5-
const { defineProperty, getPrototypeOf, hasOwn, setPrototypeOf } = Object;
5+
const { defineProperty, getPrototypeOf, hasOwn, setPrototypeOf, create: ObjectCreate } = Object;
66

77
const dummyOptions: unknown[] = [],
88
dummyReport = () => {};
@@ -104,7 +104,7 @@ function createContextAndVisitor(rule: CreateOnceRule): {
104104
// Really, `context` should be an instance of `Context`, which would throw error on accessing e.g. `id`
105105
// in body of `createOnce`. But any such bugs should have been caught when testing the rule in Oxlint,
106106
// so should be OK to take this shortcut.
107-
const context = Object.create(null, {
107+
const context = ObjectCreate(null, {
108108
id: { value: '', enumerable: true, configurable: true },
109109
options: { value: dummyOptions, enumerable: true, configurable: true },
110110
report: { value: dummyReport, enumerable: true, configurable: true },

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { getErrorMessage } from './utils.js';
33

44
import type { AfterHook, BeforeHook, Visitor, VisitorWithHooks } from './types.ts';
55

6+
const ObjectKeys = Object.keys;
7+
68
// Linter plugin, comprising multiple rules
79
export interface Plugin {
810
meta: {
@@ -86,7 +88,7 @@ async function loadPluginImpl(path: string): Promise<string> {
8688
const pluginName = plugin.meta.name;
8789
const offset = registeredRules.length;
8890
const { rules } = plugin;
89-
const ruleNames = Object.keys(rules);
91+
const ruleNames = ObjectKeys(rules);
9092
const ruleNamesLen = ruleNames.length;
9193

9294
for (let i = 0; i < ruleNamesLen; i++) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ import { assertIs } from './utils.js';
8585

8686
import type { CompiledVisitorEntry, EnterExit, Node, VisitFn, Visitor } from './types.ts';
8787

88-
const { isArray } = Array;
88+
const ObjectKeys = Object.keys,
89+
{ isArray } = Array;
8990

9091
// Types for temporary state of entries of `compiledVisitor`
9192
// between calling `initCompiledVisitor` and `finalizeCompiledVisitor`.
@@ -210,7 +211,7 @@ export function addVisitorToCompiled(visitor: Visitor): void {
210211
}
211212

212213
// Exit if is empty visitor
213-
const keys = Object.keys(visitor),
214+
const keys = ObjectKeys(visitor),
214215
keysLen = keys.length;
215216
if (keysLen === 0) return;
216217

0 commit comments

Comments
 (0)