Skip to content

Commit 4cd41f0

Browse files
committed
Improve performance 10..50x by caching the Tokenizer
Benchmarked both from command line and in Chrome, Firefox, Safari. Saw about 10 to 50x improvement.
1 parent f99bc23 commit 4cd41f0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/core/Formatter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@ export default class Formatter {
2626
throw new Error('tokenizer() not implemented by subclass');
2727
}
2828

29+
// Cache the tokenizer for each class (each SQL dialect)
30+
// So we wouldn't need to recreate the tokenizer, which is kinda expensive,
31+
// for each call to format() function.
32+
private cachedTokenizer(): Tokenizer {
33+
const cls: Function & { cachedTokenizer?: Tokenizer } = this.constructor;
34+
if (!cls.cachedTokenizer) {
35+
cls.cachedTokenizer = this.tokenizer();
36+
}
37+
return cls.cachedTokenizer;
38+
}
39+
2940
/**
3041
* Formats an SQL query.
3142
* @param {string} query - The SQL query string to be formatted
3243
* @return {string} The formatter query
3344
*/
3445
public format(query: string): string {
35-
const tokens = this.tokenizer().tokenize(query);
46+
const tokens = this.cachedTokenizer().tokenize(query);
3647
const ast = new Parser(tokens).parse();
3748
const formattedQuery = this.formatAst(ast, tokens);
3849
const finalQuery = this.postFormat(formattedQuery);

0 commit comments

Comments
 (0)