Title: [BUG]: Bar traces draw in O(n²) time since v4.0.0 (getZeroBarDir runs once per bar) · Issue #8157 · plotly/plotly.js · GitHub
Open Graph Title: [BUG]: Bar traces draw in O(n²) time since v4.0.0 (getZeroBarDir runs once per bar) · Issue #8157 · plotly/plotly.js
X Title: [BUG]: Bar traces draw in O(n²) time since v4.0.0 (getZeroBarDir runs once per bar) · Issue #8157 · plotly/plotly.js
Description: Description Since v4.0.0, drawing a bar trace takes O(n²) time whenever all bar values have the same sign, which is the common case for counts, totals, and revenue. 60k bars take ~3.7 s in v4.x versus ~0.3 s in v3.7.0, and 100k bars free...
Open Graph Description: Description Since v4.0.0, drawing a bar trace takes O(n²) time whenever all bar values have the same sign, which is the common case for counts, totals, and revenue. 60k bars take ~3.7 s in v4.x ver...
X Description: Description Since v4.0.0, drawing a bar trace takes O(n²) time whenever all bar values have the same sign, which is the common case for counts, totals, and revenue. 60k bars take ~3.7 s in v4.x ver...
Opengraph URL: https://github.com/plotly/plotly.js/issues/8157
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: Bar traces draw in O(n²) time since v4.0.0 (getZeroBarDir runs once per bar)","articleBody":"### Description\n\nSince v4.0.0, drawing a `bar` trace takes O(n²) time whenever all bar values have the same sign, which is the common case for counts, totals, and revenue. 60k bars take ~3.7 s in v4.x versus ~0.3 s in v3.7.0, and 100k bars freeze the page for close to a minute (measured in an app that embeds plotly.js 4.1.2).\n\nThe cause is in `src/traces/bar/plot.js`. Since #7872, `appendBarText()` calls `getZeroBarDir(cd, isHorizontal, xa, ya)` for **every bar**. `getZeroBarDir` loops over all of `cd` and only returns early once it has seen both a positive and a negative value:\n\n```js\nfunction appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, opts, makeOnCompleteCallback) {\n ...\n var zeroBarDir = getZeroBarDir(cd, isHorizontal, xa, ya); // O(n), runs once per bar\n\n var text = getText(fullLayout, cd, i, xa, ya);\n textPosition = getTextPosition(trace, i);\n ...\n if (!text || textPosition === 'none' || ...) {\n bar.select('text').remove();\n return;\n }\n```\n\n```js\nfunction getZeroBarDir(cd, isHorizontal, xa, ya) {\n var hasPositive = false;\n var hasNegative = false;\n\n for (var i = 0; i \u003c cd.length; i++) {\n var s = cd[i].s;\n if (s > 0) {\n hasPositive = true;\n } else if (s \u003c 0) {\n hasNegative = true;\n }\n if (hasPositive && hasNegative) {\n return 0;\n }\n }\n ...\n}\n```\n\nThe call runs before the `!text || textPosition === 'none'` early return. So traces **without any text**, and traces with `textposition: 'none'`, pay the quadratic cost too. Mixed-sign data exits the loop almost immediately, which is why it stays fast.\n\nA Chrome CPU profile of a 30k-bar `newPlot` attributes 2.6 s of the 3.4 s long task to this function.\n\n### Screenshots/Video\n\nN/A (performance). `Plotly.newPlot` timings for a single bar trace in headless Chromium 156 on an Apple M4 (macOS). Each cell is one run in a fresh page:\n\n| plotly.js | bars | all positive (ms) | mixed sign (ms) | all positive, `textposition: 'none'` (ms) |\n|---|---:|---:|---:|---:|\n| 3.7.0 | 10,000 | 93 | 86 | 76 |\n| 3.7.0 | 30,000 | 175 | 179 | 172 |\n| 3.7.0 | 60,000 | 299 | 304 | 301 |\n| 4.0.0 | 10,000 | 184 | 83 | 184 |\n| 4.0.0 | 30,000 | 1,052 | 191 | 1,134 |\n| 4.0.0 | 60,000 | 3,716 | 350 | 3,698 |\n| 4.2.0 | 10,000 | 189 | 105 | 186 |\n| 4.2.0 | 30,000 | 1,043 | 195 | 1,048 |\n| 4.2.0 | 60,000 | 3,736 | 386 | 3,701 |\n\nDoubling the bar count roughly quadruples the time on 4.x, while 3.7.0 and mixed-sign data scale linearly.\n\n### Steps to reproduce\n\nOpen this page in Chrome and look at the console. Switching the version in the script URL to `3.7.0`, or making some `y` values negative, makes it fast again.\n\n```html\n\u003c!doctype html>\n\u003chtml>\n\u003chead>\n \u003cmeta charset=\"utf-8\">\n \u003cscript src=\"https://cdn.jsdelivr.net/npm/plotly.js-dist-min@4.2.0/plotly.min.js\">\u003c/script>\n\u003c/head>\n\u003cbody>\n \u003cdiv id=\"plot\" style=\"width:900px;height:450px\">\u003c/div>\n \u003cscript>\n const n = 60000;\n const x = Array.from({ length: n }, (_, i) => i);\n const y = Array.from({ length: n }, (_, i) => 1 + ((i * 7919) % 100)); // all positive\n const t0 = performance.now();\n Plotly.newPlot('plot', [{ type: 'bar', x, y }]).then(() =>\n console.log(`newPlot: ${Math.round(performance.now() - t0)} ms`)\n );\n \u003c/script>\n\u003c/body>\n\u003c/html>\n```\n\nThe same happens from Python with `px.bar(x=np.arange(60_000), y=np.random.rand(60_000)).show()`, since plotly.py 7.1.0 bundles plotly.js 4.1.1.\n\n### Notes\n\n- **Introduced by** #7872 (\"fix(bar): prevent outside text labels from overlapping tilted axis ticks\"), commits 6b1363e5a / f291d70fb / 79c20f8ed. It ships in v4.0.0 and is still present in v4.2.0 and `main`.\n- **Suggested fix:** compute the zero-bar direction once per trace and pass it into `appendBarText`. For example, compute it in `plotBars` before iterating the bars and pass it through `opts`, or cache it on `cd[0]`. Moving the call below the `!text` / `textposition === 'none'` early return would also help traces without text, but traces with text would remain quadratic.\n- **Workaround:** none from the public API, since `textposition: 'none'` does not avoid the call.\n- Found while benchmarking chart rendering in Streamlit, which bundles plotly.js 4.1.2 for `st.plotly_chart`.\n","author":{"url":"https://github.com/lukasmasuch","@type":"Person","name":"lukasmasuch"},"datePublished":"2026-10-30T19:35:45Z","url":"https://github.com/plotly/plotly.js/issues/8157"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5d07777e-6b80-2347-9d8d-c81872850987 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CDE8:299992:841E57:AE9513:6B01AE36 |
| html-safe-nonce | 83279136fde2b840075f2a19ab185a7aa1db2cad912f994cb5e48b6b4a362512 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREU4OjI5OTk5Mjo4NDFFNTc6QUU5NTEzOjZCMDFBRTM2IiwidmlzaXRvcl9pZCI6IjQyMzQwMjIwNDI1Nzc1MTQwMzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 52d42927c010c62a0d78f255c204f17257199e4f49631920f1ca9bf336ae9184 |
| hovercard-subject-tag | issue:6125779379 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/plotly/plotly.js/8157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec67ce6cf3c9ae6a744d1202b2dc74877c274877fe596a8827d2e6f21ee3b32d/plotly/plotly.js/issues/8157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec67ce6cf3c9ae6a744d1202b2dc74877c274877fe596a8827d2e6f21ee3b32d/plotly/plotly.js/issues/8157 |
| og:image:alt | Description Since v4.0.0, drawing a bar trace takes O(n²) time whenever all bar values have the same sign, which is the common case for counts, totals, and revenue. 60k bars take ~3.7 s in v4.x ver... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lukasmasuch |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/plotly/plotly.js git https://github.com/plotly/plotly.js.git |
| octolytics-dimension-user_id | 5997976 |
| octolytics-dimension-user_login | plotly |
| octolytics-dimension-repository_id | 45646037 |
| octolytics-dimension-repository_nwo | plotly/plotly.js |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 45646037 |
| octolytics-dimension-repository_network_root_nwo | plotly/plotly.js |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width