npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hedge-fetch

v0.9.0

Published

An adaptive, speculative request-hedging library that automatically cuts p95 tail latency.

Readme

hedge-fetch

Adaptive, speculative request hedging for the modern web. hedgehog-fetch is a high-performance wrapper around the Fetch API designed to eliminate "tail latency" (the slow P95/P99 requests). By intelligently firing a second "speculative" request when the first one takes too long, Hedgehog ensures your users never wait on a stray slow server.

Key Features

  • Adaptive P95 Delay: No hardcoded timeouts. Hedgehog learns your network's latency and hedges exactly when a request is statistically "late."
  • Idempotency Safety: Automatically handles Idempotency-Key headers for POST requests to prevent duplicate server-side actions.
  • Zero Leakage: Uses modern AbortSignal.any() to ensure that once a winner is found, the loser is aborted immediately—no dangling connections.
  • Plugin-Ready Buckets: Ship with a local token bucket, or plug in Redis to coordinate hedging budgets across a global cluster.
  • Developer Visibility: Built-in hooks and response decoration (res.isHedged) for deep observability.

Installation

npm install hedge-fetch

Quick Start

import { HedgedContext, LocalTokenBucket, LatencyTracker } from 'hedge-fetch';

// 1. Initialize the context
const hedge = new HedgedContext(
  new LocalTokenBucket(10), // Allow 10% hedging overhead
  new LatencyTracker()      // Adaptive learning
);

// 2. Use it just like native fetch
const response = await hedge.fetch('https://api.example.com/data', {
  timeoutMs: 5000, // Global safety net
  onHedge: () => console.log('Hedging triggered!')
});

// 3. Check if the hedge saved the day
if ((response as any).isHedged) {
  console.log('Speculative request won!');
}

Deep Dive: How it Works

1. The Adaptive Delay (P95 Algorithm)

Instead of guessing a timeout (e.g., "wait 200ms"), Hedgehog uses the LatencyTracker. It maintains a sliding window of recent request durations and calculates the 95th percentile. If your primary request hasn't responded by the P95 mark, it is statistically likely to be a "tail latency" request, and Hedgehog fires the speculative request.

2. Idempotency & Safety

Hedging POST or PATCH requests is usually dangerous. Hedgehog makes it safe:

  • Safe Methods: GET, HEAD, OPTIONS are hedged by default.
  • Unsafe Methods: POST is only hedged if forceHedge: true is passed.
  • Auto-Key: If enabled, Hedgehog generates a UUID and attaches it to the Idempotency-Key header, ensuring your backend doesn't process the same action twice.

3. Distributed Budgets (Redis)

To prevent your fleet of servers from DDOSing your own backend during a slowdown, Hedgehog uses a Token Bucket. You can implement the IHedgeBucket interface to sync this budget across multiple instances using Redis.

class RedisBucket implements IHedgeBucket {
  async canHedge() {
    const tokens = await redis.get('hedge_tokens');
    return parseInt(tokens) > 0;
  }
  // ...
}

Technical Reference

HedgeFetchOptions

Extends the standard RequestInit with:

| Option | Type | Description | | --- | --- | --- | | timeoutMs | number | The global safety net. Aborts everything if no response in X ms. | | forceHedge | boolean | Bypass safety checks for non-idempotent methods. | | onHedge | () => void | Callback when the speculative request is fired. | | onPrimaryWin | (ms) => void | Callback when the first request succeeds. | | onSpeculativeWin | (ms) => void | Callback when the second request succeeds. |

Response Decoration

Successful responses from a speculative request are decorated with a non-enumerable property:

const res = await hedge.fetch(...);
console.log(res.isHedged); // true if the speculative request won

Best Practices

  1. Backend Support: Ensure your backend ignores duplicate Idempotency-Key headers for the best experience.
  2. Budgeting: Start with a 5-10% budget (LocalTokenBucket) to improve latency without significantly increasing server cost.
  3. Global Timeouts: Always set a timeoutMs to prevent "hanging" UI states in extreme network failure scenarios.

Contributing

Contributions are welcome! If you have ideas for new resilience patterns (like Rate Limiting or Timeouts), feel free to open an issue or a PR.


License

MIT © Ali nazari

Built with ❤️ for the Node.js community. Star this repo if it helped you sleep better at night!