From b5fa4d02599cf8ec840d70e813b08959ba0ec21d Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Wed, 17 Aug 2022 08:45:12 -0700 Subject: [PATCH] perf: optimize parser constructor (#136) --- .changeset/blue-phones-agree.md | 5 +++++ src/core/Parser.ts | 34 ++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 .changeset/blue-phones-agree.md diff --git a/.changeset/blue-phones-agree.md b/.changeset/blue-phones-agree.md new file mode 100644 index 0000000..b101cb1 --- /dev/null +++ b/.changeset/blue-phones-agree.md @@ -0,0 +1,5 @@ +--- +"htmljs-parser": patch +--- + +Optimize parser constructor to avoid initializing unecessary properties. diff --git a/src/core/Parser.ts b/src/core/Parser.ts index 4e6a1bd..cff1a37 100644 --- a/src/core/Parser.ts +++ b/src/core/Parser.ts @@ -30,24 +30,22 @@ export interface StateDefinition

{ } export class Parser { - public pos!: number; - public maxPos!: number; - public data!: string; - public activeState!: StateDefinition; - public activeRange!: Meta; - public forward!: number; - public activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag - public activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed - public indent!: string; // Used to build the indent for the current concise line - public isConcise!: boolean; // Set to true if parser is currently in concise mode - public beginMixedMode?: boolean; // Used as a flag to mark that the next HTML block should enter the parser into HTML mode - public endingMixedModeAtEOL?: boolean; // Used as a flag to record that the next EOL to exit HTML mode and go back to concise - public textPos!: number; // Used to buffer text that is found within the body of a tag - public lines: undefined | number[]; // Keeps track of line indexes to provide line/column info. - - constructor(public options: Options) { - this.options = options; - } + public declare pos: number; + public declare maxPos: number; + public declare data: string; + public declare activeState: StateDefinition; + public declare activeRange: Meta; + public declare forward: number; + public declare activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag + public declare activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed + public declare indent: string; // Used to build the indent for the current concise line + public declare isConcise: boolean; // Set to true if parser is currently in concise mode + public declare beginMixedMode?: boolean; // Used as a flag to mark that the next HTML block should enter the parser into HTML mode + public declare endingMixedModeAtEOL?: boolean; // Used as a flag to record that the next EOL to exit HTML mode and go back to concise + public declare textPos: number; // Used to buffer text that is found within the body of a tag + public declare lines: undefined | number[]; // Keeps track of line indexes to provide line/column info. + + constructor(public options: Options) {} read(range: Range) { return this.data.slice(range.start, range.end);