Skip to content

Commit c89d94b

Browse files
authored
refactor(core): guards stringify calls with ngDevMode
The `stringify` function is only needed for debugging purposes and should not be called in production mode.
1 parent 3874969 commit c89d94b

8 files changed

Lines changed: 21 additions & 16 deletions

File tree

packages/core/src/di/create_injector.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export function createInjectorWithoutInjectorInstances(
4747
scopes = new Set<InjectorScope>(),
4848
): R3Injector {
4949
const providers = [additionalProviders || EMPTY_ARRAY, importProvidersFrom(defType)];
50-
name = name || (typeof defType === 'object' ? undefined : stringify(defType));
50+
let source: string | undefined = undefined;
51+
if (ngDevMode) {
52+
source = name || (typeof defType === 'object' ? undefined : stringify(defType));
53+
}
5154

52-
return new R3Injector(providers, parent || getNullInjector(), name || null, scopes);
55+
return new R3Injector(providers, parent || getNullInjector(), source || null, scopes);
5356
}

packages/core/src/di/forward_ref.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ const __forward_ref__ = getClosureSafeProperty({__forward_ref__: getClosureSafeP
6868
*/
6969
export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
7070
(<any>forwardRefFn).__forward_ref__ = forwardRef;
71-
(<any>forwardRefFn).toString = function () {
72-
return stringify(this());
73-
};
71+
if (ngDevMode) {
72+
(<any>forwardRefFn).toString = function () {
73+
return stringify(this());
74+
};
75+
}
76+
7477
return <Type<any>>(<any>forwardRefFn);
7578
}
7679

packages/core/src/di/r3_injector.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,16 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
455455
}
456456

457457
override toString() {
458-
const tokens: string[] = [];
459-
const records = this.records;
460-
for (const token of records.keys()) {
461-
tokens.push(stringify(token));
458+
if (ngDevMode) {
459+
const tokens: string[] = [];
460+
const records = this.records;
461+
for (const token of records.keys()) {
462+
tokens.push(stringify(token));
463+
}
464+
return `R3Injector[${tokens.join(', ')}]`;
462465
}
463-
return `R3Injector[${tokens.join(', ')}]`;
466+
467+
return 'R3Injector[...]';
464468
}
465469

466470
/**
@@ -521,7 +525,7 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
521525
const prevConsumer = setActiveConsumer(null);
522526
try {
523527
if (record.value === CIRCULAR) {
524-
throw cyclicDependencyError(stringify(token));
528+
throw cyclicDependencyError(ngDevMode ? stringify(token) : '');
525529
} else if (record.value === NOT_YET) {
526530
record.value = CIRCULAR;
527531

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@
830830
"shouldBeIgnoredByZone",
831831
"shouldSearchParent",
832832
"storeLViewOnDestroy",
833-
"stringify",
834833
"stringifyCSSSelector",
835834
"stringifyCSSSelectorList",
836835
"style",

packages/core/test/bundling/create_component/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@
686686
"stashEventListenerImpl",
687687
"storeLViewOnDestroy",
688688
"storeListenerCleanup",
689-
"stringify",
690689
"stringifyCSSSelector",
691690
"stringifyCSSSelectorList",
692691
"syncViewWithBlueprint",

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,6 @@
725725
"shouldTriggerDeferBlock",
726726
"storeLViewOnDestroy",
727727
"storeTriggerCleanupFn",
728-
"stringify",
729728
"stringifyCSSSelector",
730729
"stringifyCSSSelectorList",
731730
"syncViewWithBlueprint",

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,6 @@
775775
"skipTextNodes",
776776
"sortAndConcatParams",
777777
"storeLViewOnDestroy",
778-
"stringify",
779778
"stringifyCSSSelector",
780779
"stringifyCSSSelectorList",
781780
"subscribeOn",

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@
612612
"shouldBeIgnoredByZone",
613613
"shouldSearchParent",
614614
"storeLViewOnDestroy",
615-
"stringify",
616615
"stringifyCSSSelector",
617616
"stringifyCSSSelectorList",
618617
"syncViewWithBlueprint",

0 commit comments

Comments
 (0)