Skip to content

Commit d4b123a

Browse files
Copilotzdrawku
andcommitted
Optimize performance: Remove bulk date parsing, rely on on-demand conversion
Co-authored-by: zdrawku <11193764+zdrawku@users.noreply.github.com>
1 parent 9c8ea6e commit d4b123a

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24012401
const columnDimensions: IPivotDimension[] = [];
24022402
const rowDimensions: IPivotDimension[] = [];
24032403
const values: IPivotValue[] = [];
2404-
const dateFields: string[] = [];
24052404
let isFirstDate = true;
24062405

24072406
fields.forEach((field) => {
@@ -2425,7 +2424,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24252424
}
24262425
case "date":
24272426
{
2428-
dateFields.push(field);
24292427
const dimension: IPivotDimension = new IgxPivotDateDimension(
24302428
{
24312429
memberName: field,
@@ -2449,11 +2447,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24492447
}
24502448
});
24512449

2452-
// Parse date-like values (timestamps and date strings) into Date objects
2453-
if (dateFields.length > 0) {
2454-
this.parseDateFields(data, dateFields);
2455-
}
2456-
24572450
const config: IPivotConfiguration = {
24582451
columns: columnDimensions,
24592452
rows: rowDimensions,
@@ -2462,39 +2455,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24622455
this.pivotConfiguration = config;
24632456
}
24642457

2465-
/**
2466-
* @hidden
2467-
* Parses date-like values (timestamps and date strings) in the data into Date objects
2468-
*/
2469-
private parseDateFields(data: any[], dateFields: string[]): void {
2470-
if (!data || data.length === 0) {
2471-
return;
2472-
}
2473-
2474-
for (const record of data) {
2475-
for (const field of dateFields) {
2476-
const value = record[field];
2477-
// Skip if already a Date object or null/undefined
2478-
if (!value || value instanceof Date) {
2479-
continue;
2480-
}
2481-
2482-
// Convert timestamp or date string to Date object
2483-
if (typeof value === 'number' || typeof value === 'string') {
2484-
try {
2485-
const parsedDate = new Date(value);
2486-
if (!isNaN(parsedDate.getTime())) {
2487-
record[field] = parsedDate;
2488-
}
2489-
} catch (e) {
2490-
// If parsing fails, leave the original value
2491-
console.warn(`Failed to parse date value for field '${field}':`, value);
2492-
}
2493-
}
2494-
}
2495-
}
2496-
}
2497-
24982458
protected createColumnForDimension(value: any, data: any, parent: ColumnType, isGroup: boolean) {
24992459
const key = value.value;
25002460
const ref = isGroup ?

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.spec.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ describe('IgxPivotGrid #pivotGrid', () => {
20832083
expect(pivotGrid.values.map(x => x.enabled)).toEqual([true, true]);
20842084
});
20852085

2086-
it('should auto-generate pivot config and parse date fields from timestamp values', () => {
2086+
it('should auto-generate pivot config and detect date fields from timestamp values', () => {
20872087
const pivotGrid = fixture.componentInstance.pivotGrid;
20882088
pivotGrid.pivotConfiguration = undefined;
20892089
pivotGrid.autoGenerateConfig = true;
@@ -2105,27 +2105,28 @@ describe('IgxPivotGrid #pivotGrid', () => {
21052105
expect(pivotGrid.pivotConfiguration.rows.map(x => x.memberName)).toEqual(['AllPeriods']);
21062106
expect(pivotGrid.pivotConfiguration.rows.map(x => x.enabled)).toEqual([true]);
21072107

2108-
// Verify the timestamp was converted to a Date object
2109-
expect(pivotGrid.data[0].Date instanceof Date).toBe(true);
2110-
expect(pivotGrid.data[0].Date.getTime()).toBe(TEST_TIMESTAMP);
2108+
// Verify the timestamp is still in its original format (not pre-parsed)
2109+
// IgxPivotDateDimension will handle parsing on-demand
2110+
expect(pivotGrid.data[0].Date).toBe(TEST_TIMESTAMP);
21112111

21122112
// values should be UnitPrice and UnitsSold (not Date)
21132113
expect(pivotGrid.values.length).toEqual(2);
21142114
expect(pivotGrid.values.map(x => x.member)).toEqual(['UnitPrice', 'UnitsSold']);
21152115
});
21162116

2117-
it('should auto-generate pivot config and parse date fields from ISO date strings', () => {
2117+
it('should auto-generate pivot config and detect date fields from ISO date strings', () => {
21182118
const pivotGrid = fixture.componentInstance.pivotGrid;
21192119
pivotGrid.pivotConfiguration = undefined;
21202120
pivotGrid.autoGenerateConfig = true;
21212121

21222122
// Test with ISO date string
2123+
const TEST_DATE_STRING = '2008-06-20';
21232124
pivotGrid.data = [{
21242125
ProductCategory: 'Clothing',
21252126
UnitPrice: 12.81,
21262127
SellerName: 'Stanley',
21272128
Country: 'Bulgaria',
2128-
Date: '2008-06-20',
2129+
Date: TEST_DATE_STRING,
21292130
UnitsSold: 282
21302131
}];
21312132
fixture.detectChanges();
@@ -2135,25 +2136,24 @@ describe('IgxPivotGrid #pivotGrid', () => {
21352136
expect(pivotGrid.pivotConfiguration.rows.map(x => x.memberName)).toEqual(['AllPeriods']);
21362137
expect(pivotGrid.pivotConfiguration.rows.map(x => x.enabled)).toEqual([true]);
21372138

2138-
// Verify the date string was converted to a Date object
2139-
expect(pivotGrid.data[0].Date instanceof Date).toBe(true);
2140-
expect(pivotGrid.data[0].Date.getFullYear()).toBe(2008);
2141-
expect(pivotGrid.data[0].Date.getMonth()).toBe(5); // June is month 5 (0-indexed)
2142-
expect(pivotGrid.data[0].Date.getDate()).toBe(20);
2139+
// Verify the date string is still in its original format (not pre-parsed)
2140+
// IgxPivotDateDimension will handle parsing on-demand
2141+
expect(pivotGrid.data[0].Date).toBe(TEST_DATE_STRING);
21432142
});
21442143

2145-
it('should auto-generate pivot config and parse date fields from US format date strings', () => {
2144+
it('should auto-generate pivot config and detect date fields from US format date strings', () => {
21462145
const pivotGrid = fixture.componentInstance.pivotGrid;
21472146
pivotGrid.pivotConfiguration = undefined;
21482147
pivotGrid.autoGenerateConfig = true;
21492148

21502149
// Test with US format date string (MM/DD/YYYY)
2150+
const TEST_US_DATE_STRING = '10/24/2025';
21512151
pivotGrid.data = [{
21522152
ProductCategory: 'Clothing',
21532153
UnitPrice: 12.81,
21542154
SellerName: 'Stanley',
21552155
Country: 'Bulgaria',
2156-
Date: '10/24/2025',
2156+
Date: TEST_US_DATE_STRING,
21572157
UnitsSold: 282
21582158
}];
21592159
fixture.detectChanges();
@@ -2163,11 +2163,9 @@ describe('IgxPivotGrid #pivotGrid', () => {
21632163
expect(pivotGrid.pivotConfiguration.rows.map(x => x.memberName)).toEqual(['AllPeriods']);
21642164
expect(pivotGrid.pivotConfiguration.rows.map(x => x.enabled)).toEqual([true]);
21652165

2166-
// Verify the date string was converted to a Date object
2167-
expect(pivotGrid.data[0].Date instanceof Date).toBe(true);
2168-
expect(pivotGrid.data[0].Date.getFullYear()).toBe(2025);
2169-
expect(pivotGrid.data[0].Date.getMonth()).toBe(9); // October is month 9 (0-indexed)
2170-
expect(pivotGrid.data[0].Date.getDate()).toBe(24);
2166+
// Verify the date string is still in its original format (not pre-parsed)
2167+
// IgxPivotDateDimension will handle parsing on-demand
2168+
expect(pivotGrid.data[0].Date).toBe(TEST_US_DATE_STRING);
21712169
});
21722170

21732171
it('should handle multiple date fields and enable only the first one', () => {
@@ -2196,9 +2194,10 @@ describe('IgxPivotGrid #pivotGrid', () => {
21962194
expect(rowDimensions[0].enabled).toBe(true);
21972195
expect(rowDimensions[1].enabled).toBe(false);
21982196

2199-
// Verify both date values were parsed
2200-
expect(pivotGrid.data[0].StartDate instanceof Date).toBe(true);
2201-
expect(pivotGrid.data[0].EndDate instanceof Date).toBe(true);
2197+
// Verify date values remain in their original format (not pre-parsed)
2198+
// IgxPivotDateDimension will handle parsing on-demand
2199+
expect(pivotGrid.data[0].StartDate).toBe(TEST_START_TIMESTAMP);
2200+
expect(pivotGrid.data[0].EndDate).toBe(TEST_END_DATE_STRING);
22022201
});
22032202

22042203
it('should not treat regular numbers as timestamps', () => {

0 commit comments

Comments
 (0)