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

@uu-cdh/timing-util

v0.1.1

Published

Async timing utilities for JavaScript

Downloads

11

Readme

@uu-cdh/timing-util

Timing utilities for JavaScript.

Rationale | Quickstart | Compatibility | Reference | Planned | Support | Development | BSD 3-Clause

Rationale

The functions in this package offer new ways to postpone the execution of a callback function to some later point in time. Specifically:

  • fastTimeout is similar to the built-in setTimeout in the sense that it lets you postpone a function call until a later cycle of the event loop. However, callbacks scheduled with setTimeout will be throttled by the browser if they occur too often. fastTimeout uses postMessage under the hood in order to avoid this throttling effect. The end result is that you can make many deferred callbacks in performance-critical code without causing obvious delays for the user.
  • Other functions are planned.

Quickstart

If you have a workflow in which you install dependencies from npm and possibly bundle them with some kind of tool:

// could also use yarn, pnpm, etcetera
npm add @uu-cdh/timing-util
// could also use CommonJS or AMD syntax if necessary
import fastTimeout from '@uu-cdh/timing-util/fastTimeout.js';

fastTimeout(someFunction, ...args);

Alternatively, if you are directly using the library as a browser embed:

<script
    src="https://cdn.jsdelivr.net/npm/@uu-cdh/[email protected]/umd/fastTimeout.js"
></script>
<script>
    fastTimeout(someFunction, ...args);
</script>

Compatibility

While fastTimeout is based on postMessage, it automatically falls back to setImmediate in environments where that function is defined. Hence, you can transparently use it in Node.js as well.

Reference

Module fastTimeout.js

Function fastTimeout (default export)

fastTimeout(callback, ...args) will schedule the function callback for a later cycle of the event loop. If you pass any args, these are passed to callback when it runs, in effect deferring the call callback(...args). This is almost equivalent to setTimeout(callback, 0, ...args), except that calls to setTimeout are throttled by the browser while calls to fastTimeout are not. In general, this saves several milliseconds per repetition.

While fastTimeout is not throttled, there is still no guarantee that it will run within any particular time frame. If other events happen in the meantime which trigger long-running code, there can be an indefinite amount of delay between your call to fastTimeout and the actual callback invocation. In this sense, fastTimeout is no faster than setTimeout.

Planned features

  • softDebounce: pool all calls to a function in the current event loop cycle, then execute it once at the end of the same cycle as a microtask.
  • throttlePerFrame: pool calls to a function and execute it only once per animation frame.
  • debounceUntilIdle: pool calls to a function. Wait with executing the function until the browser is idle.
  • debounceUntilTrigger: common underlying implementation for the previous three functions. The function is only executed with the pooled arguments when a custom event triggers.

Support

Please report security issues to [email protected]. For regular bugs, feature requests or any other feedback, please use our issue tracker.

Development

Please see the CONTRIBUTING for everything related to testing, pull requests, versioning, etcetera.

License

Licensed under the terms of the three-clause BSD license. See LICENSE.