UNPKG

5.37 kBMarkdownView Raw
1[![All dependencies](https://img.shields.io/librariesio/release/npm/elapsing-time/3.2.0?style=flat-square "All dependencies of elapsing-time@3.2.0")](https://libraries.io/npm/elapsing-time/3.2.0)
2[![Reported vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/elapsing-time@3.2.0?style=flat-square "Reported vulnerabilities of elapsing-time@3.2.0")](https://snyk.io/test/npm/elapsing-time/3.2.0)
3[![Commits](https://flat.badgen.net/github/commits/ArthurKa/elapsing-time)](https://github.com/ArthurKa/elapsing-time/commits/master)
4[![NPM-version](https://img.shields.io/badge/npm-v3.2.0-blue.svg?style=flat-square&&logo=npm "Current NPM-version")](https://www.npmjs.com/package/elapsing-time/v/3.2.0)
5[![Total downloads](https://img.shields.io/npm/dt/elapsing-time?style=flat-square "Total downloads for all the time")](https://npm-stat.com/charts.html?package=elapsing-time)
6[![Developed by](https://img.shields.io/badge/developed_by-ArthurKa-blueviolet.svg?style=flat-square "GitHub")](https://github.com/ArthurKa)\
7[![Publish size](https://flat.badgen.net/packagephobia/publish/elapsing-time@3.2.0?label=publish 'Publish size of elapsing-time@3.2.0')](https://packagephobia.now.sh/result?p=elapsing-time@3.2.0)
8[![Install size](https://flat.badgen.net/packagephobia/install/elapsing-time@3.2.0?label=install 'Install size of elapsing-time@3.2.0')](https://packagephobia.now.sh/result?p=elapsing-time@3.2.0)
9[![Minified size](https://img.shields.io/bundlephobia/min/elapsing-time@3.2.0?style=flat-square&label=minified "Minified size of elapsing-time@3.2.0")](https://bundlephobia.com/result?p=elapsing-time@3.2.0)
10[![Minified + gzipped size](https://img.shields.io/bundlephobia/minzip/elapsing-time@3.2.0?style=flat-square&label=minzipped "Minified + gzipped size of elapsing-time@3.2.0")](https://bundlephobia.com/result?p=elapsing-time@3.2.0)
11
12# elapsing-time@3.2.0
13
14Helps you to measure the runtime of your code. Package is available both for **browser** and **Node.js**. Time counts with help of **performance.now** function.
15
16## Installation
17`elapsing-time` is available via NPM:
18```bash
19$ npm i elapsing-time@3.2.0
20```
21
22## Usage
23```ts
24import ElapsingTime from 'elapsing-time';
25
26const timer = new ElapsingTime();
27const wait = (ms: number) => new Promise<void>(res => setTimeout(res, ms));
28
29(async () => {
30 for(let i = 0; i < 10; i++) {
31 timer.start();
32 await wait(200);
33 timer.stop();
34 }
35
36 console.log(timer.ms); // 2002.329
37 console.log(timer.s); // 2.002 // As seconds
38 console.log(timer.us); // 2001809.967 // As microseconds
39 console.log(timer.avg.ms); // 200.233 // Average value
40})();
41```
42
43### Timer.reset
44```ts
45import ElapsingTime from 'elapsing-time';
46
47const timer = new ElapsingTime();
48const wait = (ms: number) => new Promise<void>(res => setTimeout(res, ms));
49
50(async () => {
51 // Total value
52 timer.start();
53 await wait(100);
54 timer.stop();
55 console.log(timer.ms); // 102.937
56
57 timer.start();
58 await wait(200);
59 timer.stop();
60 console.log(timer.ms); // 303.213
61
62 timer.start();
63 await wait(300);
64 timer.stop();
65 console.log(timer.ms); // 603.607
66
67 // Now with reset
68 timer.reset();
69 timer.start();
70 await wait(100);
71 timer.stop();
72 console.log(timer.ms); // 100.715
73
74 timer.reset();
75 timer.start();
76 await wait(200);
77 timer.stop();
78 console.log(timer.ms); // 200.74
79
80 timer.reset();
81 timer.start();
82 await wait(300);
83 timer.stop();
84 console.log(timer.ms); // 300.782
85})();
86```
87
88### Timer.start with autoreset
89```ts
90import ElapsingTime from 'elapsing-time';
91
92const timer = new ElapsingTime();
93const wait = (ms: number) => new Promise<void>(res => setTimeout(res, ms));
94
95(async () => {
96 timer.start();
97 await wait(100);
98 timer.stop(true); // The next timer.start will invoke timer.reset under the hood
99 console.log(timer.ms); // 103.025
100
101 timer.start();
102 await wait(200);
103 timer.stop(true);
104 console.log(timer.ms); // 200.088
105
106 timer.start();
107 await wait(300);
108 timer.stop();
109 console.log(timer.ms); // 300.327
110})();
111```
112
113### Built-in print functions
114```ts
115import ElapsingTime from 'elapsing-time';
116
117const timer = new ElapsingTime();
118const wait = (ms: number) => new Promise<void>(res => setTimeout(res, ms));
119
120(async () => {
121 timer.start();
122 await wait(100);
123
124 timer.msPrint(); // Time: 102.07 ms
125 await wait(10);
126 timer.sPrint(); // Time: 0.115 s
127 // There is no timer.stop so it's still counting
128 await wait(10);
129 timer.usPrint(); // Time: 124822.4 us
130 await wait(10);
131 timer.msPrint('Custom label'); // Custom label: 134.661 ms
132})();
133```
134
135The same way "avg" print functions are also present in timer.avg:
136```ts
137import ElapsingTime from 'elapsing-time';
138
139const timer = new ElapsingTime();
140const wait = (ms: number) => new Promise<void>(res => setTimeout(res, ms));
141
142(async () => {
143 timer.start();
144 await wait(100);
145 timer.stop();
146 timer.avg.sPrint(); // Time: 0.102 s
147
148 timer.start();
149 await wait(900);
150 timer.stop();
151 timer.avg.usPrint(); // Time: 501137.335 us
152
153 timer.start();
154 await wait(200);
155 timer.avg.msPrint('Custom label'); // Custom label: 400.882 ms
156 timer.stop();
157})();
158```
159
160## Testing
161Manually tested by the developer during development. Automated tests are not provided.
162
163---
164
165Your improve suggestions and bug reports [are welcome](https://github.com/ArthurKa/elapsing-time/issues) any time.