Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Last active February 7, 2021 06:42
Show Gist options
  • Save mr-pascal/02065ae3391e1e2e879a269bda6bf00a to your computer and use it in GitHub Desktop.
Save mr-pascal/02065ae3391e1e2e879a269bda6bf00a to your computer and use it in GitHub Desktop.
const {measureSync, measureAsync} = require('easy-performance-measure');
// 1. Create a synchronous method to measure
/**
* Add values
* @param count
* @return {number}
*/
const syncFn = (count) => {
let l = 0;
for (let i = 0; i < count; i++) {
l += 1;
}
return l;
};
// 2. Create an asynchronous method that calls the synchronous method after a certain timeout
/**
* Async method wrapping 'syncFn' and calls it after some countdown
* @param count
* @return {Promise<unknown>}
*/
const asyncFn = async (count) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(syncFn(count));
}, 250);
});
};
const main = async () => {
// 3. Measure synchronous method via measureSync
console.log(measureSync(syncFn, 500000000));
// Output: [ 500000000, 504 ]
// 4. Measure asynchronous method via measureAsync
console.log(await measureAsync(asyncFn, 500000000));
// Output: [ 500000000, 754 ]
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment