Skip to content

Commit

Permalink
feat: export Parser for performance-sensitive applications
Browse files Browse the repository at this point in the history
The existing API creates a new instance of this parser for each
invocation. If you call these functions often, reusing the parser
instance can help performance.
  • Loading branch information
wkillerud committed Jul 23, 2024
1 parent d3b417c commit 38927db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,30 @@ The result from the `parse` function is an array of [`ParseResult` (type definit
- [Example output for SCSS](/src/sassdoc-parser.test.ts)
- [Example output for indented](/src/sassdoc-parser-indented.test.ts)
## Advanced usage
If you're running this parser on a hot code path, you might discover a noticable time is spent constructing the `Parser` class. The `parse` and `parseSync` methods create a new instance of this parser for each invocation. To reuse the same parser instance,
import the `Parser` class and use that instead.
```js
import { Parser } from "sassdoc-parser";

const parser = new Parser();

const result = await parser.parseString(`
/// Keeps it secret
/// @output Sets display to hidden
@mixin _keep-it-secret {
display: hidden;
}
`);

const syncResult = parser.parseStringSync(`
/// Keeps it secret
/// @output Sets display to hidden
@mixin _keep-it-secret {
display: hidden;
}
`);
```
2 changes: 1 addition & 1 deletion src/sassdoc-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sorter from "./sorter.js";
import type { ParseResult } from "./types.js";
import { removeReduntantWhitespace } from "./utils.js";

class Parser {
export class Parser {
annotations: AnnotationsApi;
commentParser: SassCommentParser;

Expand Down

0 comments on commit 38927db

Please sign in to comment.