mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
vivian-hu
parent
b62f3f3b4a
commit
5947f7fd5b
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user