@@ -31,6 +31,10 @@ export class Encoder {
31
31
readonly #info: ScopeInfo ;
32
32
readonly #names: string [ ] ;
33
33
34
+ // Hash map to resolve indices of strings in the "names" array. Otherwise we'd have
35
+ // to use 'indexOf' for every name we want to encode.
36
+ readonly #namesToIndex = new Map < string , number > ( ) ;
37
+
34
38
readonly #scopeState = { ...DEFAULT_SCOPE_STATE } ;
35
39
readonly #rangeState = { ...DEFAULT_RANGE_STATE } ;
36
40
#encodedItems: string [ ] = [ ] ;
@@ -42,6 +46,10 @@ export class Encoder {
42
46
constructor ( info : ScopeInfo , names : string [ ] ) {
43
47
this . #info = info ;
44
48
this . #names = names ;
49
+
50
+ for ( let i = 0 ; i < names . length ; ++ i ) {
51
+ this . #namesToIndex. set ( names [ i ] , i ) ;
52
+ }
45
53
}
46
54
47
55
encode ( ) : string {
@@ -223,11 +231,13 @@ export class Encoder {
223
231
}
224
232
225
233
#resolveNamesIdx( name : string ) : number {
226
- const index = this . #names . indexOf ( name ) ;
227
- if ( index >= 0 ) return index ;
234
+ const index = this . #namesToIndex . get ( name ) ;
235
+ if ( index !== undefined ) return index ;
228
236
237
+ const addedIndex = this . #names. length ;
229
238
this . #names. push ( name ) ;
230
- return this . #names. length - 1 ;
239
+ this . #namesToIndex. set ( name , addedIndex ) ;
240
+ return addedIndex ;
231
241
}
232
242
233
243
#verifyPositionWithScopeState( line : number , column : number ) {
0 commit comments