Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/perf-csv-record-assembler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"web-csv-toolbox": patch
---

Optimize CSVRecordAssembler with single-loop array processing

**Performance Optimization:**
- Replaced chained array methods (`.map().filter().map()`) with single for-loop
- Reduces array iterations from 3 passes to 1 pass
- Applied to 3 critical code paths:
- Processing non-empty records (RecordDelimiter handler)
- Processing empty lines (skipEmptyLines=false case)
- Flushing remaining buffered data

**Implementation Details:**
- Changed from functional chaining to imperative loop for better performance
- Direct array construction eliminates intermediate array allocations
- Maintains identical output behavior - all 460 tests pass

**Impact:**
- Most beneficial for CSVs with many columns
- Reduces CPU cycles during record assembly phase
- Complements the CSVLexer buffer pointer optimization
44 changes: 27 additions & 17 deletions src/CSVRecordAssembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,26 @@ export class CSVRecordAssembler<Header extends ReadonlyArray<string>> {
this.#setHeader(this.#row as unknown as Header);
} else {
if (this.#dirty) {
yield Object.fromEntries(
this.#header
.map((header, index) => [header, index] as const)
.filter(([header]) => header)
.map(([header, index]) => [header, this.#row.at(index)]),
) as unknown as CSVRecord<Header>;
// Optimize: single loop instead of map().filter().map()
const entries: [string, string | undefined][] = [];
for (let i = 0; i < this.#header.length; i++) {
const header = this.#header[i];
if (header) {
entries.push([header, this.#row[i]]);
}
}
yield Object.fromEntries(entries) as unknown as CSVRecord<Header>;
Comment on lines +113 to +121
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The optimization from chained map().filter().map() to a single for loop is a significant improvement for performance, especially when dealing with large datasets. This change reduces array iterations and avoids intermediate array allocations, which aligns with the PR's goal of optimizing CPU cycles.

} else {
if (!this.#skipEmptyLines) {
yield Object.fromEntries(
this.#header
.filter((header) => header)
.map((header) => [header, ""]),
) as CSVRecord<Header>;
// Optimize: single loop instead of filter().map()
const entries: [string, string][] = [];
for (let i = 0; i < this.#header.length; i++) {
const header = this.#header[i];
if (header) {
entries.push([header, ""]);
}
}
yield Object.fromEntries(entries) as CSVRecord<Header>;
Comment on lines +124 to +132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous change, replacing filter().map() with a single for loop here also contributes to better performance by reducing array passes and memory allocations. This consistency in optimization is good.

}
}
}
Expand All @@ -144,12 +151,15 @@ export class CSVRecordAssembler<Header extends ReadonlyArray<string>> {
*#flush(): IterableIterator<CSVRecord<Header>> {
if (this.#header !== undefined) {
if (this.#dirty) {
yield Object.fromEntries(
this.#header
.map((header, index) => [header, index] as const)
.filter(([header]) => header)
.map(([header, index]) => [header, this.#row.at(index)]),
) as unknown as CSVRecord<Header>;
// Optimize: single loop instead of map().filter().map()
const entries: [string, string | undefined][] = [];
for (let i = 0; i < this.#header.length; i++) {
const header = this.#header[i];
if (header) {
entries.push([header, this.#row[i]]);
}
}
yield Object.fromEntries(entries) as unknown as CSVRecord<Header>;
Comment on lines +154 to +162
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Applying the same single-loop optimization to the #flush method ensures that this critical path also benefits from the performance improvements. This thoroughness in applying the optimization across all relevant code paths is commendable.

}
}
}
Expand Down
Loading