HdrHistogram.js

3.0.1 · active · verified Sun Apr 19

HdrHistogram.js is a TypeScript port of the HdrHistogram library, designed for high-fidelity recording and analysis of latency and other value distributions in both Node.js and web browser environments. The library is currently stable at version 3.0.1, with a history of regular updates including significant architectural changes. A key differentiator since version 2 is the introduction of an optional WebAssembly (WASM) implementation, leveraging AssemblyScript to provide substantial performance gains for recording and processing histograms, though this is not enabled by default. It supports core HdrHistogram features such as regular and coordinated omission-corrected latency recording, resizable and memory-optimized 'packed' histograms, and the ability to add, subtract, encode, and decode compressed histograms. It also includes client-side data visualization tools for browser-based analysis. Release cadence appears to be driven by feature enhancements and performance optimizations, with major versions often introducing substantial improvements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to build a histogram, record values, and retrieve basic percentile metrics, including an example of coordinated omission correction.

import { build, Histogram } from 'hdr-histogram-js';

// Build a default histogram with auto-resizing, tracking values from 1 to 2, and 3 significant digits.
// WebAssembly is not enabled by default for maximum compatibility.
const histogram: Histogram = build({
  lowestDiscernibleValue: 1,
  highestTrackableValue: 10000000000, // 10 billion
  numberOfSignificantValueDigits: 3,
  useWebAssembly: false // Explicitly set to false, true for performance if available
});

// Record some values representing latencies in microseconds
histogram.recordValue(1000);
histogram.recordValue(5000);
histogram.recordValue(100);
for (let i = 0; i < 1000; i++) {
  histogram.recordValue(Math.floor(Math.random() * 9000) + 1000);
}
histogram.recordValue(9999);

console.log(`Total count: ${histogram.totalCount}`);
console.log(`90th percentile: ${histogram.getValueAtPercentile(90)} µs`);
console.log(`99th percentile: ${histogram.getValueAtPercentile(99)} µs`);
console.log(`Max value: ${histogram.maxValue} µs`);

// Example of coordinated omission correction
const histogramWithCoordinatedOmissions = build({
  lowestDiscernibleValue: 1,
  highestTrackableValue: 10000000000,
  numberOfSignificantValueDigits: 3
});

histogramWithCoordinatedOmissions.recordValueWithExpectedInterval(100, 10); // Record a value of 100, expecting 10ms interval
console.log(`
Histogram with coordinated omissions (total count: ${histogramWithCoordinatedOmissions.totalCount}):`);
console.log(`90th percentile (corrected): ${histogramWithCoordinatedOmissions.getValueAtPercentile(90)} µs`);

view raw JSON →