Skip to content

Commit 760f154

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 5b24aae commit 760f154

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
export class ArrayCollection<T> extends Array<T> {
2-
constructor(private globalArray: Array<T>) {
3-
super();
1+
export class ArrayCollection<T> {
2+
private array: T[] = [];
3+
4+
constructor(private globalArray: Array<T>) {}
5+
6+
getAll() {
7+
return this.array;
48
}
59

610
push(...items): number {
711
this.globalArray.push(...items);
8-
return super.push(...items);
12+
return this.array.push(...items);
913
}
1014

1115
unshift(...items): number {
1216
this.globalArray.unshift(...items);
13-
return super.unshift(...items);
17+
return this.array.unshift(...items);
18+
}
19+
20+
reverse() {
21+
return this.array.reverse();
22+
}
23+
24+
reduce<U>(
25+
callbackfn: (
26+
previousValue: U,
27+
currentValue: T,
28+
currentIndex: number,
29+
array: T[],
30+
) => U,
31+
initialValue: U,
32+
): U {
33+
return this.array.reduce(callbackfn, initialValue);
1434
}
1535
}

packages/graphql/lib/schema-builder/collections/metadata.storage.collection.list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class MetadataStorageCollectionList {
3030
}
3131

3232
compile() {
33-
this.reversePredicate((t) => t.classDirectives);
34-
this.reversePredicate((t) => t.classExtensions);
33+
this.reversePredicate((t) => t.classDirectives.getAll());
34+
this.reversePredicate((t) => t.classExtensions.getAll());
3535
this.reversePredicate((t) => t.fieldDirectives.getAll());
3636
this.reversePredicate((t) => t.fieldExtensions.getAll());
3737
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ export class TypeMetadataStorageHost {
257257
item.properties = this.getClassFieldsByPredicate(item);
258258
}
259259
if (!item.directives) {
260-
item.directives = this.targets.get(item.target).classDirectives;
260+
item.directives = this.targets
261+
.get(item.target)
262+
.classDirectives.getAll();
261263
}
262264
if (!item.extensions) {
263265
item.extensions = this.targets

0 commit comments

Comments
 (0)