Skip to content

perf: use iterative loops in waitForCondition, asyncmap, and asyncfilter - #91

Merged
mykola-mokhnach merged 1 commit into
appium:masterfrom
hoanle0126:perf/iterative-wait-and-serial-loops
Nov 11, 2026
Merged

mykola-mokhnach merged 1 commit into
appium:masterfrom
hoanle0126:perf/iterative-wait-and-serial-loops

Conversation

@hoanle0126

@hoanle0126 hoanle0126 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
  • Replaces the recursive return await spin() call in waitForCondition with a for (;;) loop so long-running polling loops do not chain pending promises across polls.
  • Replaces coll.reduce in asyncmap and asyncfilter when options === false with indexed loops over coll.slice(), avoiding quadratic array spread allocations in asyncmap and upfront promise chain creation while preserving sparse-array hole skipping and upfront element snapshots.

@mykola-mokhnach mykola-mokhnach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No general findings; see inline comments.

Comment thread lib/asyncmap.ts Resolved
Comment thread lib/asyncfilter.ts Resolved
Comment thread lib/wait-for-condition.ts Resolved
@hoanle0126

Copy link
Copy Markdown
Contributor Author

Thanks @mykola-mokhnach! Addressed all three inline comments:

  1. Sparse-array hole skipping in asyncmap and asyncfilter: Switched both serial loops to indexed for (let i = 0; i < coll.length; i++) with i in coll checks so holes in sparse arrays (e.g. [1, , 3]) are skipped identically to Array.prototype.reduce, while still avoiding the O(n^2) [...(await acc)] array copy and per-item promise chain.
  2. waitForCondition loop condition & PR description: Replaced while (true) with let result = await condFn(); while (!result) { ... } (avoiding no-constant-condition), and updated the PR description to accurately note that it avoids chaining pending spin() promises rather than synchronous call-stack growth across await.
  3. Tests: Added unit tests for sparse arrays in test/asyncmap.spec.ts and test/asyncfilter.spec.ts, and immediate-return / multi-poll loop tests in test/wait-for-condition.spec.ts.

@mykola-mokhnach mykola-mokhnach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No general findings; see inline comments.

Comment thread lib/asyncfilter.ts Resolved
Comment thread lib/asyncmap.ts Resolved

@mykola-mokhnach mykola-mokhnach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • [major] lib/wait-for-condition.ts:41-45: the branch predates #92 (error hierarchy), which is now on master. Master throws ConditionUnmetError at both sites, imports it, and documents it in the JSDoc. This PR's removed and added lines use plain Error. Merging as is gives a conflict, or a resolution that reverts to Error. In that case instanceof ConditionUnmetError and instanceof TimeoutError and the error-code checks stop matching. Please rebase onto master and keep ConditionUnmetError in both throws.

Comment thread lib/asyncmap.ts Resolved
Comment thread lib/asyncfilter.ts Resolved
Comment thread lib/wait-for-condition.ts Resolved
@hoanle0126
hoanle0126 force-pushed the perf/iterative-wait-and-serial-loops branch from ddbf19c to 92d3e33 Compare November 11, 2026 08:07
@hoanle0126

Copy link
Copy Markdown
Contributor Author

Rebased onto master in 92d3e33, preserving ConditionUnmetError in both throws and keeping the error hierarchy tests from #92.

@mykola-mokhnach
mykola-mokhnach merged commit 975f44e into appium:master Nov 11, 2026
7 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 11, 2026
## [6.6.1](v6.6.0...v6.6.1) (2026-11-11)

### Performance Improvements

* use iterative loops in waitForCondition, asyncmap, and asyncfilter ([#91](#91)) ([975f44e](975f44e))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 6.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants