Skip to content

[Regression]: evaluate() with larger results ~40% slower since 1.65 #3362

Done

Description

Last Good Version

1.64.0

First Bad Version

1.65.0

Steps to reproduce

evaluate() with a medium-sized result got about 40% slower:

import time
from playwright.sync_api import sync_playwright

JS = "Array.from({length: 200}, (_, i) => ({id: i, name: 'item ' + i, price: i * 1.5, url: 'https://example.com/' + i, tags: ['a', 'b']}))"

with sync_playwright() as p:
    page = p.chromium.launch().new_page()
    page.evaluate(JS)
    best = 1e9
    for _ in range(3):
        start = time.perf_counter()
        for _ in range(300):
            page.evaluate(JS)
        best = min(best, time.perf_counter() - start)
    print(f"{best:.2f}s")

Expected behavior

1.64.0: 1.30s

Actual behavior

1.65.2: 1.84s

Additional context

cProfile points at the protocol validator: the t_object validator in protocol/validator_primitives.py is about 60% of the profiled time. For every object node it calls the validator of every key in the schema, and SerializedValue has ~15 optional keys, so most calls are t_optional(None) for keys that aren't there. It also calls context.is_under_test() once per node.

Skipping the call when the key is absent and the validator is a t_optional (the result is None in that case anyway) brings the script above to 1.47s in my test, same output. Validated messages also include every event and initializer, so it's not only evaluate.

Environment

- Operating System: macOS 27.2
- CPU: arm64
- Browser: Chromium
- Python Version: 3.14
- Other info:

Activity

  1. microsoft-playwright-automation commented on Nov 23, 2026

    @microsoft-playwright-automation
  2. microsoft-playwright-automation commented on Nov 23, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I reproduced this and prepared a fix.

    Reproduced on 1.65.0, 1.65.2, and current main (de03c865f5738c2291a6a58c8b5c20346d8e70f2) on macOS with Python 3.14. The Python validator calls every absent optional field for each serialized value. The fix skips those calls, while keeping validation of present fields and required fields.

    The regression test fails before the fix and passes after it on Chromium, Firefox, and WebKit. It checks the unnecessary calls directly, without a timing threshold.

    Reproduction and checks

    I evaluated this expression 300 times per sample, taking the best of three samples and checking the returned data:

    Array.from({length: 200}, (_, i) => ({
      id: i, name: 'item ' + i, price: i * 1.5,
      url: 'https://example.com/' + i, tags: ['a', 'b']
    }))

    The runner is macOS 26.7.2 arm64 with Python 3.14.10. The reported macOS 27.2 was not available. All browser checks were headless; published-release benchmarks used Chromium.

    Python package Best time for 300 evaluations
    1.64.0 2.710 s
    1.65.0 3.399 s
    1.65.2 3.155 s
    Current main, with fix 2.276 s

    A separate comparison used the same client and driver on main, changing only the validator: 3.339 s before the fix and 2.586 s after it (23% less time). In a profile of 100 evaluations, optional-validator calls fell from 2,563,300 to 200,400 (92% fewer calls). Timings vary on this shared runner; the call reduction is deterministic.

    The Node.js client benchmark used Node 24.22.3 and the pinned upstream driver (a018f24a4fc76e2120d3b35d2415f1e5bd4b073e), and took 2.162 s. The extra validation cost is in the Python client.

    Sync evaluation and handle tests: 329 passed, 4 upstream skips across all three browsers. Async evaluation, handle, Python-specific evaluation, and storage-state tests: 421 passed, 5 upstream skips across all three browsers. No browser-specific validation failure occurred.

    An additional comparison checked 800 combinations against the original validator: the results and errors matched for missing required fields, present invalid fields, explicit None values, array nulls, and test hooks.

    mypy playwright and scripts/lint.sh passed. Lint used Python 3.13 because the pinned pyright version fails on Python 3.14's standard-library template strings. The benchmark and browser tests used Python 3.14.10.

    Draft PR: #3368

    Triaged on macOS by the Playwright bot - agent run

  3. dgozman commented on Nov 25, 2026

    @dgozman
    Collaborator

    Agent, let's compare performance before and after #3373.

  4. added and removed
    agent-triage-done
    on Nov 25, 2026
  5. microsoft-playwright-automation commented on Nov 25, 2026

    @microsoft-playwright-automation
  6. microsoft-playwright-automation commented on Nov 25, 2026

    @microsoft-playwright-automation
    Contributor

    Hi, I'm the Playwright bot. I compared performance before and after #3373.

    #3373 reduces median time for 300 evaluations from 2.92 s to 2.02 s: 31% less time. The returned data matches before and after. With the driver held fixed, the new Python client also takes less time: 21-29% in these measurements.

    The optional-validator calls identified in this issue fall from 2,563,300 to zero in a profile of 100 evaluations. This change is on main; it is not yet in a tagged release.

    Comparison, reproduction, and checks

    The exact Python commits are:

    I built both pinned drivers. The old driver is 049226728d2da8054db18b04782a9848b755855e; the new driver is b0e0b7b555e637cc8c0ce94dd98c3be153861333. Both use Node 26.13.1 and headless Chromium 159.0.8154.13, revision 1256.

    The runner is macOS 26.7.2 arm64 with Python 3.14.10. The reported macOS 27.2 was not available.

    Each process ran 20 warmup evaluations, then three samples of 300 evaluations. I repeated each condition in three processes, changing the order between rounds. The table shows all nine unprofiled samples per condition as their median and minimum. Result assertions were outside the timed loops.

    Python client Driver Median, 300 evaluations Minimum
    Before #3373 Old 2.918 s 2.800 s
    After #3373 New 2.019 s 1.926 s
    Before #3373 New 2.842 s 2.463 s
    After #3373 Old 2.310 s 2.176 s

    With the new driver fixed, median time falls by 29%; with the old driver fixed, it falls by 21%. This is a shared runner, so absolute timings and percentages vary. The call reduction is deterministic.

    The workload was:

    js = """Array.from({length: 200}, (_, i) => ({
        id: i, name: 'item ' + i, price: i * 1.5,
        url: 'https://example.com/' + i, tags: ['a', 'b']
    }))"""
    # After warmup, with a page from sync_playwright():
    samples = []
    for _ in range(3):
        start = time.perf_counter()
        for _ in range(300):
            result = page.evaluate(js)
        samples.append(time.perf_counter() - start)
        assert result == expected  # All 200 objects and their fields.

    In separate profiles of 100 evaluations, object-validator calls remain at 260,500 and calls to the scalar validators are unchanged. The new schema separates required and optional fields, skips absent options before constructing their paths, and validates present fields directly without the optional wrapper. It still calls is_under_test() per object.

    On the merged commit, sync evaluation and handle tests passed: 341 passed, 4 upstream skips. Async evaluation, handle, Python-specific evaluation, and the rolled validator regression passed: 371 passed, 4 upstream skips. Both runs covered Chromium, Firefox, and WebKit in separate pytest processes. Performance measurements were Chromium-only.

    Commands:

    pytest tests/sync/page/test_page_evaluate.py \
      tests/sync/page/test_page_evaluate_handle.py \
      tests/sync/page/test_jshandle_evaluate.py \
      tests/sync/page/test_jshandle_json_value.py
    pytest tests/async/page/test_page_evaluate.py \
      tests/async/page/test_page_evaluate_handle.py \
      tests/async/page/test_jshandle_evaluate.py \
      tests/async/page/test_jshandle_json_value.py \
      tests/async/python/test_page_evaluate.py \
      tests/async/python/test_validator_primitives.py

    The benchmark script, raw samples, profiles, and test logs are in the agent run artifact. No additional fix is proposed: #3373 already contains the change that removes this validation cost.

    Triaged on macOS by the Playwright bot - agent run

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

    agent-triage-done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions