Skip to content

Commit fc114f7

Browse files
crisbetoVivian Hu
authored andcommitted
refactor(table): use DocumentFragment for faster DOM insertions (#14747)
Reworks some code in the table that was creating elements in a loop to use a `DocumentFragment` so it only hits the DOM once, rather than doing so for each element.
1 parent 30a310e commit fc114f7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/cdk/table/table.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,19 +965,23 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
965965

966966
/** Adds native table sections (e.g. tbody) and moves the row outlets into them. */
967967
private _applyNativeTableSections() {
968+
// @breaking-change 8.0.0 remove the `|| document` once the `_document` is a required param.
969+
const documentRef = this._document || document;
970+
const documentFragment = documentRef.createDocumentFragment();
968971
const sections = [
969972
{tag: 'thead', outlet: this._headerRowOutlet},
970973
{tag: 'tbody', outlet: this._rowOutlet},
971974
{tag: 'tfoot', outlet: this._footerRowOutlet},
972975
];
973976

974977
for (const section of sections) {
975-
// @breaking-change 8.0.0 remove the `|| document` once the `_document` is a required param.
976-
const documentRef = this._document || document;
977978
const element = documentRef.createElement(section.tag);
978979
element.appendChild(section.outlet.elementRef.nativeElement);
979-
this._elementRef.nativeElement.appendChild(element);
980+
documentFragment.appendChild(element);
980981
}
982+
983+
// Use a DocumentFragment so we don't hit the DOM on each iteration.
984+
this._elementRef.nativeElement.appendChild(documentFragment);
981985
}
982986

983987
/**

0 commit comments

Comments
 (0)