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

@anishbenji/tscache

v0.2.0

Published

Browser time-series cache: SharedWorker multi-tab sharing, worker-side fetch orchestration, coverage tracking with real-gap awareness

Readme

@anishbenji/tscache

A browser cache for timestamp-aligned time series: candles, metrics, sensor readings, event counts. It is built for a chart that scrolls through history and follows a live tail, in several tabs at once.

Measured benefit: in the demo comparison's workload (36 chart views over one tab, then three), tscache made 8 backend requests where no cache made 36 and a per-tab cache keyed by exact range made 28.

Two tabs share one cache: tab 2 draws the history at once and fetches only the newest bars

The two tabs in the recording are two frames of one page, which share the origin's SharedWorker as two tabs do.

  • One cache for every tab. The cache runs in a SharedWorker, so every same-origin tab reads the same data. Without SharedWorker it falls back to a dedicated Worker, then to the page itself; those give each tab a cache of its own, shared with no other tab.
  • The worker fetches. You give it a fetcher module. The worker fetches only what's missing, and tabs share fetches already in flight when they share a SharedWorker (see Browser support). How a read fetches has the details.
  • Known gaps are first-class. Coverage records what the cache knows, separately from the points that exist. A weekend without candles is a confirmed gap: it is never fetched again, and it is not confused with "not loaded yet".
  • Columns, not objects. A read returns one typed array per field, which chart libraries take as they are, moved from the worker without copying.
  • Honest freshness. A watermark marks the provisional tail that is always fetched again, a dataset version clears the cache when history changes, and invalidate forgets a range on your own clock. There is no TTL.

ESM-only, no runtime dependencies.

Install

npm install @anishbenji/tscache

Quick start

With Vite, add the plugin that serves the fetcher module:

// vite.config.ts
import { fetcherUrl } from "@anishbenji/tscache/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [fetcherUrl()],
  worker: { format: "es" },
});

Write a fetcher. The worker loads it with import() and calls its default export with the range to fetch:

// src/fetcher.ts
import type { Fetcher } from "@anishbenji/tscache/fetcher";

const fetcher: Fetcher = {
  async fetch(request) {
    const { start, end } = request.range;
    const response = await fetch(
      `/api/series/${request.cacheId}?start=${start}&end=${end}`,
    );
    if (!response.ok) throw new Error(`backend answered ${response.status}`);
    // { timestamps, fields, meta: { version, finalizedUntil } }
    // finalizedUntil: the first timestamp that is not final yet. Points at or
    // after it are fetched again. It only moves forward: a response without it
    // leaves it where it is, and a cache that never had one treats every point
    // and gap as final.
    return response.json();
  },
};

export default fetcher;

For TypeScript, declare the ?fetcher import once, in a file your tsconfig.json includes, such as src/ (Vite's client types already declare ?worker&url):

// src/env.d.ts
declare module "*?fetcher" {
  const url: string;
  export default url;
}

Then create a client on the page, open a cache and read a range:

// src/main.ts
import { createClient } from "@anishbenji/tscache";
import workerUrl from "@anishbenji/tscache/worker?worker&url";
import fetcherModule from "./fetcher.ts?fetcher";

const client = await createClient({
  workerUrl,
  fetcher: { module: fetcherModule },
});

const MINUTE = 60_000;
const candles = await client.cache({
  id: "candles/ACME",
  interval: MINUTE, // required: there is no default
  fields: { open: "f64", high: "f64", low: "f64", close: "f64", volume: "f64" },
});

const now = Math.floor(Date.now() / MINUTE) * MINUTE;
const result = await candles.get({ start: now - 6 * 60 * MINUTE, end: now });
// result.timestamps and result.fields: the points, one typed array per field
// result.coverage: the ranges the cache knows for certain
// result.misses: the ranges it does not, each with a reason
console.log(result.timestamps.length, result.misses);

A read never rejects because data is unavailable: a failed fetch comes back as a miss. The getting-started guide covers webpack, setups without a bundler, and the full fetcher contract.

Entries

| Import | For | | ----------------------------- | -------------------------------------------------------------------------------------- | | @anishbenji/tscache | the page: createClient, the types, the errors, encodeSnapshot and decodeSnapshot | | @anishbenji/tscache/worker | the worker script; you serve it, you never import it into the page | | @anishbenji/tscache/fetcher | your fetcher module: its types and AuthInvalidError, nothing else | | @anishbenji/tscache/engine | the engine without a worker, for Node, server rendering and tests | | @anishbenji/tscache/vite | fetcherUrl(), the Vite plugin for the fetcher module (vite 8 is an optional peer) |

Browser support

createClient tries three hostings in this order, starting from its mode option (default "shared"), and reports the one it settled on as client.mode:

  1. SharedWorker ("shared"), where the browser has it. Every same-origin tab that loads the same worker script shares one cache and its fetches.
  2. Dedicated worker ("dedicated"): one Worker per tab, each with a cache of its own.
  3. In-process ("in-process"): the engine in the page itself, with no sharing.

Each step down emits a modeFallback event with the reason. Chrome on Android before version 148 has no SharedWorker, so there every tab gets its own cache through the dedicated worker. Only the SharedWorker hosting persists a cache with persist: true. Workers and fallback has the details.

Stability

tscache follows semantic versioning within 0.x: a minor release (0.3.0) may break the API, a patch release (0.2.1) never does. Pin a minor range (~0.2.0) to take fixes without breaking changes. Known limits of 0.2.0: unbounded memory, deploy skew, and change events that reach other tabs only through a SharedWorker.

Documentation

License

MIT