Skip to content

Commit 5aaefdb

Browse files
committed
fix: made type-metadata.storage.ts much faster
Changed the data modeling of type-metadata storage to store the data in maps instead of arrays. This results in a dramatic improvement of application startup time since many of the lookups for metadata are now in O(1) instead of O(n)
1 parent 316d442 commit 5aaefdb

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/graphql/lib/schema-builder/storages/type-metadata.storage.model.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ interface AllMetadata {
179179
}
180180

181181
export class TypeMetadataStorageModelList {
182-
private map = new Map<Function, TypeMetadataStorageModel>();
183-
private array = new Array<TypeMetadataStorageModel>();
182+
private storageMap = new Map<Function, TypeMetadataStorageModel>();
183+
private storageList = new Array<TypeMetadataStorageModel>();
184184

185185
public all: AllMetadata = {
186186
argumentType: [],
@@ -195,21 +195,27 @@ export class TypeMetadataStorageModelList {
195195
};
196196

197197
get(target: Function) {
198-
let metadata = this.map.get(target);
198+
let metadata = this.storageMap.get(target);
199199

200200
if (!metadata) {
201201
metadata = new TypeMetadataStorageModel(this.all);
202-
this.map.set(target, metadata);
203-
this.array.push(metadata);
202+
this.storageMap.set(target, metadata);
203+
this.storageList.push(metadata);
204204
}
205205

206206
return metadata;
207207
}
208208

209209
compile() {
210-
this.all.classDirectives.reverse();
211-
this.all.classExtensions.reverse();
212-
this.all.fieldDirectives.reverse();
213-
this.all.fieldExtensions.reverse();
210+
this.reversePredicate((t) => t.classDirectives);
211+
this.reversePredicate((t) => t.classExtensions);
212+
this.reversePredicate((t) => t.fieldDirectives.getAll());
213+
this.reversePredicate((t) => t.fieldExtensions.getAll());
214+
}
215+
216+
private reversePredicate<V>(
217+
predicate: (t: TypeMetadataStorageModel) => Array<V>,
218+
) {
219+
this.storageList.forEach((t) => predicate(t).reverse());
214220
}
215221
}

0 commit comments

Comments
 (0)