Skip to content

Commit

Permalink
Add async measure function
Browse files Browse the repository at this point in the history
  • Loading branch information
ruowan committed Jun 23, 2020
1 parent 17e0f22 commit 93bab88
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Test {
}
```

`measure` - measure time taken by a function to execute
`measure` - measure time taken by a sync function to execute
```js
import { measure } from 'helpful-decorators';

Expand All @@ -75,6 +75,20 @@ class Test {
}
```

`measureAsync` - measure time taken by an async function to execute

```js
import { measureAsync } from 'helpful-decorators';

class Test {
@measureAsync
async doSomething() {
// Call to doSomething took 0.35 milliseconds.
}
}
```


`Mixin` - this pattern is used to achieve multiple inheritance
```js
import { Mixin } from 'helpful-decorators';
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helpful-decorators",
"version": "2.1.0",
"version": "2.2.0",
"description": "Helpful decorators for typescript projects",
"jest": {
"moduleFileExtensions": [
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { measure } from './measure';
export { measure, measureAsync } from './measure';
export { delay } from './delay';
export { debounce } from './debounce';
export { throttle } from './throttle';
export { once } from './once';
export { Mixin } from './mixin';
export { memo } from './memoize';
export { bind } from './bind';
export { SortBy } from './sortby';
export { SortBy } from './sortby';
12 changes: 12 additions & 0 deletions src/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export function measure(target: any, propertyKey: string, descriptor: PropertyDe

return descriptor;
}

export function measureAsync(target: any, propertyKey: string, descriptor: PropertyDescriptor): any {
const originalMethod = descriptor.value;
descriptor.value = async function(...args: any): Promise<any> {
const start = performance.now();
const result = await originalMethod.apply(this, args);
const end = performance.now();
console.log(`Call to ${propertyKey} took ${(end-start).toFixed(2)} milliseconds.`);
return result;
};
return descriptor;
};

0 comments on commit 93bab88

Please sign in to comment.