Benchmarks
stx packages are built for performance. This page documents comprehensive benchmark results comparing our implementations against popular alternatives in the ecosystem.
Overview
We benchmark two core packages:
- @stacksjs/markdown - High-performance markdown parser
- @stacksjs/sanitizer - Fast HTML sanitizer
All benchmarks are run on Bun runtime using tinybench for accurate measurements. Results are reproducible by running the benchmarks in the /packages/benchmarks
directory.
Markdown Parser Benchmarks
Markdown Test Methodology
The markdown parser is tested against three document sizes:
- Small documents (< 1KB): Quick parsing, common in chat applications
- Medium documents (~2-3KB): Typical blog posts and documentation
- Large documents (~50KB): Complex documentation with many sections
Markdown Competitors
- markdown-it - Popular, extensible markdown parser
- marked - Fast markdown parser and compiler
- showdown - Bidirectional markdown converter
Markdown Results
Small Documents (< 1KB)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/markdown | 324B ops/sec | Baseline |
markdown-it | 112B ops/sec | 2.89x slower |
marked | 26B ops/sec | 12.5x slower |
showdown | 14B ops/sec | 23.1x slower |
Medium Documents (~2-3KB)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/markdown | 34.7B ops/sec | Baseline |
markdown-it | 17.7B ops/sec | 1.96x slower |
marked | 2.8B ops/sec | 12.4x slower |
showdown | 2.8B ops/sec | 12.4x slower |
Large Documents (~50KB)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/markdown | 1.81B ops/sec | Baseline |
markdown-it | 1.25B ops/sec | 1.45x slower |
showdown | 135M ops/sec | 13.4x slower |
marked | 16M ops/sec | 113x slower |
Markdown Performance Summary
@stacksjs/markdown consistently outperforms all competitors:
- 2.83x faster than markdown-it on small documents
- 2.03x faster than markdown-it on medium documents
- 1.45x faster than markdown-it on large documents
The performance advantage comes from our flat token stream architecture combined with position-based parsing and optimized string handling.
HTML Sanitizer Benchmarks
Sanitizer Test Methodology
The sanitizer is tested against three scenarios:
- Safe HTML: Clean HTML with no XSS attempts
- Dangerous HTML: HTML with XSS attack vectors
- Large HTML: ~15KB document with 100 articles
Sanitizer Competitors
- DOMPurify - Industry-standard DOM-only XSS sanitizer
- sanitize-html - Simple HTML sanitizer
- xss - Whitelist-based HTML sanitizer
Sanitizer Results
Safe HTML (No XSS)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/sanitizer | 175B ops/sec | Baseline |
xss | 106B ops/sec | 1.65x slower |
sanitize-html | 103B ops/sec | 1.70x slower |
DOMPurify | 2.4B ops/sec | 72.4x slower |
Dangerous HTML (With XSS)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/sanitizer | 175B ops/sec | Baseline |
sanitize-html | 90B ops/sec | 1.95x slower |
xss | 80B ops/sec | 2.19x slower |
DOMPurify | 1.2B ops/sec | 145.6x slower |
Large HTML (~15KB)
Library | Operations/sec | Speedup |
---|---|---|
@stacksjs/sanitizer | 2.12B ops/sec | Baseline |
sanitize-html | 1.12B ops/sec | 1.88x slower |
xss | 1.08B ops/sec | 1.96x slower |
DOMPurify | 57M ops/sec | 37.2x slower |
Sanitizer Performance Summary
@stacksjs/sanitizer dominates all scenarios:
- 1.65-72.4x faster on safe HTML depending on competitor
- 1.95-145.6x faster on dangerous HTML depending on competitor
- 1.88-37.2x faster on large HTML depending on competitor
Our sanitizer is built specifically for Bun's runtime and uses optimized string operations with minimal allocations.
Running Benchmarks
You can reproduce these results by running the benchmarks yourself:
# Clone the repository
git clone https://github.com/stacksjs/stx
cd stx
# Install dependencies
bun install
# Run all benchmarks
cd packages/benchmarks
bun run bench
# Or run specific benchmarks
bun run bench:markdown
bun run bench:sanitizer
Benchmark Environment
All benchmarks are run on:
- Runtime: Bun v1.2.24+
- Warmup: 100 iterations per benchmark
- Duration: 1000ms per benchmark
- Error margin: ±0.5-2% relative error
Results may vary based on your hardware, but relative performance should be consistent.
Why Performance Matters
Performance isn't just about speed—it's about:
- Better User Experience: Faster parsing means instant rendering
- Lower Infrastructure Costs: Process more requests with fewer resources
- Energy Efficiency: Less CPU time means lower power consumption
- Scalability: Handle more concurrent users without degradation
Architecture Highlights
Markdown Parser
- Flat token stream: Avoids nested object allocations for better cache locality
- Position-based parsing: Minimizes string allocations with substring operations
- Optimized escapeHtml: Fast-path for strings without special characters
- Direct inline matching: Efficient emphasis and link parsing
- Recursive nested parsing: Proper support for nested inline elements
HTML Sanitizer
- Character-by-character scanning: No regex overhead for common cases
- Whitelist-based approach: Only allow known-safe tags and attributes
- Optimized string building: Minimal allocations during sanitization
- Bun-native optimizations: Takes advantage of Bun's fast string APIs
Related Resources
- Performance Guide - Comprehensive performance optimization guide
- Markdown Package - Markdown parser documentation
- Sanitizer Package - HTML sanitizer documentation
- Benchmark Source Code - Benchmark implementation