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 🙏

© 2024 – Pkg Stats / Ryan Hefner

timer-wheel

v1.0.0

Published

Timer wheel for managing expiration of a large amount of items

Downloads

3,975

Readme

Timer Wheel

npm version Build Status Coverage Status Dependencies

timer-wheel is a library for JavaScript and TypeScript for efficiently managing the expiration of a large amount of items. It allows you to schedule items to be expired after a certain delay and then control when to advance and handle the expired items.

This implementation is designed for larger delays and has a minimum delay of 1000 ms.

import { TimerWheel } from 'timer-wheel';

const wheel = new TimerWheel<any>();
wheel.schedule('data', 1500 /* ms */)

// Call `advance()` to advance the wheel and run actions
setInterval(() => {
  const expired = wheel.advance();
  for(const data of expired) {
    // Do something with the expired data
  }
}, 1000);

This library is useful for things like expiring caches with lazy expiration, instead of checking if every cache item should be expired use a wheel to queue removal actions and call advance before every get/set. This is how Transitory implements expiring caches.

Rescheduling

By default TimerWheel will schedule the same object to expired more than once. This will schedule obj to be expired both after 1 and 5 seconds:

const wheel = new TimerWheel();
const obj = {};

// Schedule to be expired after 1 seconds
wheel.schedule(obj, 1000);

// Schedule to also be expired after 5 seconds
wheel.schedule(obj, 5000);

If you want to be able to reschedule when an item expires use ReschedulingTimerWheel:

import { ReschedulingTimerWheel } from 'timer-wheel';

const wheel = new ReschedulingTimerWheel();

const obj = {};

// First schedule at 1 second
wheel.schedule(obj, 1000);

// Replace first scheduling with a new one after 5 seconds
wheel.schedule(obj, 5000);

Running actions

There's a wheel designed to run actions available:

import { ActionTimerWheel } from 'timer-wheel';

const wheel = new ActionTimerWheel();

wheel.schedule(() => {
  /* do something here */
}, 8000);

// Advance the wheel to run expired actions
wheel.advance();