ixfx
    Preparing search index...

    Class RateTracker

    Tracks the rate of events: eg, how many times an event happens per second.

    It's also able to compute the min, max and average interval between events.

    // Initialise it once
    const clicks = Trackers.rate(); // or: const clicks = new Trackers.RateTracker();

    // Mark when a click happens
    document.addEventListener(`click`, () => clicks.mark());

    // Compute rate (returned as a number)
    clicks.perSecond; // How many clicks per second
    clicks.perMinute; // How many clicks per minute

    By default the tracker keeps averaging over all the data it sees. It can be useful to limit it so more accurately tracks changes in frequency.

    To do so, pass in options when creating it:

    // Eg reset tracker after 5 seconds of inactivity,
    // and only keep up to 10 samples max.
    const clicks = Trackers.rate({
    sampleLimit: 10,
    timeoutInterval: { secs: 5 }
    });
    • timeoutInterval is a makes the tracker reset after some period without mark() being called.
    • sampleLimit sets an upper bound for how many of the most recent events should we consider. A smaller value means the analysis will be more responsive to changes in data source, at the cost of jitter. A larger value gets a smoother response, at the cost of responsiveness.

    The way it works under-the-hood is we store the timestamp each time mark() is called, and use this as the basis for computation.

    Index

    Accessors

    • get elapsed(): number

      Returns the time period (in milliseconds) that encompasses the data set. Eg, a result of 1000 means there's data that covers a one second period.

      Returns number

    Constructors

    Methods

    • Computes the average time between events, as well as the longest and shortest time seen.

      Returns { avg: number; max: number; min: number }

      Analysis in the form of { min, max, avg }