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 🙏

© 2026 – Pkg Stats / Ryan Hefner

p-cost

v1.0.2

Published

Measure the time a promise take to resolve

Readme

p-cost

Build Status

How long will a Promise to be resolved(rejected) ?

When I used nodejs for backend development,I always create many promises to interact between multiple systems, such as database, or some other systems. These systems have different response times,in other word, these promises consume different lengths of time, some promises will take long time until it resolved, and some promises would only take very short time. I need a tool to count the length of each created promise cost。

So I extend the ES6 Promise to resolve this problem.

Examples

basic usage

new Promise(resolve => {
  setTimeout(resolve, 1000);
});
//output: anonymous ==> 1004ms at /p-cost/examples/stdout.example.js:6

custom name

new Promise(resolve => {
  setTimeout(resolve, 1000);
}, {name: "p-cost"});
//output: p-cost ==> 1003ms at /p-cost/examples/stdout.example.js:11

sum the time cost in same promise chain

new Promise(resolve => {
  setTimeout(resolve, 1000);
}, {
  name: "cost",
  sum: true    // sum the time cost in the promise chain
}).then(() => { // cost-#1
  // do something here
}).then(() => { // cost-#2
  // do something here
});
// output:
//         cost ==> 1009ms /p-cost/examples/name.example.js:15
//         cost-#1 ==> 1011ms /p-cost/examples/name.example.js:20
//         cost-#2 ==> 1010ms /p-cost/examples/name.example.js:22

custom notifier

new Promise(resolve => {
  setTimeout(resolve, 1000);
}, {
  name: "bunyan1000",
  notifier: {
    notify: bunyanNotifier,
    notifyOpt: {name: "overtime promise"}
  }
});

// output
// {
//   "name": "bunyan1000",
//   "hostname": "qeesungdeMacBook-Pro.local",
//   "pid": 18100,
//   "level": 30,
//   "cost": "1004ms",
//   "location": {
//     "file": "/p-cost/examples/bunyan.example.js",
//     "line": "7"
//   },
//   "msg": "",
//   "time": "2017-06-03T16:05:35.527Z",
//   "v": 0
// }

more examples can be found in example directory

Usage

new Promise((resolve, reject) => {}, options);

options

{
  name: "hello",
  timeout: 500,
  sum: true,
  notifer: {
    notify: () => {}
    notifyOpt: {}
  }
}

| name |type| required | default |description | |:-:|:-:|:-:|:-:| :-- | | timeout |boolean| false | 1000 | if promise resolved time exceed timeoutms, notify function will be called | | name | string | false | "anonymous" | promise name | | sum | boolean | false | false | if sum the time cost in same promise chain | | notifier.notify | function | false | stdoutNotifier | the way to notify user if timeout | | notifier.notifyOpt | object | false | null | will pass to notifier.notify function as argument |

options.timeout

Not all promises will notify the user the time it spent, just the promise that exceed the timeout, You can set the timeout to control the threshold of the reminder.

// default timeout is 1000ms
new Promise((resolve) => {
  setTimeout(resolve, 1000);
});
//output: anonymous ==> 1004ms at /p-cost/examples/stdout.example.js:6
// custom timeout
new Promise((resolve) => {
  setTimeout(resolve, 500);
}, {name: "stdout-500", timeout: 300});
//output: stdout-500 ==> 506ms at /p-cost/examples/stdout.example.js:16

options.name

Give promise a custom name,you can make it easier to identify those notify message.

// default promise name
new Promise((resolve) => {
  setTimeout(resolve, 1000);
});
//output: anonymous ==> 1004ms at /p-cost/examples/stdout.example.js:6
// custom promise name
new Promise(resolve => {
  setTimeout(resolve, 1000);
}, {
  name: "cost"
});
// output: cost ==> 1005ms at /p-cost/examples/name.example.js:8

// auto increment suffix serial number
new Promise(resolve => {
  setTimeout(resolve, 1000);
}, {
  name: "cost",
  sum: true    // sum the time cost in the promise chain
}).then(() => { // cost-#1
  // do something here
}).then(() => { // cost-#2
  // do something here
});
// output:
//         cost ==> 1009ms /p-cost/examples/name.example.js:15
//         cost-#1 ==> 1011ms /p-cost/examples/name.example.js:20
//         cost-#2 ==> 1010ms /p-cost/examples/name.example.js:22

options.sum

Note: because every promise only have three status: pending, fullfiled, rejected, so the then function will return a new Promise, and this new Promise will be resolved after previous promises were fullfilled, so the new Promise spent time is the sum of previous promises spent time and it own spent.

If the sum is true, the prmise in the same promise chain spent time is the sum of previous promises spent time and it own spent. If the sum is false, the promise in the same promise chain spent time is only the time it own spent.

// sum the time cost
new Promise(resolve => {
  setTimeout(resolve, 1000);
},{
  sum: true
}).then(() => {
  // do something here
}).then(() => {
  // do something here
});
// output:
//         anonymous ==> 1006ms at /p-cost/examples/sum.example.js:9
//         anonymous-#1 ==> 1010ms at /p-cost/examples/sum.example.js:11
//         anonymous-#2 ==> 1010ms at /p-cost/examples/sum.example.js:13
// do not sum the time cost
new Promise(resolve => {
  setTimeout(resolve, 1000);
},{
  sum: false
}).then(() => {
  // do something here
}).then(() => {
  // do something here
});
// output: anonymous ==> 1008ms at /p-cost/examples/sum.example.js:24

options.notifier

When the time the promise spent exceed timeout, the notifier.notify function will be called to notify the user.

provided notifer

how to custom a notifier

notifier is just a normal object, and the notifier.notify is a normal function. and notify function will be called with:

  • name, promise name
  • cost, the promise tim cost
  • caller, call stack, promise location
    • caller.file the file define the promise
    • caller.line the line define the promise
  • options, notifyOptions
let customNotify = (name, cost, caller, options) => {
  
}