From 6af3709e03692d4623aa6cd031fbf13ec2f6b2ce Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 3 Apr 2024 21:18:03 +0800 Subject: [PATCH] Refactor to improve performance w/ hoisted regex Closes GH-39. Reviewed-by: Titus Wormer --- lib/handle/comment.js | 14 +++++++++++--- lib/handle/text.js | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/handle/comment.js b/lib/handle/comment.js index 6e16d1f..95ff4aa 100644 --- a/lib/handle/comment.js +++ b/lib/handle/comment.js @@ -7,6 +7,12 @@ import {stringifyEntities} from 'stringify-entities' +const htmlCommentRegex = /^>|^->||--!>|'] +const commentEntitySubset = ['<', '>'] + /** * Serialize a comment. * @@ -27,10 +33,12 @@ export function comment(node, _1, _2, state) { ? '']}) + Object.assign({}, state.settings.characterReferences, { + subset: bogusCommentEntitySubset + }) ) + '>' - : '|--!>|' + : '' /** * @param {string} $0 @@ -39,7 +47,7 @@ export function comment(node, _1, _2, state) { return stringifyEntities( $0, Object.assign({}, state.settings.characterReferences, { - subset: ['<', '>'] + subset: commentEntitySubset }) ) } diff --git a/lib/handle/text.js b/lib/handle/text.js index de25941..af08b28 100644 --- a/lib/handle/text.js +++ b/lib/handle/text.js @@ -9,6 +9,9 @@ import {stringifyEntities} from 'stringify-entities' +// Declare array as variable so it can be cached by `stringifyEntities` +const textEntitySubset = ['<', '&'] + /** * Serialize a text node. * @@ -32,7 +35,7 @@ export function text(node, _, parent, state) { : stringifyEntities( node.value, Object.assign({}, state.settings.characterReferences, { - subset: ['<', '&'] + subset: textEntitySubset }) ) }