Uh oh!
There was an error while loading. Please reload this page.
Refactor the escape() function to improve performance 10-20% - #975
Conversation
Feder1co5oave
commented
Dec 27, 2017
I know this sounds kinda silly, but can we stick to the present coding style? This almost looks like a different language. |
Okey, I changed the style code, it provided me with VS Code through auto formatting. Also, I replaced |
| "'": ''' | ||
| }; | ||
| var escapeTestNoEncode = /(?:[<>"']|&(?!#?\w+;))/; |
There was a problem hiding this comment.
There's no need to use grouping to wrap the whole thing
There was a problem hiding this comment.
Thank you, I fixed it.
| * Helpers | ||
| */ | ||
| var escapeTest = /[&<>"']/; |
There was a problem hiding this comment.
These should be declared inside escape() IMO
There was a problem hiding this comment.
No, marked do not have to recreate the same RegExp instance every call escape(). This reduces performance and increases memory usage.
There was a problem hiding this comment.
Ok, I made them static.
Feder1co5oave
commented
Jan 5, 2018
Actually, I've found there's no advantage in testing before replacing, you still have to scan the whole thing at least once, either by testing or replacing, so the first phase is pretty useless. # with current changes:
$ node test --bench
marked completed in 8388ms.
marked (gfm) completed in 9380ms.
marked (pedantic) completed in 8315ms.
Could not bench robotskirt.
Could not bench showdown.
Could not bench markdown.js.
# without testing first:
$ node test --bench
marked completed in 8394ms.
marked (gfm) completed in 9286ms.
marked (pedantic) completed in 8045ms.
Could not bench robotskirt.
Could not bench showdown.
Could not bench markdown.js.And you can spare some line of code: functionescape(html,encode){if(encode){returnhtml.replace(escape.replace,function(ch){returnescape.replacements[ch];});}else{returnhtml.replace(escape.replaceNoEncode,function(ch){returnescape.replacements[ch];});}}escape.replace=/[&<>"']/g;escape.replaceNoEncode=/[<>"']|&(?!#?\w+;)/g;escape.replacements={'&': '&','<': '<','>': '>','"': '"',"'": '''}; |
KostyaTretyak
commented
Jan 5, 2018
My first functionescape(html,encode){if(encode){returnhtml.replace(escape.escapeReplace,function(ch){returnescape.replacements[ch]});}else{returnhtml.replace(escape.escapeReplaceNoEncode,function(ch){returnescape.replacements[ch]});}returnhtml;}I run this code: node test -tThree times: My second functionescape(html,encode){if(encode){if(escape.escapeTest.test(html)){returnhtml.replace(escape.escapeReplace,function(ch){returnescape.replacements[ch]});}}else{if(escape.escapeTestNoEncode.test(html)){returnhtml.replace(escape.escapeReplaceNoEncode,function(ch){returnescape.replacements[ch]});}}returnhtml;}Run three times: |
UziTech
commented
Jan 7, 2018
Looks like this would make |
In my benchmarks, remarkable is faster and more economical than |
joshbruce
commented
Jan 7, 2018
Yeah. @worker8's independent benchmark sample has remarkable at the top as well. @KostyaTretyak: Just to make sure. They can compete with marked with large files >2mb - versus the can not? I think if we do what in #746 - we will be able to see areas for optimization easier. Right now we kinda have the large class thing happening. |
KostyaTretyak
commented
Jan 7, 2018
In |
joshbruce
commented
Jan 7, 2018
Interesting. Of course, if they're (or we're) targeting web developers - most folks aren't going to need to go above that. Maybe marked is the "large file" parser. :) Thinking of something like LeanPub - parse an entire book in markdown. |
KostyaTretyak
commented
Jan 7, 2018
No, it is a favorite when files are smaller than 2MB. If the files are bigger, then Not for the sake of advertising, just for you to see it clearly. Do the following: git clone https://github.com/KostyaTretyak/marked-ts.git
cd marked-ts
npm install
npm run compileAnd then you can: npm run bench -- -l 1000Where |
joshbruce
commented
Jan 7, 2018
Thanks! That's an interesting trick...might interesting for us to add to the CLI...if I'm understanding correctly: I can secify how large of a file. Kind of like lipsum https://lipsum.lipsum.com - generate Markdown of X size to run the bench against. |
styfle
commented
Sep 11, 2018
Is there a way to force push to invoke travis unit tests? |
UziTech
commented
Sep 11, 2018
@KostyaTretyak if you can rebase this PR we should be able to merge it. git fetch upstream && git rebase upstream/master && git push -f |
UziTech
commented
Sep 11, 2018
I rebased and tested locally, and everything worked fine. |
styfle
commented
Sep 11, 2018
Nice! I ran benchmarks locally and this is the before and after: BeforeAfter |
KostyaTretyak
commented
Sep 11, 2018
@UziTech, done: git fetch upstream && git rebase upstream/master && git push -f |
styfle
commented
Sep 11, 2018
@KostyaTretyak Thanks! Can you fix this lint error 😄 |
No description provided.