1.0.0 • Published 5 months ago

@mrozio/exectime v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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 💖

1.0.0

5 months ago