From 911d297136a41484b4d67df98c3fd1866a4479d1 Mon Sep 17 00:00:00 2001 From: Ed J Date: Sat, 17 Feb 2018 04:17:56 +0000 Subject: [PATCH] memoise wrapper values --- src/type/wrappers.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/type/wrappers.js b/src/type/wrappers.js index 8ebec67729..99edc9ae2e 100644 --- a/src/type/wrappers.js +++ b/src/type/wrappers.js @@ -34,12 +34,19 @@ declare class GraphQLList<+T: GraphQLType> { // Note: constructors cannot be used for covariant types. Drop the "new". constructor(ofType: any): void; } +const listCache = new WeakMap(); // eslint-disable-next-line no-redeclare export function GraphQLList(ofType) { if (this instanceof GraphQLList) { this.ofType = assertType(ofType); } else { - return new GraphQLList(ofType); + const cachedValue = listCache.get(ofType); + if (cachedValue !== undefined) { + return cachedValue; + } + const newValue = new GraphQLList(ofType); + listCache.set(ofType, newValue); + return newValue; } } @@ -75,12 +82,19 @@ declare class GraphQLNonNull<+T: GraphQLNullableType> { // Note: constructors cannot be used for covariant types. Drop the "new". constructor(ofType: any): void; } +const nnCache = new WeakMap(); // eslint-disable-next-line no-redeclare export function GraphQLNonNull(ofType) { if (this instanceof GraphQLNonNull) { this.ofType = assertNullableType(ofType); } else { - return new GraphQLNonNull(ofType); + const cachedValue = nnCache.get(ofType); + if (cachedValue !== undefined) { + return cachedValue; + } + const newValue = new GraphQLNonNull(ofType); + nnCache.set(ofType, newValue); + return newValue; } }