From 83b05755db5abec627bfc98c6fbc0ac0253fd84c Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Mon, 23 Nov 2020 17:44:18 +0300 Subject: [PATCH] Fix quadratic time on emphasis close https://github.com/markdown-it/markdown-it/issues/735 --- CHANGELOG.md | 1 + lib/rules_inline/balance_pairs.js | 4 +--- test/pathological.js | 6 ++++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index decb77c..b45a881 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `[](url (xxx())` is no longer a valid link. - Fix performance issues when parsing links, #732, #734. - Fix performance issues when parsing backticks, #733. +- Fix performance issues when parsing emphases, #735. ## [12.0.2] - 2020-10-23 diff --git a/lib/rules_inline/balance_pairs.js b/lib/rules_inline/balance_pairs.js index 8d649bf..4278c8e 100644 --- a/lib/rules_inline/balance_pairs.js +++ b/lib/rules_inline/balance_pairs.js @@ -27,17 +27,15 @@ function processDelimiters(state, delimiters) { } minOpenerIdx = openersBottom[closer.marker][closer.length % 3]; - newMinOpenerIdx = -1; openerIdx = closerIdx - closer.jump - 1; + newMinOpenerIdx = openerIdx; for (; openerIdx > minOpenerIdx; openerIdx -= opener.jump + 1) { opener = delimiters[openerIdx]; if (opener.marker !== closer.marker) continue; - if (newMinOpenerIdx === -1) newMinOpenerIdx = openerIdx; - if (opener.open && opener.end < 0) { isOddMatch = false; diff --git a/test/pathological.js b/test/pathological.js index fb7e44a..a905dc5 100644 --- a/test/pathological.js +++ b/test/pathological.js @@ -117,4 +117,10 @@ describe('Pathological sequences speed', () => { await test_pattern('[a](b'.repeat(30000)); }); }); + + describe('Markdown-it', () => { + it('emphasis **_* pattern', async () => { + await test_pattern('**_* '.repeat(50000)); + }); + }); });