summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-10 21:33:25 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-10 21:34:04 -0800
commit958505853ebd24fd3c0dc2bc6f6fb0615a48c8b7 (patch)
treeeb949684782940565b5e1c4dbadbbaa6cdddbff8
parentf5476eb652b7d69ed25c858a4d757b9313b40b67 (diff)
Added options to DocParser.
So far only 'time' is supported. 'time' now gives a breakdown of block parsing, inline parsing, and rendering. Currently the ratio is block parsing: 725ms inline parsing: 332ms rendering: 213ms so perhaps block.js is the best place to look for further optimizations.
-rwxr-xr-xjs/bin/commonmark13
-rw-r--r--js/lib/blocks.js9
-rw-r--r--js/lib/html.js3
3 files changed, 14 insertions, 11 deletions
diff --git a/js/bin/commonmark b/js/bin/commonmark
index 6c9955c..8f1210b 100755
--- a/js/bin/commonmark
+++ b/js/bin/commonmark
@@ -7,9 +7,8 @@ var commonmark = require('../lib/index.js');
var inps = [];
var file;
var files = [];
-var options = { sourcepos: false };
+var options = { sourcepos: false, time: false };
var format = 'html';
-var time = false;
var i;
for (i = 2; i < process.argv.length; i++) {
@@ -17,7 +16,7 @@ for (i = 2; i < process.argv.length; i++) {
if (arg === '--ast') {
format = 'ast';
} else if (arg === '--time') {
- time = true;
+ options.time = true;
} else if (arg === '--sourcepos') {
options.sourcepos = true;
} else if (/^--/.test(arg)) {
@@ -28,7 +27,7 @@ for (i = 2; i < process.argv.length; i++) {
}
}
-var parser = new commonmark.DocParser();
+var parser = new commonmark.DocParser(options);
var renderer;
if (format === 'html') {
@@ -48,12 +47,8 @@ for (i = 0; i < files.length; i++) {
}
var inp = inps.join('\n');
-if (time) { console.time("parsing"); }
var doc = parser.parse(inp);
-if (time) { console.timeEnd("parsing"); }
-if (time) { console.time("rendering"); }
var rendered = renderer.render(doc);
-if (time) { console.timeEnd("rendering"); }
-if (!time) { process.stdout.write(rendered); } \ No newline at end of file
+if (!options.time) { process.stdout.write(rendered); } \ No newline at end of file
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
index 05de246..c2d14d7 100644
--- a/js/lib/blocks.js
+++ b/js/lib/blocks.js
@@ -671,6 +671,7 @@ var Document = function() {
// The main parsing function. Returns a parsed document AST.
var parse = function(input) {
"use strict";
+ if (this.options.time) { console.time("block parsing"); }
this.doc = Document();
this.tip = this.doc;
this.refmap = {};
@@ -686,13 +687,16 @@ var parse = function(input) {
while (this.tip) {
this.finalize(this.tip, len);
}
+ if (this.options.time) { console.timeEnd("block parsing"); }
+ if (this.options.time) { console.time("inline parsing"); }
this.processInlines(this.doc);
+ if (this.options.time) { console.timeEnd("inline parsing"); }
return this.doc;
};
// The DocParser object.
-function DocParser(){
+function DocParser(options){
"use strict";
return {
doc: Document(),
@@ -706,7 +710,8 @@ function DocParser(){
incorporateLine: incorporateLine,
finalize: finalize,
processInlines: processInlines,
- parse: parse
+ parse: parse,
+ options: options || {}
};
}
diff --git a/js/lib/html.js b/js/lib/html.js
index a985039..847ed98 100644
--- a/js/lib/html.js
+++ b/js/lib/html.js
@@ -50,6 +50,8 @@ var renderNodes = function(block) {
var options = this.options;
+ if (options.time) { console.time("rendering"); }
+
while ((event = walker.next())) {
entering = event.entering;
node = event.node;
@@ -225,6 +227,7 @@ var renderNodes = function(block) {
}
}
+ if (options.time) { console.timeEnd("rendering"); }
return buffer;
};