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

supertiming

v2.0.1

Published

function timing

Downloads

20

Readme

supertiming

Get the function timing log

Build Status Coverage Status npm Github Releases

Installation

$ npm install supertiming

API

constructor

  • options.startIndex The start index for server timing, default is 'A'
  • options.precision The precision for time, ms or ns, default is 'ms'
const Timing = require('supertiming');
const timing = new Timing({
  startIndex: '0',
  precision: 'ns',
})

start

Set the starting point of timing function, if there is any function is not finished, it will be the child of those functions

  • name The function name of timing
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
const endGetUser = timing.start('GetUserInfo');
delay(10).then(() => {
  timing.start('FindOneById:User');
}).then(() => {
  endGetUser();
});

end

Set the ending point of timing function

  • name The function name to timing, if the name is * or null, end all doing timing
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.start('GetUserInfo');
delay(10).then(() => {
  timing.start('FindOneById:User');
  return delay(40);
}).then(() => {
  timing.end();
});

addMetric

Add metric to timing

  • name The function name of timing

  • use Cost time of function

const Timing = require('supertiming');
const timing = new Timing();
timing.addMetric('Get-Session', 35);
const data = timing.toJSON();
// [ { name: 'Get-Session', use: 35 } ]
console.info(data);

remove

Remove the function from timing

  • name The function name of timing
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.start('GetUserInfo');
delay(10).then(() => {
  timing.start('FindOneById:User');
  return delay(40);
}).then(() => {
  timing.remove('FindOneById:User');
  // only timing `GetUserInfo`
  timing.end();
});

rename

Change the function name of timing

  • name The original function name

  • newName The new function name

const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.start('GetUserInfo');
delay(10).then(() => {
  timing.start('FindOneById:User');
  return delay(40);
}).then(() => {
  timing.rename('FindOneById:User', 'FindOneByIdNew:User');
  timing.end();
});

toJSON

Get tming json format

  • ignoreChildren ignore the children, default is false
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.start('/users/me');
const endGetUser = timing.start('getUser');
timing.start('mongodb:get')
delay(30)
  .then(() => {
    timing.end('mongodb:get');
    timing.start('validate:user');
    return delay(50);
  })
  .then(() => {
    timing.end('validate:user');
    return delay(10);
  })
  .then(() => {
    endGetUser();
    timing.end('/users/me');
    const data = timing.toJSON();
    //[ { name: '/users/me',
    //    startedAt: 1486736323078,
    //    use: 104,
    //    children: [ 'getUser', 'mongodb:get', 'validate:user' ] },
    //  { name: 'getUser',
    //    startedAt: 1486736323078,
    //    use: 104,
    //    children: [ 'mongodb:get', 'validate:user' ] },
    //  { name: 'mongodb:get', startedAt: 1486736323078, use: 37 },
    //  { name: 'validate:user', startedAt: 1486736323116, use: 53 } ]
    console.info(data);
  }).catch(console.error);

toString

  • ignoreChildren ignore the children, default is false
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing({
  precision: 'ns',
});
timing.start('/users/me');
const endGetUser = timing.start('getUser');
timing.start('mongodb:get')
delay(30)
  .then(() => {
    timing.end('mongodb:get');
    timing.start('validate:user');
    return delay(50);
  })
  .then(() => {
    timing.end('validate:user');
    return delay(10);
  })
  .then(() => {
    endGetUser();
    timing.end('/users/me');
    const data = timing.toString();
    // /users/me[getUser,mongodb:get,validate:user][99915666ns] getUser[mongodb:get,validate:user][99834611ns] mongodb:get[32904455ns] validate:user[53700273ns]
    console.info(data);
  }).catch(console.error);

toServerTiming

Get server timing for http response

  • ignoreChildren ignore the children, default is false
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.start('/users/me');
timing.start('getUser');
timing.start('mongodb:get')
delay(30)
  .then(() => {
    timing.end('mongodb:get');
    timing.start('validate:user');
    return delay(50);
  })
  .then(() => {
    timing.end('validate:user');
    return delay(10);
  })
  .then(() => {
    timing.end('getUser');
    timing.end('/users/me');
    const data = timing.toServerTiming();
    // A=0.097;"/users/me(1 2 3)",B=0.096;"getUser(2 3)",C=0.03;"mongodb:get",D=0.054;"validate:user"
    console.info(data);
  }).catch(console.error);

setServerTimingStartIndex

Set the server timing start index, default is A

  • ch The start index char
const Timing = require('supertiming');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const timing = new Timing();
timing.setStartIndex('a');
timing.start('/users/me');
timing.start('getUser');
timing.start('mongodb:get')
delay(30)
  .then(() => {
    timing.end('mongodb:get');
    timing.start('validate:user');
    return delay(50);
  })
  .then(() => {
    timing.end('validate:user');
    return delay(10);
  })
  .then(() => {
    timing.end('getUser');
    timing.end('/users/me');
    const data = timing.toServerTiming();
    // a=0.097;"/users/me(1 2 3)",b=0.096;"getUser(2 3)",c=0.03;"mongodb:get",d=0.054;"validate:user"
    console.info(data);
  }).catch(console.error);

Examples

Set Server-Timing useing Koa2

const Timing = require('supertiming');
const Koa = require('koa');
const app = new Koa();

function getUser() {
  ...
}

function mongodbGet() {
  ...
}

function validateUser() {
  ...
}

app.use((ctx, next) => {
  const timing = new Timing();
  timing.start('Total');
  ctx.state.timing = timing;
  return next().then(() => {
    tming.end('*');
    ctx.set('Server-Timing', timing.toServerTiming());
  });
});

app.use((ctx) => {
  if (ctx.url === '/user/me') {
    const timing = ctx.state.timing;
    timing.start('/user/me');
    timing.start('getUser');
    return getUser()
      .then(() => {
        timing.start('mongodb:get');
        return mongodbGet();
      })
      .then(() => {
        timing.end('mongodb:get');
        timing.start('validate:user');
        return validateUser();
      })
      .then(() => {
        timing.end('validate:user');
        ctx.body = {
          account: 'vicanso',
        };
        timing.end('/user/me');
      });
  }
});

app.listen();

License

MIT