Description
Thank you so much for helping improve the quality of Dash!
We do our best to catch bugs during the release process, but we rely on your help to find the ones that slip through.
Describe your context
- dash 4.5.1 (also on
devas of 2026-11-25), React 19, Chromium/Edge. - A page whose callback replaces a list of components that carry dict (pattern-matching) ids with a different list
(for example, a grid of cells re-rendered for another team).
Describe the bug
Replacing N components that have pattern-matching ids with N different ones takes time proportional to N², while
the first render of the same N is linear. The time is spent in computePaths (dash-renderer/src/actions/paths.js):
const index = indexOf(item, oldie);
if (index === -1) {
paths.push(item);
} else {
objs[keyStr] = insert(index, item, paths);
}For every new component with a dict id, R.indexOf scans oldie (every previous id with the same key set) with
R.equals on {values, path}. When nothing matches (new ids), each new id is compared with all old ones. When matches
are found, insert copies the array each time. #3954 made getPath O(1) with objIndex, but this scan in the crawl
remains.
Minimal reproduction
from dash import Dash, html, Input, Output, ALL
app = Dash(__name__)
app.layout = html.Div([html.Button("render", id="go"), html.Div(id="out"), html.Div(id="dummy")])
N = 2000
@app.callback(Output("out", "children"), Input("go", "n_clicks"), prevent_initial_call=True)
def render(n):
return [html.Div(f"item {n}-{i}", id={"type": "cell", "batch": n, "i": i}) for i in range(N)]
@app.callback(Output("dummy", "children"),
Input({"type": "cell", "batch": ALL, "i": ALL}, "n_clicks"), prevent_initial_call=True)
def clicked(_):
return ""
if __name__ == "__main__":
app.run()Click "render" once (first render), then again (replace). Measured with Playwright, headless Chromium, until the new
first item is shown and the main thread is idle:
| N | First render | Replace | Replace again |
|---|---|---|---|
| 500 | 0.27 s | 1.46 s | 1.35 s |
| 1,000 | 0.45 s | 5.44 s | 4.97 s |
| 2,000 | 1.06 s | 19.3 s | 18.7 s |
In a real app (about 2,800 pattern-matching ids on the page, ~2,300 of one shape replaced by a team switch), a CPU
profile of the switch shows 43 of 54 s in computePaths → crawlLayout, with 37 s in indexOf → _equals.
Replacing that one indexOf with a hash lookup (below) took the same switch from 69 s to 25 s, and the longest main
thread block from 48 s to 8.9 s.
Expected behavior
Replacing N pattern-matching components costs about the same as rendering N for the first time.
Suggested fix
Look the item up in a map instead of scanning: build, once per keyStr, a Map from a key of {values, path} (for
example JSON.stringify([values, path]), matching valuesKey) to its index in oldie, and use it in place of
indexOf(item, oldie). Also consider replacing insert(index, item, paths) with a push, so a match doesn't copy the
array each time (and doesn't insert at an index that belongs to the old list).
This is the second quadratic path we've hit on the same page after #4121/#4124 (fixed in 4.5.1, thank you).
Activity
biyani701 commentedon Nov 25, 2026
Tested a fix locally on 4.5.1. Fixing the
indexOfincomputePathsalone is not enough: the same replace then hitsa second O(removed × current) loop, in
getUnfilteredLayoutCallbacks→addCallbackIfArray(actions/dependencies_tsin the bundle). For every removed component that was an input of an
ALLcallback, it callscb.getInputs(paths),which resolves the callback's whole
ALLinput list, and stringifies every id in it to compare with the removed id.With both changed, replacing N components costs the same as rendering them the first time (repro from the issue,
headless Chromium):
computePathsfixed@@APP_NUMBERS@@
The two changes, as tested (written against the built bundle; the source equivalents are small):
computePaths: look the old entry up instead of scanning, e.g. aMapfromJSON.stringify(entry)to its index inoldie, built once per key set:addCallbackIfArray: compute the set of stringified ids in a callback's array inputs once per callback (perpaths), then test membership:arrayInputIdsscoped to onegetUnfilteredLayoutCallbackscall.)Happy to open a PR with these and the benchmark if that helps.