@mrozio/exectime
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

exectime npm

Measures the time it takes to execute a function.

npm i @mrozio/exectime

For both synchronous and asynchronous functions:

import { execTime } from '@mrozio/exectime';

await execTime(async () => {
    // wait 1 second
    await new Promise(r => setTimeout(r, 1000));
}); // => 1002.227 (ms)

Specifically for synchronous functions:

import { execTimeSync } from '@mrozio/exectime';

execTimeSync(() => {
    // count to 1 billion
    for (let i = 0; i < 1e9;) i++;
}); // => 388.2297 (ms)

Note: These functions always return the execution time in milliseconds, even if the underlying measurements are in nanoseconds. This is done for consistency across different environments.

In Node.js environments, it uses process.hrtime for precise measurements, while in the browser, it utilizes performance.now. If you want the results to be in nanoseconds, you can simply multiply them by 1 million.

License

MIT 💖

Readme

Keywords

none

Package Sidebar

Install

npm i @mrozio/exectime

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

6.15 kB

Total Files

8

Last publish

Collaborators

  • mrozio