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.
This commit is contained in:
Kristiyan Kostadinov
2019-01-15 00:40:20 +01:00
committed by vivian-hu
parent b62f3f3b4a
commit 5947f7fd5b
+7 -3
View File
@@ -965,6 +965,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
/** Adds native table sections (e.g. tbody) and moves the row outlets into them. */
private _applyNativeTableSections() {
// @breaking-change 8.0.0 remove the `|| document` once the `_document` is a required param.
const documentRef = this._document || document;
const documentFragment = documentRef.createDocumentFragment();
const sections = [
{tag: 'thead', outlet: this._headerRowOutlet},
{tag: 'tbody', outlet: this._rowOutlet},
@@ -972,12 +975,13 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
];
for (const section of sections) {
// @breaking-change 8.0.0 remove the `|| document` once the `_document` is a required param.
const documentRef = this._document || document;
const element = documentRef.createElement(section.tag);
element.appendChild(section.outlet.elementRef.nativeElement);
this._elementRef.nativeElement.appendChild(element);
documentFragment.appendChild(element);
}
// Use a DocumentFragment so we don't hit the DOM on each iteration.
this._elementRef.nativeElement.appendChild(documentFragment);
}
/**