Prepare ship for ludicrous speed.

Make sure to eat extra newlines at the end of certain block rules.
This avoids an extra iteration for `space` tokens.
Marked is now roughly 97% or 98% the speed of Discount.
This commit is contained in:
Christopher Jeffrey 2012-01-12 02:46:06 -06:00
parent ac880f0b79
commit 5f7edf47ed
2 changed files with 15 additions and 17 deletions

View File

@ -19,15 +19,15 @@ node v0.6.x
``` bash
$ node test --bench
marked completed in 7904ms.
marked (with gfm) completed in 8947ms.
discount completed in 7171ms.
showdown (reuse converter) completed in 15729ms.
showdown (new converter) completed in 18149ms.
markdown-js completed in 24521ms.
marked completed in 7303ms.
marked (with gfm) completed in 8206ms.
discount completed in 7170ms.
showdown (reuse converter) completed in 15865ms.
showdown (new converter) completed in 18140ms.
markdown-js completed in 24357ms.
```
__Marked is now only ~700ms behind Discount (which is written in C).__
__Marked is now only ~130ms behind Discount, which is written in C.__
For those feeling skeptical: These benchmarks run the entire markdown test suite
1000 times. The test suite tests every feature. It doesn't cater to specific

View File

@ -11,13 +11,13 @@
var block = {
newline: /^\n+/,
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?=\n| *$)/,
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*```(?= *\n| *$)/,
hr: /^( *[\-*_]){3,} *\n/,
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?:\n+| *$)/,
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*```(?: *\n+| *$)/,
hr: /^( *[\-*_]){3,} *\n+/,
heading: /^ *(#{1,6}) *([^\0]+?) *#* *\n+/,
lheading: /^([^\n]+)\n *(=|-){3,}/,
blockquote: /^( *>[^\n]+(\n[^\n]+)*)+/,
list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1bullet)/,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
blockquote: /^( *>[^\n]+(\n[^\n]+)*)+\n*/,
list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1bullet)\n*/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
paragraph: /^([^\n]+\n?(?!body))+\n*/,
text: /^[^\n]+/
@ -27,7 +27,7 @@ block.list = (function() {
var list = block.list.source;
list = list
.replace('bullet', /(?:[*+-](?!( *[-*]){2,})|\d+\.)/.source)
.replace('bullet', /(?:[*+-](?!(?: *[-*]){2,})|\d+\.)/.source);
return new RegExp(list);
})();
@ -121,9 +121,7 @@ block.token = function(str, tokens, top) {
cap = cap[0].replace(/^ {4}/gm, '');
tokens.push({
type: 'code',
text: cap[cap.length-1] === '\n'
? cap.slice(0, -1)
: cap
text: cap.replace(/\n+$/, '')
});
continue;
}