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

raf-schd

v4.0.3

Published

A scheduler based on requestAnimationFrame

Downloads

9,117,925

Readme

raf-schd

A throttle function that uses requestAnimationFrame to limit the rate at which a function is called.

Build Status dependencies npm SemVer

For background information on rate limiting functions, see Rate limiting functions from scratch

import rafSchd from 'raf-schd';

const expensiveFn = arg => {
  //...
  console.log(arg);
};

const schedule = rafSchd(expensiveFn);

schedule('foo');
schedule('bar');
schedule('baz');

// animation frame fires

// => 'baz'

Why?

raf-schd supports the use case where you want to limit the rate at which your functions are called based on requestAnimationFrame. Unlike a standard throttle function, raf-schd uses requestAnimationFrame to rate limit, rather than waiting a fixed amount of time. Using requestAnimationFrame for throttling is powerful because the browser will automatically reduce the amount of frames provided based on the available resources. So if the browser only provides 30fps then your throttled function will only be called 30 times.

raf-schd is an extremely useful performance utility.

Without raf-schd

Optimised scroll listener example taken from MDN

var last_known_scroll_position = 0;
var ticking = false;

function doSomething(scroll_pos) {
  // do something with the scroll position
}

window.addEventListener('scroll', function(e) {
  last_known_scroll_position = window.scrollY;
  if (!ticking) {
    window.requestAnimationFrame(function() {
      doSomething(last_known_scroll_position);
      ticking = false;
    });
  }
  ticking = true;
});

With raf-schd

import rafSchd from 'raf-schd';

function doSomething(scroll_pos) {
  // do something with the scroll position
}

const schedule = rafSchd(doSomething);

window.addEventListener('scroll', function() {
  schedule(window.scrollY);
});

At the top level raf-schd accepts any function a returns a new ResultFn (a function that wraps your original function).

The ResultFn will execute your function with the latest arguments provided to it on the next animation frame.

Throttled with latest argument

import rafSchd from 'raf-schd';

const doSomething = () => {...};

const schedule = rafSchd(doSomething);

schedule(1, 2);
schedule(3, 4);
schedule(5, 6);

// animation frame fires

// do something called with => '5, 6'

Cancelling a frame with .cancel

raf-schd adds a .cancel property to the ResultFn so that it can be easily cancelled. The frame will only be cancelled if it has not yet executed.

const schedule = rafSchd(doSomething);

schedule('foo');

schedule.cancel();

// now doSomething will not be executed in the next animation frame

Types

rafSchedule

type rafSchedule = (fn: Function) => ResultFn;

// Adding a .cancel property to the WrapperFn

type WrapperFn = (...arg: mixed[]) => void;
type CancelFn = {|
  cancel: () => void,
|};
type ResultFn = WrapperFn & CancelFn;

Testing your code

If you want to really ensure that your code is working how you intend it to - use raf-stub to test your animation frame logic.

Installation

# yarn
yarn add raf-schd

# npm
npm install raf-schd --save

Module usage

ES6 module

import rafSchd from 'raf-schd';

CommonJS

If you are in a CommonJS environment (eg Node), then you will need add .default to your import:

const rafSchd = require('raf-schd').default;