Skip to content

[Fix] Replace array sort with reduce to find longest match, avoiding O(n log n) sort#10486

Open
NethmikaKekuu wants to merge 1 commit into
wso2:masterfrom
NethmikaKekuu:fix/js-min-max-loop
Open

[Fix] Replace array sort with reduce to find longest match, avoiding O(n log n) sort#10486
NethmikaKekuu wants to merge 1 commit into
wso2:masterfrom
NethmikaKekuu:fix/js-min-max-loop

Conversation

@NethmikaKekuu

Copy link
Copy Markdown

Purpose

Fixes react-doctor/js-min-max-loop performance warning by replacing an
unnecessary array sort with reduce to find the longest consecutive character
match.

Previously, the code sorted the entire array of regex matches by length just
to grab the first (longest) element — an O(n log n) operation. This is now
replaced with reduce, which finds the longest match in a single O(n) pass.

Affected file:

  • admin.core.v1/utils/user-store-utils.ts (line 153)

Related Issues

Related PRs

  • N/A

Checklist

  • e2e cypress tests locally verified. (for internal contributers)
  • Manual test round performed and verified.
  • UX/UI review done on the final implementation.
  • Documentation provided. (Add links if there are any)
  • Relevant backend changes deployed and verified
  • Unit tests provided. (Add links if there are any)
  • Integration tests provided. (Add links if there are any)

Security checks

Developer Checklist (Mandatory)

  • Complete the Developer Checklist in the related product-is issue to track any behavioral change or migration impact.

No behavioural change, migration impact, or new configuration introduced by this fix.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 2e80eca0-3c54-40a3-aafa-8866adaf3446

📥 Commits

Reviewing files that changed from the base of the PR and between 78163d0 and 3401b38.

📒 Files selected for processing (2)
  • .changeset/fix-js-min-max-loop.md
  • features/admin.core.v1/utils/user-store-utils.ts

📝 Walkthrough

Walkthrough

In validatePasswordAgainstRules(), the logic for finding the longest consecutive character match is changed from sorting the match array and taking the first element (O(n log n)) to using Array.reduce() (O(n)). A changeset records a patch bump for @wso2is/admin.core.v1.

Changes

Password Validation Fix

Layer / File(s) Summary
Replace sort with reduce for longest consecutive match
features/admin.core.v1/utils/user-store-utils.ts, .changeset/fix-js-min-max-loop.md
validatePasswordAgainstRules() now uses reduce() to select the longest consecutive character substring instead of sorting the match array and indexing the first element. Changeset records the patch bump.
🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main performance fix in the affected file and matches the code changes.
Description check ✅ Passed The description follows the template sections and includes purpose, related issue, checklist items, and security notes.
Linked Issues check ✅ Passed The change replaces sorting with a linear-time reduce approach in the reported file, satisfying issue #27892's performance goal.
Out of Scope Changes check ✅ Passed The PR only changes the targeted utility and its changeset, with no unrelated code or scope creep evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Changeset Required ✅ Passed The PR diff includes a new non-README changeset file: .changeset/fix-js-min-max-loop.md.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • 🛠️ create changeset

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[React Doctor] js-min-max-loop: array (1 occurrence)

1 participant