Skip to content

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)

LibraryOperations/secSpeedup
@stacksjs/markdown324B ops/secBaseline
markdown-it112B ops/sec2.89x slower
marked26B ops/sec12.5x slower
showdown14B ops/sec23.1x slower

Medium Documents (~2-3KB)

LibraryOperations/secSpeedup
@stacksjs/markdown34.7B ops/secBaseline
markdown-it17.7B ops/sec1.96x slower
marked2.8B ops/sec12.4x slower
showdown2.8B ops/sec12.4x slower

Large Documents (~50KB)

LibraryOperations/secSpeedup
@stacksjs/markdown1.81B ops/secBaseline
markdown-it1.25B ops/sec1.45x slower
showdown135M ops/sec13.4x slower
marked16M ops/sec113x 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)

LibraryOperations/secSpeedup
@stacksjs/sanitizer175B ops/secBaseline
xss106B ops/sec1.65x slower
sanitize-html103B ops/sec1.70x slower
DOMPurify2.4B ops/sec72.4x slower

Dangerous HTML (With XSS)

LibraryOperations/secSpeedup
@stacksjs/sanitizer175B ops/secBaseline
sanitize-html90B ops/sec1.95x slower
xss80B ops/sec2.19x slower
DOMPurify1.2B ops/sec145.6x slower

Large HTML (~15KB)

LibraryOperations/secSpeedup
@stacksjs/sanitizer2.12B ops/secBaseline
sanitize-html1.12B ops/sec1.88x slower
xss1.08B ops/sec1.96x slower
DOMPurify57M ops/sec37.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:

bash
# 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:

  1. Better User Experience: Faster parsing means instant rendering
  2. Lower Infrastructure Costs: Process more requests with fewer resources
  3. Energy Efficiency: Less CPU time means lower power consumption
  4. 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

Released under the MIT License.