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

exectimer

v2.2.2

Published

Very simple module to calculate block execution time.

Downloads

39,649

Readme

Description

Greenkeeper badge Build Status

Very simple but powerful nodejs and browser module build to track execution time with a resolution of nanoseconds.

Install

Simply run:

npm install exectimer

Usage

Example

Wrapping generator

const t = require('exectimer');
const Tick = t.Tick;

const promises = [];
for(var i = 0; i < 10; i++) {
    const functionExecution = Tick.wrap(function* myFunction() {
        yield Promise.resolve(true);
    });

    promises.push(functionExecution);
}

// After all ticks are finished
Promise.all(promises).then(() => {
    // display the results
    var results = t.timers.myFunction;
    console.log(results.parse(results.duration())); // total duration of all ticks
    console.log(results.parse(results.min()));      // minimal tick duration
    console.log(results.parse(results.max()));      // maximal tick duration
    console.log(results.parse(results.mean()));     // mean tick duration
    console.log(results.parse(results.median()));   // median tick duration
});

Wrapping

const t = require('exectimer');
const Tick = t.Tick;


for(var i = 0; i < 10; i++) {
    Tick.wrap(function myFunction(done) {
        setTimeout(function() {
            done();
        }, 10);
    });
}

// Display the results
var results = t.timers.myFunction;
setTimeout(() => {
    console.log(results.parse(
        results.duration()
    )); // total duration of all ticks
    console.log(results.parse(
        results.min()
    ));      // minimal tick duration
    console.log(results.parse(
        results.max()
    ));      // maximal tick duration
    console.log(results.parse(
        results.mean()
    ));     // mean tick duration
    console.log(results.parse(
        results.median()
    ));   // median tick duration
}, 101);

Instantiating the ticks yourself

const t = require('exectimer');
const Tick = t.Tick;

for(var i = 0; i < 10; i++) {
    // unique contexts to avoid aliasing (#9)
    (function () {
        var tick = new Tick("myFunction");
        tick.start();

        setTimeout(function() {
            tick.stop();
        }, Math.random() * 10);
    })();
}

// Display the results
var results = t.timers.myFunction;
setTimeout(() => {
    console.log(results.parse(
        results.duration()
    )); // total duration of all ticks
    console.log(results.parse(
        results.min()
    ));      // minimal tick duration
    console.log(results.parse(
        results.max()
    ));      // maximal tick duration
    console.log(results.parse(
        results.mean()
    ));     // mean tick duration
    console.log(results.parse(
        results.median()
    ));   // median tick duration
}, 101);

API

Tick

A tick is used to measure the difference between two execution points. All ticks are than used to calculate the average, median, min, max etc. Takes the name of the timer as an argument.

Tick.wrap()

Arguments

  1. [name] (String) Name of the function
  2. callback (Function) Function for which to calculate the duration
  • callback Can be a function or a generator
    1. [done] (Function) Should be passed if function is asynchronous

Static function that takes a name and a function as arguments. If the name is omitted than it tries to read the name of the function or it just uses "anon". If done function is not requested than it presumes that the call is synchronous. It also accepts a generator function in which case done function is not necessary.

Tick.prototype.start()

Starts the timer of this tick.

Tick.prototype.stop()

Stops the timer of this tick.

###Timers Array of timers. Each timer has methods to calculate the various metrics. When a tick is created, it is pushed into the timer with name that was passed to the ticker in the constructor.

var tick = new t.Tick("TIMER");

tick.start();
// Do some processing
tick.stop();

var myTimer = t.timers.TIMER;

console.log("It took: " + myTimer.duration());

You can name your timer however you want.

Timers.TIMER.min()

Get the shortest tick.

Timers.TIMER.max()

Get the longest tick.

Timers.TIMER.mean()

Get the average tick.

Timers.TIMER.median()

Get the median tick.

Timers.TIMER.duration()

Get the total duration of all ticks.

Timers.TIMER.count()

Get the number of ticks.

Timers.TIMER.parse()

Parse the output of the previous methods from nanoseconds to us, ms, ns or seconds.