Skip to content

Text locator count and nth lookups take seconds on large repeated lists #3238

Open

Description

@vishalanton16

Description

Text locator operations become disproportionately slow on pages with thousands of matching elements. Even reading the third match can take several seconds.

Reproduction

await page.goto(
  "data:text/html," + encodeURIComponent("<main id='root'></main>"),
);

await page.evaluate(() => {
  document.querySelector("#root")!.innerHTML = Array.from(
    { length: 10_000 },
    (_, index) => `<div><button>Repeated label ${index}</button></div>`,
  ).join("");
});

const matches = page.locator("text=Repeated label");

for (let run = 0; run < 3; run++) {
  console.time("count");
  console.log(await matches.count());
  console.timeEnd("count");

  console.time("nth");
  console.log(await matches.nth(2).innerText());
  console.timeEnd("nth");
}

Actual behavior

A local benchmark with 10,000 text matches measured median times of:

  • count(): 4,124 ms.
  • nth(2).innerText(): 12,498 ms.

Results were correct, but lookup cost made these operations impractical on large lists. Timings depend on the machine and browser.

Expected behavior

Counting and selecting one match should avoid repeated pairwise comparisons and resolving remote handles for every match.

The example should return 10,000 and "Repeated label 2" without multi-second overhead. Match order, innermost-element semantics, shadow-root behavior, and cancellation should remain correct.

Technical details

Text matching repeatedly scans candidates to eliminate matching ancestors. Indexed resolution also resolves more matches than the requested operation needs.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions