├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type PromiseWithTime = { 2 | /** 3 | The elapsed time in milliseconds. 4 | */ 5 | readonly time?: number; 6 | } & Promise; 7 | 8 | declare const pTime: { 9 | /** 10 | Measure the time a promise takes to resolve. 11 | 12 | @param asyncFunction - Promise-returning/async function. 13 | @returns A decorated version of `asyncFunction`. 14 | 15 | @example 16 | ``` 17 | import pTime from 'p-time'; 18 | import {execa} from 'execa'; 19 | 20 | const promise = pTime(execa)('sleep', ['1']); 21 | 22 | await promise; 23 | console.log(promise.time); 24 | //=> 1016 25 | ``` 26 | */ 27 | ( 28 | asyncFunction: (...arguments: ArgumentsType) => PromiseLike 29 | ): (...arguments: ArgumentsType) => PromiseWithTime; 30 | 31 | /** 32 | Measure the time a promise takes to resolve. Logs the elapsed time. 33 | 34 | @param asyncFunction - Promise-returning/async function. 35 | @returns A decorated version of `asyncFunction`. 36 | */ 37 | log( 38 | asyncFunction: (...arguments: ArgumentsType) => PromiseLike 39 | ): (...arguments: ArgumentsType) => PromiseWithTime; 40 | }; 41 | 42 | export default pTime; 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import mimicFunction from 'mimic-function'; 2 | 3 | export default function pTime(asyncFunction) { 4 | const wrappedFunction = (...arguments_) => { 5 | const start = Date.now(); 6 | 7 | const returnPromise = (async () => { 8 | try { 9 | return await asyncFunction(...arguments_); 10 | } finally { 11 | returnPromise.time = Date.now() - start; 12 | } 13 | })(); 14 | 15 | return returnPromise; 16 | }; 17 | 18 | mimicFunction(wrappedFunction, asyncFunction); 19 | 20 | return wrappedFunction; 21 | } 22 | 23 | const log = (function_, promise) => { 24 | console.log(`Promise from ${function_.displayName || function_.name || '[anonymous]'} resolved in ${promise.time} ms`); 25 | }; 26 | 27 | pTime.log = asyncFunction => { 28 | const wrappedFunction = pTime(asyncFunction); 29 | 30 | return (...arguments_) => { 31 | const promise = wrappedFunction(...arguments_); 32 | 33 | (async () => { 34 | try { 35 | await promise; 36 | } finally { 37 | log(wrappedFunction, promise); 38 | } 39 | })(); 40 | 41 | return promise; 42 | }; 43 | }; 44 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import pTime, {type PromiseWithTime} from './index.js'; 3 | 4 | expectType<(input: number) => PromiseWithTime>( 5 | pTime(async (input: number) => input), 6 | ); 7 | expectType(pTime(async (input: number) => input)(1).time); 8 | 9 | expectType<(input: number) => PromiseWithTime>( 10 | pTime.log(async (input: number) => input), 11 | ); 12 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-time", 3 | "version": "4.0.0", 4 | "description": "Measure the time a promise takes to resolve", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-time", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "promise", 31 | "time", 32 | "timed", 33 | "timing", 34 | "timer", 35 | "elapsed", 36 | "measure", 37 | "bench", 38 | "resolve", 39 | "execute" 40 | ], 41 | "dependencies": { 42 | "mimic-function": "^5.0.0" 43 | }, 44 | "devDependencies": { 45 | "ava": "^5.3.1", 46 | "delay": "^6.0.0", 47 | "hook-std": "^3.0.0", 48 | "in-range": "^3.0.0", 49 | "tsd": "^0.29.0", 50 | "xo": "^0.56.0" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-time 2 | 3 | > Measure the time a promise takes to resolve 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install p-time 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import pTime from 'p-time'; 15 | import {execa} from 'execa'; 16 | 17 | const promise = pTime(execa)('sleep', ['1']); 18 | 19 | await promise; 20 | console.log(promise.time); 21 | //=> 1016 22 | ``` 23 | 24 | ## API 25 | 26 | ### pTime(asyncFunction) 27 | 28 | Returns a decorated version of `asyncFunction` that when called returns a `Promise` with a `time` property of the elapsed time in milliseconds. 29 | 30 | ### pTime.log(asyncFunction) 31 | 32 | Returns a decorated version of `asyncFunction` that when called logs the elapsed time in milliseconds of the `Promise`. 33 | 34 | #### asyncFunction 35 | 36 | Type: `Function` 37 | 38 | Promise-returning/async function. 39 | 40 | ## Related 41 | 42 | - [p-log](https://github.com/sindresorhus/p-log) - Log the value/error of a promise 43 | - [More…](https://github.com/sindresorhus/promise-fun) 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'delay'; 3 | import inRange from 'in-range'; 4 | import {hookStdout} from 'hook-std'; 5 | import pTime from './index.js'; 6 | 7 | test('then', async t => { 8 | const timedDelay = pTime(delay); 9 | const promise = timedDelay(200); 10 | await promise; 11 | t.true(inRange(promise.time, {start: 180, end: 250})); 12 | }); 13 | 14 | test('catch', async t => { 15 | const timedDelay = pTime(delay); 16 | const promise = timedDelay(200); 17 | 18 | try { 19 | await promise.then(() => { 20 | throw new Error('fixture'); 21 | }); 22 | } catch {} 23 | 24 | t.true(inRange(promise.time, {start: 180, end: 250})); 25 | }); 26 | 27 | test.serial('log', async t => { 28 | const myDelay = ms => new Promise(resolve => { 29 | setTimeout(resolve, ms); 30 | }); 31 | 32 | const promise = hookStdout({silent: true}, (output, unhook) => { 33 | unhook(); 34 | t.regex(output, /Promise from myDelay resolved in \d+ ms/); 35 | }); 36 | 37 | pTime.log(myDelay)(200); 38 | 39 | await promise; 40 | }); 41 | 42 | test.serial('log aynonymous function', async t => { 43 | const promise = hookStdout({silent: true}, (output, unhook) => { 44 | unhook(); 45 | t.regex(output, /Promise from \[anonymous] resolved in \d+ ms/); 46 | t.end(); 47 | }); 48 | 49 | pTime.log(delay)(200); 50 | 51 | await promise; 52 | }); 53 | --------------------------------------------------------------------------------