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

Performance improvement for large texts with many HTML spans #994

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/subParsers/makehtml/hashHTMLSpans.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ showdown.subParser('makehtml.unhashHTMLSpans', function (text, options, globals)
'use strict';
text = globals.converter._dispatch('makehtml.unhashHTMLSpans.before', text, options, globals).getText();

var replacedSpans = [];

for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
var repText = globals.gHtmlSpans[i],
// limiter to prevent infinite loop (assume 10 as limit for recurse)
Expand All @@ -50,9 +52,17 @@ showdown.subParser('makehtml.unhashHTMLSpans', function (text, options, globals)
}
++limit;
}
text = text.replace('¨C' + i + 'C', repText);

replacedSpans.push(repText);
}

// It is important to only do one replace for all the spans combined.
// Otherwise this gets really slow on large texts with many spans because
// of all the string copies.
text = text.replace(/¨C(\d+)C/g, function (_wm, num) {
return replacedSpans[num];
});

text = globals.converter._dispatch('makehtml.unhashHTMLSpans.after', text, options, globals).getText();
return text;
});