From 9f0c549bc8916897d16fb35cc8a3e8b98d5f8cf1 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 4 Oct 2019 12:13:25 +0200 Subject: [PATCH] perf(ivy): avoid memory allocation in the isAnimationProp check (#32997) Accessing a string's character at index allocates a new, single character string. A better (faster) check is to use `charCodeAt` that doesn't trigger allocation. This simple change speeds up the element_text_create benchmark by ~7%. PR Close #32997 --- packages/core/src/render3/instructions/shared.ts | 5 ++--- packages/core/src/render3/util/attrs_utils.ts | 7 ++++--- .../test/bundling/cyclic_import/bundle.golden_symbols.json | 3 --- .../core/test/bundling/todo/bundle.golden_symbols.json | 3 --- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index c4706c9a54..0b9d24a3ce 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -33,7 +33,7 @@ import {isNodeMatchingSelectorList} from '../node_selector_matcher'; import {ActiveElementFlags, executeElementExitFn, getBindingsEnabled, getCheckNoChangesMode, getIsParent, getPreviousOrParentTNode, getSelectedIndex, hasActiveElementFlag, incrementActiveDirectiveId, namespaceHTMLInternal, selectView, setActiveHostElement, setBindingRoot, setCheckNoChangesMode, setCurrentDirectiveDef, setCurrentQueryIndex, setPreviousOrParentTNode, setSelectedIndex} from '../state'; import {renderStylingMap} from '../styling/bindings'; import {NO_CHANGE} from '../tokens'; -import {ANIMATION_PROP_PREFIX, isAnimationProp} from '../util/attrs_utils'; +import {isAnimationProp} from '../util/attrs_utils'; import {INTERPOLATION_DELIMITER, renderStringify, stringifyForError} from '../util/misc_utils'; import {getLViewParent} from '../util/view_traversal_utils'; import {getComponentViewByIndex, getNativeByIndex, getNativeByTNode, getTNode, isCreationMode, readPatchedLView, resetPreOrderHookFlags, unwrapRNode, viewAttachedToChangeDetector} from '../util/view_utils'; @@ -980,8 +980,7 @@ function validateProperty( // The property is considered valid if the element matches the schema, it exists on the element // or it is synthetic, and we are in a browser context (web worker nodes should be skipped). return matchingSchemas(hostView, tNode.tagName) || propName in element || - propName[0] === ANIMATION_PROP_PREFIX || typeof Node !== 'function' || - !(element instanceof Node); + isAnimationProp(propName) || typeof Node !== 'function' || !(element instanceof Node); } function matchingSchemas(hostView: LView, tagName: string | null): boolean { diff --git a/packages/core/src/render3/util/attrs_utils.ts b/packages/core/src/render3/util/attrs_utils.ts index 6249d6adf9..5d729c2c76 100644 --- a/packages/core/src/render3/util/attrs_utils.ts +++ b/packages/core/src/render3/util/attrs_utils.ts @@ -100,8 +100,9 @@ export function isNameOnlyAttributeMarker(marker: string | AttributeMarker | Css marker === AttributeMarker.I18n; } -export const ANIMATION_PROP_PREFIX = '@'; - export function isAnimationProp(name: string): boolean { - return name[0] === ANIMATION_PROP_PREFIX; + // Perf note: accessing charCodeAt to check for the first character of a string is faster as + // compared to accessing a character at index 0 (ex. name[0]). The main reason for this is that + // charCodeAt doesn't allocate memory to return a substring. + return name.charCodeAt(0) === 64; // @ } diff --git a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json index 8c761c8140..06e787207c 100644 --- a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json +++ b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json @@ -2,9 +2,6 @@ { "name": "ACTIVE_INDEX" }, - { - "name": "ANIMATION_PROP_PREFIX" - }, { "name": "BINDING_INDEX" }, diff --git a/packages/core/test/bundling/todo/bundle.golden_symbols.json b/packages/core/test/bundling/todo/bundle.golden_symbols.json index b7a2012325..bb63b82b26 100644 --- a/packages/core/test/bundling/todo/bundle.golden_symbols.json +++ b/packages/core/test/bundling/todo/bundle.golden_symbols.json @@ -2,9 +2,6 @@ { "name": "ACTIVE_INDEX" }, - { - "name": "ANIMATION_PROP_PREFIX" - }, { "name": "BINDING_INDEX" },