You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The operators and literalValues inputs are identical across all tokenizer instances for a given Liquid engine. In scenarios where many short template strings are parsed in a single pass (e.g. validating hundreds of {{ variable }} expressions in a document), this creates significant overhead from redundant trie construction.
Profiling data
We're using LiquidJS (v10.25.0) inside Kibana's workflow YAML editor to validate Liquid template expressions on every keystroke. For a large document (~1,500 lines, ~360 template variables), CPU profiling shows:
We already cache parse() results (LRU by template string), which helps on cache hits. But on cache misses, createTrie is the dominant cost inside the tokenizer — more expensive than the actual tokenization work.
Suggested fix
Cache the tries at the Liquid engine level (or Parser level) and pass them into Tokenizer instances, since the operator set and literal values don't change after engine construction:
// In Parser or Liquid engine — build oncethis.opTrie=createTrie(this.options.operators);this.literalTrie=createTrie(literalValues);// In Tokenizer constructor — reuseconstructor(input,opTrie,literalTrie,file,range){this.opTrie=opTrie;this.literalTrie=literalTrie;// ...}
This would be a non-breaking internal change. The Tokenizer constructor isn't part of the public API contract (users go through Liquid.parse()).
Problem
Every
new Tokenizer(...)call rebuilds two tries from scratch viacreateTrie():The
operatorsandliteralValuesinputs are identical across all tokenizer instances for a givenLiquidengine. In scenarios where many short template strings are parsed in a single pass (e.g. validating hundreds of{{ variable }}expressions in a document), this creates significant overhead from redundant trie construction.Profiling data
We're using LiquidJS (v10.25.0) inside Kibana's workflow YAML editor to validate Liquid template expressions on every keystroke. For a large document (~1,500 lines, ~360 template variables), CPU profiling shows:
createTrie: 173ms self-time — the Conversion to Async #10 hottest function by self-timeTokenizer → createTrieparseTemplateString → parse → readTopLevelToken → readTagToken → TagToken → Tokenizer → createTrieWe already cache
parse()results (LRU by template string), which helps on cache hits. But on cache misses,createTrieis the dominant cost inside the tokenizer — more expensive than the actual tokenization work.Suggested fix
Cache the tries at the
Liquidengine level (orParserlevel) and pass them intoTokenizerinstances, since the operator set and literal values don't change after engine construction:This would be a non-breaking internal change. The
Tokenizerconstructor isn't part of the public API contract (users go throughLiquid.parse()).Environment
parse()calls per validation pass