Skip to content

Commit a1b4579

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(core): avoid unnecessary lookup when writing inputs (#59980)
Currently we resolve the DOM node when writing inputs up-front, because it's necessary for the `ng-reflect-` attributes. Since the attributes are dev-mode-only, we can move the resolution into the function that writes them so we can avoid the resolution when it's not used. PR Close #59980
1 parent d359650 commit a1b4579

File tree

1 file changed

+14
-28
lines changed
  • packages/core/src/render3/instructions

1 file changed

+14
-28
lines changed

packages/core/src/render3/instructions/shared.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,16 @@ export function elementPropertyInternal<T>(
263263
nativeOnly: boolean,
264264
): void {
265265
ngDevMode && assertNotSame(value, NO_CHANGE as any, 'Incoming value should never be NO_CHANGE.');
266-
const element = getNativeByTNode(tNode, lView) as RElement | RComment;
267266
let inputData = tNode.inputs;
268267
let dataValue: NodeInputBindings[typeof propName] | undefined;
269268
if (!nativeOnly && inputData != null && (dataValue = inputData[propName])) {
270269
setInputsForProperty(tView, lView, dataValue, propName, value);
271270
if (isComponentHost(tNode)) markDirtyIfOnPush(lView, tNode.index);
272271
if (ngDevMode) {
273-
setNgReflectProperties(lView, tView, element, tNode.type, dataValue, value);
272+
setNgReflectProperties(lView, tView, tNode, dataValue, value);
274273
}
275274
} else if (tNode.type & TNodeType.AnyRNode) {
275+
const element = getNativeByTNode(tNode, lView) as RElement | RComment;
276276
propName = mapPropName(propName);
277277

278278
if (ngDevMode) {
@@ -305,17 +305,12 @@ export function markDirtyIfOnPush(lView: LView, viewIndex: number): void {
305305
}
306306
}
307307

308-
function setNgReflectProperty(
309-
lView: LView,
310-
element: RElement | RComment,
311-
type: TNodeType,
312-
attrName: string,
313-
value: any,
314-
) {
308+
function setNgReflectProperty(lView: LView, tNode: TNode, attrName: string, value: any) {
309+
const element = getNativeByTNode(tNode, lView) as RElement | RComment;
315310
const renderer = lView[RENDERER];
316311
attrName = normalizeDebugBindingName(attrName);
317312
const debugValue = normalizeDebugBindingValue(value);
318-
if (type & TNodeType.AnyRNode) {
313+
if (tNode.type & TNodeType.AnyRNode) {
319314
if (value == null) {
320315
renderer.removeAttribute(element as RElement, attrName);
321316
} else {
@@ -332,25 +327,17 @@ function setNgReflectProperty(
332327
export function setNgReflectProperties(
333328
lView: LView,
334329
tView: TView,
335-
element: RElement | RComment,
336-
type: TNodeType,
337-
dataValue: NodeInputBindings[string],
330+
tNode: TNode,
331+
inputConfig: NodeInputBindings[string],
338332
value: any,
339333
) {
340-
if (type & (TNodeType.AnyRNode | TNodeType.Container)) {
341-
/**
342-
* dataValue is an array containing runtime input or output names for the directives:
343-
* i+0: directive instance index
344-
* i+1: privateName
345-
*
346-
* e.g. [0, 'change']
347-
* we want to set the reflected property with the privateName: dataValue[i+1]
348-
*/
349-
for (let i = 0; i < dataValue.length; i += 2) {
350-
const index = dataValue[i] as number;
351-
const lookupName = dataValue[i + 1] as string;
334+
if (tNode.type & (TNodeType.AnyRNode | TNodeType.Container)) {
335+
// Note: we set the private name of the input as the reflected property, not the public one.
336+
for (let i = 0; i < inputConfig.length; i += 2) {
337+
const index = inputConfig[i] as number;
338+
const lookupName = inputConfig[i + 1] as string;
352339
const def = tView.data[index] as DirectiveDef<unknown>;
353-
setNgReflectProperty(lView, element, type, def.inputs[lookupName][0], value);
340+
setNgReflectProperty(lView, tNode, def.inputs[lookupName][0], value);
354341
}
355342
}
356343
}
@@ -540,8 +527,7 @@ function setInputsFromAttrs<T>(
540527
writeToDirectiveInput<T>(def, instance, lookupName, value);
541528

542529
if (ngDevMode) {
543-
const nativeElement = getNativeByTNode(tNode, lView) as RElement;
544-
setNgReflectProperty(lView, nativeElement, tNode.type, lookupName, value);
530+
setNgReflectProperty(lView, tNode, def.inputs[lookupName][0], value);
545531
}
546532
}
547533
}

0 commit comments

Comments
 (0)