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

@sagold/tick

v1.1.0

Published

Timing functions using a single raf-loop

Downloads

3

Readme

tick

old-school performance friendly timing utilities in dom-context

yarn add @sagold/tick or npm i @sagold/tick

Since 2012, i use these little helpers for all performance critical tasks. Until now, they were copied multiple times, into multiple projects. Finally they have their own package. They follow performance best practices which are still true today.

Overview

why and what

bulk updates The main reason for this custom loop is an optimization for browser bulk updates. This means, we try to get as many dom-updates as possible into a single browser-repaint. Thus, the loop will call calculate for all loopable entities to prepare a possible dom-update. The following call to render expects only the remaining dom-assignments that will trigger a browser-repaint.

further performance guidelines (currently unexplained)

  • running everything in one loop
  • dont use css animations in conjuction with js-animations (js only)
  • dont use strict timeouts or intervals
  • prevent object and array creation (reuse, keep in state)

Import

via script

<script type="text/javascript" src="node_modules/@sagold/tick/dist/tick.js"></script>
<script>
    const { loop, Timeout, Debounce } = window.tick;
</script>

via module

import { loop, Timeout, Debounce } from "@sagold/tick";

General usage

All there is, is a function calculate executed within a requestAnimationFrame (raf) and a second function render which behaves exactly like calculate, but is called afterwards.

import { loop, EXIT } from "@sagold/tick";

// the following will add a function and execute it infinitely within a raf
const timer1 = {
    calculate(currentMs) { 
        console.log("tick", currentMs); 
    }
};
loop.add(timer1);

// the following will add a function and execute it once
const timer2 = {
    calculate(currentMs) { 
        console.log("tick", currentMs); 
        return EXIT; // true
    }
};
loop.add(timer2);

// the following will add a function for around 1s
const timer3 = {
    start: Date.now(),
    calculate(currentMs) { 
        console.log("tick", currentMs); 
        if (currentMs - this.start > 1000) {
            return EXIT; // true    
        }
    }
};
loop.add(timer3);

// the following will log "calculate 1", "render 1", "calculate 2", "render 2" infinitely
const timer = {
    start: Date.now(),
    calculate(currentMs) { 
        console.log("calculate", currentMs); 
    },
    render(currentMs) {
        console.log("render", currentMs); 
    }
};
loop.add(timer);

Timeout

import { Timeout } from "@sagold/tick";

const ttl = 1000;
const onTimeout = () => console.log("done");
const timeout = new Timeout(onTimeout, ttl);

// start timeout
timeout.start();

// reset ttl while timeout is running
timeout.keepAlive();

// reuse timeout and start it again
timeout.start();

Debounce

import { Debounce } from "@sagold/tick";

const ttl = 1000/2;
const interval = 1000/20;
const onUpdate = () => console.log("update");
const onEnd = () => console.log("done");
const debounce = new Debounce(onUpdate, onEnd, interval, ttl);

// start debounce
debounce.start(); // logs "update"

// will call "onUpdate" once within each `interval`
debounce.update();
debounce.update();
debounce.update();
debounce.update();

// logs "end", after no more updates have been received within `ttl`