Skip to content

Commit

Permalink
perf: prefer .forEach over for...in and for...of (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Dec 22, 2023
1 parent 33e9f2e commit 1938048
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 81 deletions.
8 changes: 6 additions & 2 deletions src/plugins/slick.checkboxselectcolumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ export class SlickCheckboxSelectColumn<T = any> implements SlickPlugin {
removeList.push(row);
}
}
for (const selectedRow in this._selectedRowsLookup) {
this._grid.invalidateRow(+selectedRow);
if (typeof this._selectedRowsLookup === 'object') {
Object.keys(this._selectedRowsLookup).forEach(selectedRow => {
if (selectedRow !== undefined) {
this._grid.invalidateRow(+selectedRow);
}
});
}
this._selectedRowsLookup = lookup;
this._grid.render();
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/slick.rowselectionmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ export class SlickRowSelectionModel {
protected rowsToRanges(rows: number[]) {
const ranges: SlickRange_[] = [];
const lastCell = this._grid.getColumns().length - 1;
for (let i = 0; i < rows.length; i++) {
ranges.push(new SlickRange(rows[i], 0, rows[i], lastCell));
}
rows.forEach(row => ranges.push(new SlickRange(row, 0, row, lastCell)));
return ranges;
}

Expand Down
17 changes: 8 additions & 9 deletions src/slick.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ export class SlickEventData<ArgType = any> {
// when we already have an event, we want to keep some of the event properties
// looping through some props is the only way to keep and sync these properties to the returned EventData
if (event) {
const eventProps = [
[
'altKey', 'ctrlKey', 'metaKey', 'shiftKey', 'key', 'keyCode',
'clientX', 'clientY', 'offsetX', 'offsetY', 'pageX', 'pageY',
'bubbles', 'type', 'which', 'x', 'y'
];
for (const key of eventProps) {
(this as any)[key] = event[key as keyof Event];
}
].forEach(key => (this as any)[key] = event[key as keyof Event]);
}
this.target = this.nativeEvent ? this.nativeEvent.target : undefined;
}
Expand Down Expand Up @@ -963,10 +960,12 @@ export class Utils {
}

public static applyDefaults(targetObj: any, srcObj: any) {
for (const key in srcObj) {
if (srcObj.hasOwnProperty(key) && !targetObj.hasOwnProperty(key)) {
targetObj[key] = srcObj[key];
}
if (typeof srcObj === 'object') {
Object.keys(srcObj).forEach(key => {
if (srcObj.hasOwnProperty(key) && !targetObj.hasOwnProperty(key)) {
targetObj[key] = srcObj[key];
}
});
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/slick.dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1579,11 +1579,13 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD

const storeCellCssStyles = (hash: CssStyleHash) => {
hashById = {};
for (const row in hash) {
if (hash) {
const id = this.rows[row as any][this.idProperty as keyof TData];
hashById[id] = hash[row];
}
if (typeof hash === 'object') {
Object.keys(hash).forEach(row => {
if (hash) {
const id = this.rows[row as any][this.idProperty as keyof TData];
hashById[id] = hash[row];
}
});
}
};

Expand All @@ -1592,18 +1594,16 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
storeCellCssStyles(grid.getCellCssStyles(key));

const update = () => {
if (hashById) {
if (typeof hashById === 'object') {
inHandler = true;
this.ensureRowsByIdCache();
const newHash: CssStyleHash = {};
for (const id in hashById) {
if (hashById) {
const row = this.rowsById?.[id];
if (Utils.isDefined(row)) {
newHash[row] = hashById[id];
}
Object.keys(hashById).forEach(id => {
const row = this.rowsById?.[id];
if (Utils.isDefined(row)) {
newHash[row as number] = hashById[id];
}
}
});
grid.setCellCssStyles(key, newHash);
inHandler = false;
}
Expand Down
126 changes: 72 additions & 54 deletions src/slick.grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
/** handles "display:none" on container or container parents, related to issue: https://github.com/6pac/SlickGrid/issues/568 */
cacheCssForHiddenInit() {
this._hiddenParents = Utils.parents(this._container, ':hidden') as HTMLElement[];
for (const el of this._hiddenParents) {
this._hiddenParents.forEach(el => {
const old: Partial<CSSStyleDeclaration> = {};
for (const name in this.cssShow) {
if (this.cssShow) {
Expand All @@ -954,20 +954,22 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
}
}
this.oldProps.push(old);
}
});
}

restoreCssFromHiddenInit() {
// finish handle display:none on container or container parents
// - put values back the way they were
let i = 0;
for (const el of this._hiddenParents) {
const old = this.oldProps[i++];
for (const name in this.cssShow) {
if (this.cssShow) {
el.style[name as CSSStyleDeclarationWritable] = (old as any)[name];
if (this._hiddenParents) {
this._hiddenParents.forEach(el => {
const old = this.oldProps[i++];
for (const name in this.cssShow) {
if (this.cssShow) {
el.style[name as CSSStyleDeclarationWritable] = (old as any)[name];
}
}
}
});
}
}

Expand Down Expand Up @@ -2435,9 +2437,9 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

const sheet = this._style.sheet;
if (sheet) {
for (const rule of rules) {
rules.forEach(rule => {
sheet.insertRule(rule);
}
});

for (let i = 0; i < this.columns.length; i++) {
if (!this.columns[i] || this.columns[i].hidden) { continue; }
Expand Down Expand Up @@ -4029,11 +4031,11 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
cellDiv.className = `${cellCss} ${addlCssClasses || ''}`.trim();
cellDiv.setAttribute('title', toolTipText);
if (m.hasOwnProperty('cellAttrs') && m.cellAttrs instanceof Object) {
for (const key in m.cellAttrs) {
Object.keys(m.cellAttrs).forEach(key => {
if (m.cellAttrs.hasOwnProperty(key)) {
cellDiv.setAttribute(key, m.cellAttrs[key]);
}
}
});
}

// if there is a corresponding row (if not, this is the Add New row or this data hasn't been loaded yet)
Expand Down Expand Up @@ -4089,12 +4091,18 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
if (this.currentEditor) {
this.makeActiveCellNormal();
}
for (const row in this.rowsCache) {
if (this.rowsCache) {
this.removeRowFromCache(+row);
}

if (typeof this.rowsCache === 'object') {
Object.keys(this.rowsCache).forEach(row => {
if (this.rowsCache) {
this.removeRowFromCache(+row);
}
});
}

if (this._options.enableAsyncPostRenderCleanup) {
this.startPostProcessingCleanup();
}
if (this._options.enableAsyncPostRenderCleanup) { this.startPostProcessingCleanup(); }
}

/**
Expand Down Expand Up @@ -4131,16 +4139,18 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
this.postProcessgroupId++;

// store and detach node for later async cleanup
for (const columnIdx in postProcessedRow) {
if (postProcessedRow.hasOwnProperty(columnIdx)) {
this.postProcessedCleanupQueue.push({
actionType: 'C',
groupId: this.postProcessgroupId,
node: cacheEntry.cellNodesByColumnIdx[+columnIdx],
columnIdx: +columnIdx,
rowIdx
});
}
if (typeof postProcessedRow === 'object') {
Object.keys(postProcessedRow).forEach(columnIdx => {
if (postProcessedRow.hasOwnProperty(columnIdx)) {
this.postProcessedCleanupQueue.push({
actionType: 'C',
groupId: this.postProcessgroupId,
node: cacheEntry.cellNodesByColumnIdx[+columnIdx],
columnIdx: +columnIdx,
rowIdx
});
}
});
}

if (!cacheEntry.rowNode) {
Expand Down Expand Up @@ -4459,11 +4469,15 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
// remove the rows that are now outside of the data range
// this helps avoid redundant calls to .removeRow() when the size of the data decreased by thousands of rows
const r1 = dataLength - 1;
for (const i in this.rowsCache) {
if (Number(i) > r1) {
this.removeRowFromCache(+i);
}
if (typeof this.rowsCache === 'object') {
Object.keys(this.rowsCache).forEach(row => {
const cachedRow = +row;
if (cachedRow > r1) {
this.removeRowFromCache(cachedRow);
}
});
}

if (this._options.enableAsyncPostRenderCleanup) {
this.startPostProcessingCleanup();
}
Expand Down Expand Up @@ -4845,10 +4859,12 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

protected invalidatePostProcessingResults(row: number) {
// change status of columns to be re-rendered
for (const columnIdx in this.postProcessedRows[row]) {
if (this.postProcessedRows[row].hasOwnProperty(columnIdx)) {
this.postProcessedRows[row][columnIdx] = 'C';
}
if (typeof this.postProcessedRows[row] === 'object') {
Object.keys(this.postProcessedRows[row]).forEach(columnIdx => {
if (this.postProcessedRows[row].hasOwnProperty(columnIdx)) {
this.postProcessedRows[row][columnIdx] = 'C';
}
});
}
this.postProcessFromRow = Math.min(this.postProcessFromRow as number, row);
this.postProcessToRow = Math.max(this.postProcessToRow as number, row);
Expand Down Expand Up @@ -5170,33 +5186,35 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
let columnId: number | string;
let addedRowHash;
let removedRowHash;
for (const row in this.rowsCache) {
if (this.rowsCache) {
removedRowHash = removedHash?.[row];
addedRowHash = addedHash?.[row];

if (removedRowHash) {
for (columnId in removedRowHash) {
if (!addedRowHash || removedRowHash[columnId] !== addedRowHash[columnId]) {
node = this.getCellNode(+row, this.getColumnIndex(columnId));
if (node) {
node.classList.remove(removedRowHash[columnId]);
if (typeof this.rowsCache === 'object') {
Object.keys(this.rowsCache).forEach(row => {
if (this.rowsCache) {
removedRowHash = removedHash?.[row];
addedRowHash = addedHash?.[row];

if (removedRowHash) {
for (columnId in removedRowHash) {
if (!addedRowHash || removedRowHash[columnId] !== addedRowHash[columnId]) {
node = this.getCellNode(+row, this.getColumnIndex(columnId));
if (node) {
node.classList.remove(removedRowHash[columnId]);
}
}
}
}
}

if (addedRowHash) {
for (columnId in addedRowHash) {
if (!removedRowHash || removedRowHash[columnId] !== addedRowHash[columnId]) {
node = this.getCellNode(+row, this.getColumnIndex(columnId));
if (node) {
node.classList.add(addedRowHash[columnId]);
if (addedRowHash) {
for (columnId in addedRowHash) {
if (!removedRowHash || removedRowHash[columnId] !== addedRowHash[columnId]) {
node = this.getCellNode(+row, this.getColumnIndex(columnId));
if (node) {
node.classList.add(addedRowHash[columnId]);
}
}
}
}
}
}
});
}
}

Expand Down

0 comments on commit 1938048

Please sign in to comment.