Skip to content

buildClientSchema: build enum type value maps lazily #4424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {

private _values:
| ReadonlyArray<GraphQLEnumValue /* <T> */>
| (() => GraphQLEnumValueConfigMap);
| (() => ReadonlyArray<GraphQLEnumValue>) /* <T> */;

private _valueLookup: ReadonlyMap<any /* T */, GraphQLEnumValue> | null;
private _nameLookup: ObjMap<GraphQLEnumValue> | null;
Expand All @@ -1534,13 +1534,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];

this._values =
typeof config.values === 'function'
? config.values
: Object.entries(config.values).map(
([valueName, valueConfig]) =>
new GraphQLEnumValue(this, valueName, valueConfig),
);
this._values = defineEnumValues.bind(undefined, this, config.values);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be surprised if the .bind() method ends up faster than the simpler/shorter/clearer version:

Suggested change
this._values = defineEnumValues.bind(undefined, this, config.values);
this._values = () => defineEnumValues(this, config.values);

Both require memory allocation, and traditionally .bind() has ended up slower than just using a new function, though the V8 team may have optimised that difference away by now!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could benchmark when I get a chance I think we are just using the convention from elsewhere in the file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with the simpler code if there's no perf difference. My guess is that .bind() was assumed to be more efficient, but I don't think it is.

this._valueLookup = null;
this._nameLookup = null;
}
Expand All @@ -1551,10 +1545,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {

getValues(): ReadonlyArray<GraphQLEnumValue /* <T> */> {
if (typeof this._values === 'function') {
this._values = Object.entries(this._values()).map(
([valueName, valueConfig]) =>
new GraphQLEnumValue(this, valueName, valueConfig),
);
this._values = this._values();
}
return this._values;
}
Expand Down Expand Up @@ -1684,6 +1675,18 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
}
}

function defineEnumValues(
parentEnum: GraphQLEnumType,
values: ThunkObjMap<GraphQLEnumValueConfig /* <T> */>,
): ReadonlyArray<GraphQLEnumValue> {
const valueMap = resolveObjMapThunk(values);

return Object.entries(valueMap).map(
([valueName, valueConfig]) =>
new GraphQLEnumValue(parentEnum, valueName, valueConfig),
);
}

function didYouMeanEnumValue(
enumType: GraphQLEnumType,
unknownValueStr: string,
Expand Down
Loading