Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve block html perf #570

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-boxes-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"markdown-to-jsx": patch
---

Improved block html detection regex to handle certain edge cases that cause extreme slowness. Thank you @devbrains-com for the basis for this fix 🤝
Binary file not shown.
Binary file not shown.
148 changes: 148 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,154 @@ Item detail
</p>
`)
})

it('#546 perf regression test, self-closing block + block HTML causes exponential degradation', () => {
render(
compiler(
`<span class="oh" data-self-closing="yes" />

You can have anything here. But it's best if the self-closing tag also appears in the document as a pair tag multiple times. We have found it when compiling a table with spans that had a self-closing span at the top.

<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>
<span class="oh">no</span>

Each span you copy above increases the time it takes by 2. Also, writing text here increases the time.`.trim()
)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<div>
<span class="oh"
data-self-closing="yes"
>
</span>
<p>
You can have anything here. But it's best if the self-closing tag also appears in the document as a pair tag multiple times. We have found it when compiling a table with spans that had a self-closing span at the top.
</p>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<span class="oh">
no
</span>
<p>
Each span you copy above increases the time it takes by 2. Also, writing text here increases the time.
</p>
</div>
`)
})
})

describe('horizontal rules', () => {
Expand Down
2 changes: 1 addition & 1 deletion index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ const HEADING_SETEXT_R = /^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/
* \n*
*/
const HTML_BLOCK_ELEMENT_R =
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?([^>]*)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?((?:[^>]*[^/])?)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i

const HTML_CHAR_CODE_R = /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/gi

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"microtime": "^3.1.1",
"mkdirp": "^3.0.1",
"preact": "^10.19.3",
"prettier": "^3.1.1",
"prettier": "^2.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rimraf": "^5.0.5",
Expand Down
13 changes: 2 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7101,7 +7101,7 @@ __metadata:
microtime: "npm:^3.1.1"
mkdirp: "npm:^3.0.1"
preact: "npm:^10.19.3"
prettier: "npm:^3.1.1"
prettier: "npm:^2.0.0"
react: "npm:^17.0.2"
react-dom: "npm:^17.0.2"
rimraf: "npm:^5.0.5"
Expand Down Expand Up @@ -8479,7 +8479,7 @@ __metadata:
languageName: node
linkType: hard

"prettier@npm:^2.7.1":
"prettier@npm:^2.0.0, prettier@npm:^2.7.1":
version: 2.8.8
resolution: "prettier@npm:2.8.8"
bin:
Expand All @@ -8488,15 +8488,6 @@ __metadata:
languageName: node
linkType: hard

"prettier@npm:^3.1.1":
version: 3.1.1
resolution: "prettier@npm:3.1.1"
bin:
prettier: bin/prettier.cjs
checksum: 26a249f321b97d26c04483f1bf2eeb22e082a76f4222a2c922bebdc60111691aad4ec3979610e83942e0b956058ec361d9e9c81c185172264eb6db9aa678082b
languageName: node
linkType: hard

"pretty-bytes@npm:^3.0.0":
version: 3.0.1
resolution: "pretty-bytes@npm:3.0.1"
Expand Down
Loading