cmark

My personal build of CMark ✏️

Commit
4d49f2befc95241abf8f5d7ef07ffd1ef4c23bfb
Parent
bdf6f4fe614e1065b74622e7f359d998be5e491e
Author
John MacFarlane <jgm@berkeley.edu>
Date

Rewrote HTML escaping for big performance gain.

Diffstat

2 files changed, 20 insertions, 12 deletions

Status File Name N° Changes Insertions Deletions
Modified benchmarks.md 8 4 4
Modified js/lib/html.js 24 16 8
diff --git a/benchmarks.md b/benchmarks.md
@@ -38,7 +38,7 @@ They can be run using `make benchjs`:
 
 |Implementation     |  ops/sec    |
 |-------------------|-------------|
-| showdown.js       | 173 ±1.43%  |
-| **commonmark.js** | 371 ±1.20%  |
-| marked.js         | 554 ±0.48%  |
-| markdown-it       | 694 ±0.97%  |
+| showdown.js       | 169 ±1.72%  |
+| **commonmark.js** | 420 ±0.88%  |
+| marked.js         | 548 ±0.66%  |
+| markdown-it       | 690 ±1.00%  |
diff --git a/js/lib/html.js b/js/lib/html.js
@@ -213,6 +213,20 @@ var renderNodes = function(block) {
     return buffer.join('');
 };
 
+var sub = function(s) {
+    if (s === '&') {
+        return '&amp;';
+    } else if (s === '<') {
+        return '&lt;';
+    } else if (s === '>') {
+        return '&gt;';
+    } else if (s === '"') {
+        return '&quot;';
+    } else {
+        return s;
+    }
+};
+
 
 // The HtmlRenderer object.
 function HtmlRenderer(){
@@ -225,15 +239,9 @@ function HtmlRenderer(){
         // set to " " if you want to ignore line wrapping in source
         escape: function(s, preserve_entities) {
             if (preserve_entities) {
-                return s.replace(/[&](?![#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)/gi, '&amp;')
-                    .replace(/[<]/g, '&lt;')
-                    .replace(/[>]/g, '&gt;')
-                    .replace(/["]/g, '&quot;');
+                return s.replace(/[&](?:[#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)|[&<>"]/gi, sub);
             } else {
-                return s.replace(/[&]/g, '&amp;')
-                    .replace(/[<]/g, '&lt;')
-                    .replace(/[>]/g, '&gt;')
-                    .replace(/["]/g, '&quot;');
+                return s.replace(/[&<>"]/g, sub);
             }
         },
         render: renderNodes