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

duration-interval-clock

v1.0.5

Published

A small library to measure duration and interval, including averaging.

Downloads

137

Readme

duration-interval-clock github, npm

A small library to measure the duration and interval of execution, including averaging.

Measures time between start() and end() to provide duration, and between end() and the subsequent end() to provide interval, both with built-in window-averaging.

Usage

Simple usage:

import { DurationIntervalClock } from 'duration-interval-clock';

// defaults to average over 10 last measurements
const dic = new DurationIntervalClock();

dic.start();
// perform some task
dic.end();

// number of milliseconds taken by the task
dic.lastDuration

// average, after one measurement will be equal to lastDuration
dic.averageDuration

// both will be undefined, start() needs to be called at least twice
dic.lastInterval, dic.averageInterval

Example:

import { DurationIntervalClock } from 'duration-interval-clock';

// average over last 5 measurements
const dic = new DurationIntervalClock(5);

for(let i = 0; i <= 20; i++) {
    const ret = await dic.measureAsync(async () => {
        // some async task
        await new Promise(r => setTimeout(r, 100 + i * 10));
        return "ret value";
    });

    new Promise(r => setTimeout(r, 50));

    if(i % 5 === 0) {
        console.log("\nIteration", i)
        console.log("  lastDuration:", dic.lastDuration);
        console.log("  averageDuration:", dic.averageDuration);
        console.log("  lastInterval:", dic.lastInterval);
        console.log("  averageInterval:", dic.averageInterval);
    }
}

Types:

class DurationIntervalClock {
    sampleTargetCount: number;
    durationsSamples: number[];
    intervalSamples: number[];

    constructor(sampleTargetCount?: number = 10);
    
    reset(): void;
    cancel(ignoreIfNotStarted?: boolean = false): void; // cancel last start()
    
    start(endIfAlreadyStarted?: boolean = false): void;
    // itemsProcessed may be a positive number which will divide measured values
    end(ignoreIfNotStarted?: boolean = false, itemsProcessed?: number): void;
    
    measureAsync<T>(fn: () => Promise<T>): Promise<T>;
    measureSync<T>(fn: () => T): T;
    
    checkHasGoodAverage(q?: number = 0.5): boolean;
    get isStarted(): boolean;
    get lastDuration(): number | undefined;
    get lastInterval(): number | undefined;
    get averageDuration(): number | undefined;
    get averageInterval(): number | undefined;
}

FAQ

1. What is interval and duration?

Last interval measures end()-to-end(), last duration measures subsequent start()-to-end().