Skip to content
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

fix(core): Remove for-of syntax from core helpers for JSC memory reduction #3690

Merged
merged 10 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/plenty-carrots-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Remove `for-of` syntax from `@urql/core` helpers for JSC memory reduction.
3 changes: 2 additions & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
} else {
// If the current result has queued up an operation of the same
// key, then `stale` refers to it
for (const operation of queue) {
for (let i = 0; i < queue.length; i++) {
const operation = queue[i];
if (operation.key === result.operation.key) {
dispatched.delete(operation.key);
break;
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ function gql(parts: string | TemplateStringsArray /* arguments */) {
}

source.unshift(keyDocument(body));
for (const document of source) {
for (const definition of document.definitions) {
for (let i = 0; i < source.length; i++) {
for (let j = 0; j < source[i].definitions.length; j++) {
const definition = source[i].definitions[j];
if (definition.kind === Kind.FRAGMENT_DEFINITION) {
const name = definition.name.value;
const value = stringifyDocument(definition);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/utils/collectTypenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ interface EntityLike {

const collectTypes = (obj: EntityLike | EntityLike[], types: Set<string>) => {
if (Array.isArray(obj)) {
for (const item of obj) collectTypes(item, types);
for (let i = 0, l = obj.length; i < l; i++) {
collectTypes(obj[i], types);
}
} else if (typeof obj === 'object' && obj !== null) {
for (const key in obj) {
if (key === '__typename' && typeof obj[key] === 'string') {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const generateErrorMessage = (
let error = '';
if (networkErr) return `[Network] ${networkErr.message}`;
if (graphQlErrs) {
for (const err of graphQlErrs) {
for (let i = 0, l = graphQlErrs.length; i < l; i++) {
if (error) error += '\n';
error += `[GraphQL] ${err.message}`;
error += `[GraphQL] ${graphQlErrs[i].message}`;
}
}
return error;
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/utils/formatDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const formatNode = <
): FormattedNode<T> => {
if ('definitions' in node) {
const definitions: FormattedNode<DefinitionNode>[] = [];
for (const definition of node.definitions) {
const newDefinition = formatNode(definition);
for (let i = 0, l = node.definitions.length; i < l; i++) {
const newDefinition = formatNode(node.definitions[i]);
definitions.push(newDefinition);
}

Expand All @@ -27,7 +27,8 @@ const formatNode = <
if ('directives' in node && node.directives && node.directives.length) {
const directives: DirectiveNode[] = [];
const _directives = {};
for (const directive of node.directives) {
for (let i = 0, l = node.directives.length; i < l; i++) {
const directive = node.directives[i];
let name = directive.name.value;
if (name[0] !== '_') {
directives.push(directive);
Expand All @@ -43,7 +44,8 @@ const formatNode = <
const selections: FormattedNode<SelectionNode>[] = [];
let hasTypename = node.kind === Kind.OPERATION_DEFINITION;
if (node.selectionSet) {
for (const selection of node.selectionSet.selections || []) {
for (let i = 0, l = node.selectionSet.selections.length; i < l; i++) {
const selection = node.selectionSet.selections[i];
hasTypename =
hasTypename ||
(selection.kind === Kind.FIELD &&
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ export const createRequest = <
* @returns the operation's name contained within the document, or `undefined`
*/
export const getOperationName = (query: DocumentNode): string | undefined => {
for (const node of query.definitions) {
for (let i = 0, l = query.definitions.length; i < l; i++) {
const node = query.definitions[i];
if (node.kind === Kind.OPERATION_DEFINITION) {
return node.name ? node.name.value : undefined;
}
Expand All @@ -192,7 +193,8 @@ export const getOperationName = (query: DocumentNode): string | undefined => {
* @returns the operation's type contained within the document, or `undefined`
*/
export const getOperationType = (query: DocumentNode): string | undefined => {
for (const node of query.definitions) {
for (let i = 0, l = query.definitions.length; i < l; i++) {
const node = query.definitions[i];
if (node.kind === Kind.OPERATION_DEFINITION) {
return node.operation;
}
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/utils/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ export const makeResult = (

const deepMerge = (target: any, source: any): any => {
if (typeof target === 'object' && target != null) {
if (Array.isArray(target)) {
target = [...target];
for (let i = 0, l = source.length; i < l; i++)
target[i] = deepMerge(target[i], source[i]);
}
if (
!target.constructor ||
target.constructor === Object ||
Array.isArray(target)
) {
target = Array.isArray(target) ? [...target] : { ...target };
for (const key of Object.keys(source))
target = { ...target };
for (const key in source)
target[key] = deepMerge(target[key], source[key]);
return target;
}
Expand Down Expand Up @@ -108,7 +113,8 @@ export const mergeResultPatch = (

const withData = { data: prevResult.data };
if (incremental) {
for (const patch of incremental) {
for (let i = 0, l = incremental.length; i < l; i++) {
const patch = incremental[i];
if (Array.isArray(patch.errors)) {
errors.push(...(patch.errors as any));
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/utils/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const stringify = (x: any, includeFiles: boolean): string => {
return stringify(x.toJSON(), includeFiles);
} else if (Array.isArray(x)) {
let out = '[';
for (const value of x) {
for (let i = 0, l = x.length; i < l; i++) {
if (out.length > 1) out += ',';
out += stringify(value, includeFiles) || 'null';
out += stringify(x[i], includeFiles) || 'null';
}
out += ']';
return out;
Expand All @@ -39,11 +39,11 @@ const stringify = (x: any, includeFiles: boolean): string => {

seen.add(x);
let out = '{';
for (const key of keys) {
const value = stringify(x[key], includeFiles);
for (let i = 0, l = keys.length; i < l; i++) {
const value = stringify(x[keys[i]], includeFiles);
if (value) {
if (out.length > 1) out += ',';
out += stringify(key, includeFiles) + ':' + value;
out += stringify(keys[i], includeFiles) + ':' + value;
}
}

Expand All @@ -62,7 +62,7 @@ const extract = (map: FileMap, path: string, x: any): void => {
map.set(path, x as File | Blob);
} else {
seen.add(x);
for (const key of Object.keys(x)) extract(map, `${path}.${key}`, x[key]);
for (const key in x) extract(map, `${path}.${key}`, x[key]);
}
};

Expand Down