Skip to content

πŸš€ Tempo Performance Benchmarks ​

To ensure high performance in mass-instantiation scenarios, Tempo implements a "Zero-Cost Constructor" architecture using prototype shadowing and lazy delegation. This document outlines the benchmark results for these optimizations.

πŸ“Š Summary of Timings ​

Timings were captured over 1,000 iterations to measure micro-overhead and compare different instantiation strategies.

MethodTotal TimeΒ΅s / opNotes
Default (Lazy Proxy)523.14ms523.14Β΅sO(1) constructor.
Eager Simulation1394.51ms1394.51Β΅s~2.7x slower (simulating pre-refactor impact).
Fast-Fail @sync359.04ms359.04Β΅sRejected instantly by the Master Guard.
Object.create Baseline0.22ms0.22Β΅sRaw JS overhead for comparison.

πŸ—οΈ Architectural Impact ​

1. Proxy-Delegator Delegation (O(1)) ​

By using a Proxy-Delegator pattern, the constructor returns near-instantly without populating the formatting (fmt) or Term (term) objects. These registries are only discovered and memoized on the first property access.

  • Gain: ~65% reduction in instantiation overhead.
  • v2.1.2 Update: The Scan-and-Consume guard further stabilizes the "Zero-Cost Constructor" by ensuring that even with massive plugin lists, the entry-point remains O(1).

2. The Master Guard (Fast-Fail) ​

The dynamic Master Guard acts as a rapid scan-and-consume gatekeeper.

  • Efficiency: Invalid inputs are rapidly rejected via a fast greedy scan, bounding failure costs tightly to string length.
  • Performance: Validating a string against the guard is ~30% faster than a full parsing cycle, even for simple ISO strings.

πŸ§ͺ Benchmark Methodology ​

The benchmark script used performance.now() within a Vitest environment to ensure accurate module resolution and test environment isolation.

  1. Lazy Creation: Creates a new Tempo('2024-05-20') without accessing any properties.
  2. Eager Simulation: Creates a new Tempo() and manually triggers discovery on 5 core properties to simulate O(N) initialization.
  3. Invalid Parse: Passes a string that fails the Master Guard (e.g., includes emojis or exotic symbols) to measure rejection speed.

INFO

These benchmarks represent the library's performance under Node.js v22+. Results may vary based on the JS engine (V8, JavaScriptCore, etc.) but the O(1) complexity remains constant.


πŸ“ˆ Self-Service Benchmark Module ​

Tempo provides a built-in BenchmarkModule to allow developers to prove the performance ROI in their own exact environment (e.g. comparing Node.js memory metrics against Browser DOM performance).

This module is completely decoupled from the core to keep the bundle light, but accepts your specific configured Tempo instance to give accurate results.

Usage ​

typescript
import { Tempo } from 'tempo-kit';
import { BenchmarkModule } from 'tempo-kit/module/benchmark';

// 1. Configure your Tempo instance (e.g. plugins, timezone, etc)
Tempo.init({ timeZone: 'America/New_York' });

// 2. Run the benchmark against your datasets
const results = BenchmarkModule.run(Tempo, {
  data: ['2026-05-20', '2026-05-21', 'Invalid-Pizza-String'], // production datasets
  iterations: 100, // stress-test count
  baseline: true,  // compare against native `new Date()`
  modes: ['auto', 'defer']
});

console.table(results);

Metrics Collected ​

The BenchmarkModule is platform-aware. Using the internal getContext() utility, it tracks:

  • totalTimeMs: Total elapsed time using high-resolution timers (performance.now()).
  • microSecPerOp: The average number of microseconds (Β΅s) taken per operation.
  • successRate: The percentage of strings that successfully resolved to a valid date.
  • heapUsedDeltaMb: (Node.js Only) The change in memory allocation to measure Garbage Collection overhead and memory safety.

Indicative Benchmark Results (v5.0.0) ​

When evaluated with bench.v5.0.0.ts (--expose-gc + per-mode warm-up) against a 20-entry mixed corpus (Strict ISO, compact numeric shorthands, relative expressions like next Friday at 5pm and in 3 weeks, and epoch timestamps) over 2,000 operations (100 iterations Γ— 20 inputs) on Node.js v26.10.0, BenchmarkModule yields:

StrategyΒ΅s / OpTotal TimeSuccess RateMemory OverheadNotes
Native Date0.18 Β΅s0.36 ms25.0%0.23 MBRaw C++ speed, but fails on 1,500/2,000 shorthand, relative, and natural-language inputs.
Tempo (Auto)957.59 Β΅s1,915.19 ms100.0%70.01 MBSub-millisecond full natural-language, shorthand, and localized parsing (~2.32Γ— faster than v4.4.3).
Tempo (Defer)946.40 Β΅s1,892.80 ms100.0%65.30 MBFastest overall: defers deep eager discovery until accessed (~2.28Γ— faster than v4.4.3).
Tempo (Strict)1,076.69 Β΅s2,153.38 ms100.0%105.16 MB~2.08Γ— faster than v4.4.3; bypasses heuristic fallback attempts.

Additional v5.0.0 Throughput Profiles ​

  • Stock Baseline (WeakCache, 20-entry baseCorpus Γ— 100 iterations = 2,000 ops): 1,030.68 Β΅s / op (970 ops/sec, 100.0% success rate).
  • Repeated ISO Memoization (WeakCache, 4-entry repeatCorpus Γ— 500 iterations = 2,000 ops): 1,042.82 Β΅s / op (959 ops/sec, 100.0% success rate).
  • Localized fr-FR Modifiers (8-entry frCorpus Γ— 100 iterations = 800 ops): 1,292.63 Β΅s / op (774 ops/sec, 100.0% success rate).

πŸ” Key Takeaways ​

  1. Sub-Millisecond Rich Parsing: Native Date is fast (0.18 Β΅s) on strict ISO/RFC strings, but fails 75% of the mixed corpus (25.0% success). Tempo resolves 100.0% of the corpus at ~0.95 ms / op in auto and defer modes.
  2. 2Γ—+ Throughput Improvement Over v4.4.3: Compared against the committed v4.4.3 benchmark artifact (v4.4.3 time Γ· v5.0.0 time), v5.0.0 records 2.08×–2.36Γ— lower per-operation timings across the three corpora (baseCorpus 20Γ—100, repeatCorpus 4Γ—500, and frCorpus 8Γ—100). Note that the v5.0.0 BenchmarkModule harness adds a per-mode warm-up loop and a pre-timing gc() call (--expose-gc) not present in the v4.4.3 run, so part of the auto/defer/strict reduction reflects cleaner warm-up/GC isolation in addition to engine gains.
  3. Adapt This Test: The runner script used to generate these results is available in the bench/runner.test.ts directory. You can use it as a scaffold to test Tempo against your own unique datasets.

Released under the MIT License.