Skip to content

Commit

Permalink
perf(ssr): remove filterProperties (#5101)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson authored Jan 7, 2025
1 parent b2468c9 commit 0b12680
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
8 changes: 1 addition & 7 deletions packages/@lwc/ssr-compiler/src/compile-js/generate-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ const bGenerateMarkup = esTemplate`
tagName = tagName ?? ${/*component tag name*/ is.literal};
attrs = attrs ?? Object.create(null);
props = props ?? Object.create(null);
props = __filterProperties(
props,
publicFields,
privateFields,
);
const instance = new ${/* Component class */ is.identifier}({
tagName: tagName.toUpperCase(),
});
__establishContextfulRelationship(contextfulParent, instance);
${/*connect wire*/ is.statement}
instance[__SYMBOL__SET_INTERNALS](props, attrs);
instance[__SYMBOL__SET_INTERNALS](props, attrs, publicFields, privateFields);
instance.isConnected = true;
if (instance.connectedCallback) {
__mutationTracker.enable(instance);
Expand Down Expand Up @@ -128,7 +123,6 @@ export function addGenerateMarkupFunction(
program.body.unshift(
bImportDeclaration({
fallbackTmpl: '__fallbackTmpl',
filterProperties: '__filterProperties',
hasScopedStaticStylesheets: undefined,
mutationTracker: '__mutationTracker',
renderAttrs: '__renderAttrs',
Expand Down
1 change: 0 additions & 1 deletion packages/@lwc/ssr-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export {
SYMBOL__SET_INTERNALS,
} from './lightning-element';
export { mutationTracker } from './mutation-tracker';
export { filterProperties } from './reflection';
export {
fallbackTmpl,
fallbackTmplNoYield,
Expand Down
35 changes: 32 additions & 3 deletions packages/@lwc/ssr-runtime/src/lightning-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
// and be located before import statements.
// /// <reference lib="dom" />

import { assign, defineProperties, hasOwnProperty, StringToLowerCase, toString } from '@lwc/shared';
import {
assign,
defineProperties,
hasOwnProperty,
htmlPropertyToAttribute,
isAriaAttribute,
keys,
REFLECTIVE_GLOBAL_PROPERTY_SET,
StringToLowerCase,
toString,
} from '@lwc/shared';

import { ClassList } from './class-list';
import { mutationTracker } from './mutation-tracker';
Expand Down Expand Up @@ -64,10 +74,29 @@ export class LightningElement implements PropsAvailableAtConstruction {
assign(this, propsAvailableAtConstruction);
}

[SYMBOL__SET_INTERNALS](props: Properties, attrs: Attributes) {
[SYMBOL__SET_INTERNALS](
props: Properties,
attrs: Attributes,
publicFields: Set<string>,
privateFields: Set<string>
) {
this.#props = props;
this.#attrs = attrs;
assign(this, props);

// Avoid setting the following types of properties that should not be set:
// - Properties that are not public.
// - Properties that are not global.
// - Properties that are global but are internally overridden.
for (const propName of keys(props)) {
const attrName = htmlPropertyToAttribute(propName);
if (
publicFields.has(propName) ||
((REFLECTIVE_GLOBAL_PROPERTY_SET.has(propName) || isAriaAttribute(attrName)) &&
!privateFields.has(propName))
) {
(this as any)[propName] = props[propName];
}
}
}

get className() {
Expand Down
37 changes: 1 addition & 36 deletions packages/@lwc/ssr-runtime/src/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,10 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import {
AriaAttrNameToPropNameMap,
create,
entries,
htmlPropertyToAttribute,
isAriaAttribute,
isNull,
keys,
REFLECTIVE_GLOBAL_PROPERTY_SET,
toString,
} from '@lwc/shared';
import { AriaAttrNameToPropNameMap, entries, isNull, toString } from '@lwc/shared';

import type { LightningElement } from './lightning-element';

/**
* Filters out the following types of properties that should not be set.
* - Properties that are not public.
* - Properties that are not global.
* - Properties that are global but are internally overridden.
*/
export function filterProperties(
props: Record<string, unknown>,
publicFields: Set<string>,
privateFields: Set<string>
): Record<string, unknown> {
const propsToAssign = create(null);
for (const propName of keys(props)) {
const attrName = htmlPropertyToAttribute(propName);
if (
publicFields.has(propName) ||
((REFLECTIVE_GLOBAL_PROPERTY_SET.has(propName) || isAriaAttribute(attrName)) &&
!privateFields.has(propName))
) {
propsToAssign[propName] = props[propName];
}
}
return propsToAssign;
}

/**
* Descriptor for IDL attribute reflections that merely reflect the string, e.g. `title`.
*/
Expand Down

0 comments on commit 0b12680

Please sign in to comment.