@@ -194,6 +194,38 @@ function findLineNumberBinarySearch(lineStartIndices, target) {
194194// Directive Comments
195195//-----------------------------------------------------------------------------
196196
197+ /**
198+ * Normalized globals for `ecmaVersion`, `sourceType`, and `globals`, keyed by `languageOptions`.
199+ * Files matching the same configs share one `languageOptions` object, so each set is built only once.
200+ * @type {WeakMap<Object, Record<string, "readonly"|"writable"|"off">> }
201+ */
202+ const configGlobalsCache = new WeakMap ( ) ;
203+
204+ /**
205+ * Names of enabled (not `"off"`) globals, keyed by a normalized globals object from `configGlobalsCache`.
206+ * Lets `addDeclaredGlobals()` skip copying and filtering globals for files without inline global comments.
207+ * @type {WeakMap<Object, string[]> }
208+ */
209+ const enabledConfigGlobalNamesCache = new WeakMap ( ) ;
210+
211+ /**
212+ * Gets the names of the globals in a normalized globals object that aren't turned off.
213+ * @param {Object } configGlobals The normalized globals declared in configuration.
214+ * @returns {string[] } The names of the enabled globals.
215+ */
216+ function getEnabledConfigGlobalNames ( configGlobals ) {
217+ let names = enabledConfigGlobalNamesCache . get ( configGlobals ) ;
218+
219+ if ( ! names ) {
220+ names = Object . keys ( configGlobals ) . filter (
221+ name => configGlobals [ name ] !== "off" ,
222+ ) ;
223+ enabledConfigGlobalNamesCache . set ( configGlobals , names ) ;
224+ }
225+
226+ return names ;
227+ }
228+
197229/**
198230 * Ensures that variables representing built-in properties of the Global Object,
199231 * and any globals declared by special block comments, are present in the global
@@ -208,15 +240,23 @@ function addDeclaredGlobals(
208240 configGlobals = Object . create ( null ) ,
209241 inlineGlobals = Object . create ( null ) ,
210242) {
211- const finalGlobals = { __proto__ : null , ...configGlobals } ;
243+ let finalGlobals = configGlobals ;
244+ let names ;
212245
213- for ( const [ name , data ] of Object . entries ( inlineGlobals ) ) {
214- finalGlobals [ name ] = data . value ;
215- }
246+ // Most files have no inline globals, so their names depend only on the (cached) config globals.
247+ if ( Object . keys ( inlineGlobals ) . length === 0 ) {
248+ names = getEnabledConfigGlobalNames ( configGlobals ) ;
249+ } else {
250+ finalGlobals = { __proto__ : null , ...configGlobals } ;
216251
217- const names = Object . keys ( finalGlobals ) . filter (
218- name => finalGlobals [ name ] !== "off" ,
219- ) ;
252+ for ( const [ name , data ] of Object . entries ( inlineGlobals ) ) {
253+ finalGlobals [ name ] = data . value ;
254+ }
255+
256+ names = Object . keys ( finalGlobals ) . filter (
257+ name => finalGlobals [ name ] !== "off" ,
258+ ) ;
259+ }
220260
221261 scopeManager . addGlobals ( names ) ;
222262
@@ -944,27 +984,33 @@ class SourceCode extends TokenStore {
944984 * @returns {void }
945985 */
946986 applyLanguageOptions ( languageOptions ) {
947- /*
948- * Add configured globals and language globals
949- *
950- * Using Object.assign instead of object spread for performance reasons
951- * https://github.com/eslint/eslint/issues/16302
952- */
953- const configGlobals = Object . assign (
954- Object . create ( null ) , // https://github.com/eslint/eslint/issues/18363
955- getGlobalsForEcmaVersion ( languageOptions . ecmaVersion ) ,
956- languageOptions . sourceType === "commonjs"
957- ? globals . commonjs
958- : void 0 ,
959- languageOptions . globals ,
960- ) ;
987+ let configGlobals = configGlobalsCache . get ( languageOptions ) ;
988+
989+ if ( ! configGlobals ) {
990+ /*
991+ * Add configured globals and language globals
992+ *
993+ * Using Object.assign instead of object spread for performance reasons
994+ * https://github.com/eslint/eslint/issues/16302
995+ */
996+ configGlobals = Object . assign (
997+ Object . create ( null ) , // https://github.com/eslint/eslint/issues/18363
998+ getGlobalsForEcmaVersion ( languageOptions . ecmaVersion ) ,
999+ languageOptions . sourceType === "commonjs"
1000+ ? globals . commonjs
1001+ : void 0 ,
1002+ languageOptions . globals ,
1003+ ) ;
9611004
962- /*
963- * `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
964- * typically be caught when validating a config anyway (validity for inline global comments is checked separately).
965- */
966- for ( const [ name , value ] of Object . entries ( configGlobals ) ) {
967- configGlobals [ name ] = normalizeConfigGlobal ( value ) ;
1005+ /*
1006+ * `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
1007+ * typically be caught when validating a config anyway (validity for inline global comments is checked separately).
1008+ */
1009+ for ( const [ name , value ] of Object . entries ( configGlobals ) ) {
1010+ configGlobals [ name ] = normalizeConfigGlobal ( value ) ;
1011+ }
1012+
1013+ configGlobalsCache . set ( languageOptions , configGlobals ) ;
9681014 }
9691015
9701016 const varsCache = this [ caches ] . get ( "vars" ) ;
0 commit comments