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

time-me

v1.2.0

Published

Time a block of code with minimal effort.

Downloads

9

Readme

timeMe!

Time a block of code with minimal effort.

Sometimes you want to time a certain execution path to see its performance. So, you add a bunch of time captures and log statements. Cleaning them up isn't so much fun. It would also be nice to leave them in your code so they can be enabled at will. timeMe is meant to help in that situation.

timeMe Will take some options and a function to wrap. It will return the wrapped function. Everytime that function is executed, its time will be recorded. You can choose to print it out, or query the function for its lastTime.

In the case of Promise based functions, timeMe will wait until the Promise resolves, log the time it took to resolve, and then pass the promise back.

Usage

timeMe.configure({
  log: function(msg, msgObj) { console.log(msg); }
});

var foo = timeMe.async('foo()', function(cb) {
  setTimeout(function() {
      cb(null, '25');
  }, 2000);
});
foo(function(err, result) {
  console.log(result);
});

// outputs:
// foo() 2000ms
// 25


// using with Promises
var foo = timeMe.promise('foo()', function() {
  return new Promise(function(res, rej) {
    setTimeout(function() {
      res('26');
    }, 1000);
  });
});
var p = foo();
p.then(function(result) {
  console.log(result);
})

// outputs:
// foo() 1000ms
// 26

Methods

timeMe.configure

Globally configure the module on what log function to use. Defaults to console.log

timeMe.configure({
  log: function(msg, obj) { // do logging here }
});

The configured logging function will be passed two arguments:

  1. A string containing the message used to name the call, and the call time.

  2. An object containing the same information, of the form

    {
        prefix: string // prefix message
        elapsed: number // elapsed time in milliseconds
    }

timeMe.async

timeMe.sync

timeMe.promise

All three methods can be called like so

timeMe.async('a prefex', fn);
timeMe.async(fn); // default prefix of `timeMe`
timeMe.async({msg: 'foo()', index: 1}, fn); // options hash for more fine grained control
timeMe.promise(promiseBasedFn);

Options

options.msg

The prefix of the logged time

options.index

(only for timeMe.async) You can specify the index of the callback in the wrapped function. Defaults to the last argument.

options.noLog

Turn logs on or off. Defaults to false (meaning logging on).

wrappedFn.lastTime

Whenever a wrapped function is called, additionally to logging the time, a property will be set on the function: lastTime. You can query the function for the property.