Skip to content

perf: avoid quadratic grouping of raw results selected without primary keys - #12802

Merged
alumni merged 1 commit into
typeorm:masterfrom
akdfkeks:perf/group-row-index-fallback
Aug 27, 2026
Merged

alumni merged 1 commit into
typeorm:masterfrom
akdfkeks:perf/group-row-index-fallback

Conversation

@akdfkeks

Copy link
Copy Markdown
Contributor

Description of change

The row-index grouping fallback in RawSqlResultsToEntityTransformer.group() (added in #11669, used when primary key columns are not selected) computes the row index with rawResults.indexOf(rawResult) inside the row loop — a full array scan per row, making the grouping O(n^2). This PR uses the loop index instead, restoring O(n). The primary-key grouping path is unchanged.

Benchmark (transform(), median of 7 runs):

rows before (ms) after (ms)
10,000 15.4 4.8
30,000 112.8 14.2
100,000 1,129.4 55.3

Behavior is identical: indexOf uses reference equality and drivers return a distinct object per row, so it always resolved to the current loop index. Added tests for the fallback on the main alias (partial select without the primary key, no joins); the existing fallback tests from #11669 still pass.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword — N/A (regression introduced by fix: include joined entity primary keys in pagination subquery #11669, no open issue)
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change — N/A (internal performance fix)

Replace Array#indexOf with the loop index when building fallback group ids in
RawSqlResultsToEntityTransformer. Previously each row performed an O(n) indexOf
scan, making entity transformation O(n^2) for result sets selected without
primary key columns (100k rows: 1129ms -> 55ms).

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added the needs-triage PR needs issue link triage label Aug 27, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review
  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Tip of the day
💡 Did you know, you can ask Qodo to dismiss a finding you disagree with, with your reason on record

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@akdfkeks akdfkeks changed the title perf: avoid O(n^2) grouping of raw results selected without primary keys perf: avoid quadratic grouping of raw results selected without primary keys Aug 27, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 27, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12802
    

commit: d72bf3e

@Cprakhar Cprakhar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 💪

@Cprakhar
Cprakhar requested review from alumni and gioboa August 27, 2026 10:16
@alumni
alumni merged commit b495ed4 into typeorm:master Aug 27, 2026
48 of 50 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-triage PR needs issue link triage

Development

Successfully merging this pull request may close these issues.

3 participants