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

idlefy

v1.1.1

Published

Defer non-critical tasks to run when the main thread is idle

Downloads

7,940

Readme

idlefy

Defer non-critical tasks to execute when the main thread is idle

The idlefy library is a collection of JavaScript tools designed to help you improve website performance by strategically scheduling tasks to run during the browser's idle periods. This adheres to the idle-until-urgent pattern.

Inspired by the idilize library, idlefy not only retains all the original features but also introduces enhanced TypeScript support, a reduced bundle size, and a more adaptable API. Moreover, it seamlessly serves as a drop-in replacement for idilize, passing all the tests from the original library.

  • TypeScript Precision: Enjoy the benefits of type safety, better code structure, and improved developer productivity.
  • SSR Compatibility: Our library seamlessly integrates with server-side rendering.
  • Light on Disk: An optimized bundle ensures minimal overhead and maximum impact and you can leverage tree shaking to remove unused code by using module level imports.

Installation

npm install idlefy

Usage

// imports all helpers methods and classes from idlefy library
import { IdleQueue, IdleValue } from 'idlefy'
// import only methods and classes you need
import { IdleQueue } from 'idlefy/idleQueue.js'
// import methods from minified build
import { IdleQueue } from 'idlefy/min'

IdleQueue

idlefy/idleQueue

Overview

It's useful for apps that want to split up their logic into a sequence of functions and schedule them to run idly.

This class offers a few benefits over the regular usage of requestIdleCallback():

  • The queue can be configured so all queued functions are guaranteed to run before the page is unloaded.
  • Queued tasks can be run immediately at any time.
  • Queued tasks can pass a minimum time budget, below which they won't attempt to run (this minimum time budget can also be configured per queue).
  • Queued tasks store the time/visibilityState when they were added to the queue, and are invoked with this data when run.

Exports

Usage

import { IdleQueue } from 'idlefy/idleQueue.js'

const queue = new IdleQueue()

queue.pushTask(() => {
  // Some expensive function that can run idly...
})

queue.pushTask(() => {
  // Some other task that depends on the above
  // expensive function having already run...
})

Methods

IdleValue

idlefy/idleValue

Overview

It's useful when you want to initialize a value during an idle period but ensure it can be initialized immediately as soon as it's needed.

Exports

Usage

import { IdleValue } from 'idlefy/idleValue.js'

class MyClass {
  constructor() {
    // Create an IdleValue instance for `this.data`. It's value is
    // initialized in an idle callback (or immediately as soon as
    // `this.data.getValue()` is called).
    this.data = new IdleValue(() => {
      // Run expensive code and return the result...
    })
  }
}

Methods

idle-callback-with-polyfills

idlefy/idleCbWithPolyfill

Overview

Small polyfills that allow developers to use requestIdleCallback and cancelIdleCallback() in all browsers.

These are not full polyfills (since the native APIs cannot be fully polyfilled), but they offer the basic benefits of idle tasks via setTimeout() and clearTimeout().

Exports

Usage

import { cIC, rIC } from 'idlefy/idleCbWithPolyfill.js'

// To run a task when idle.
const handle = rIC(() => {
  // Do something here...
})

// To cancel the idle callback.
cIC(handle)

rIC

Uses the native requestIdleCallback() function in browsers that support it, or a small polyfill (based on setTimeout()) in browsers that don't.

See the requestIdleCallback() docs on MDN for details.

cIC

Uses the native cancelIdleCallback() function in browsers that support it, or a small polyfill (based on clearTimeout()) in browsers that don't.

See the cancelIdleCallback() docs on MDN for details.

defineIdleProperty

idlefy/defineIdleProperty

Overview

It's useful when you want to initialize a property value during an idle period but ensure it can be initialized immediately as soon as it's referenced.

Exports

Usage

import { defineIdleProperty } from 'idlefy/defineIdleProperty.js'

class MyClass {
  constructor() {
    // Define a getter for `this.data` whose value is initialized
    // in an idle callback (or immediately if referenced).
    defineIdleProperty(this, 'data', () => {
      // Run expensive code and return the result...
    })
  }
}

Syntax

defineIdleProperty(obj, prop, init)

Parameters

defineIdleProperties

idlefy/defineIdleProperties

Overview

It's useful when you want to initialize one or more property values during an idle period but ensure they can be initialized immediately as soon as they're referenced.

Exports

Usage

import { defineIdleProperties } from 'idlefy/defineIdleProperties.js'

class MyClass {
  constructor() {
    // Define a getter for `this.data` whose value is initialized
    // in an idle callback (or immediately if referenced).
    defineIdleProperties(this, {
      data: () => {
        // Run expensive code and return the result...
      },
    })
  }
}

Syntax

defineIdleProperties(obj, props)

Parameters