Add two simple perf helpers. (#3399)

Can be useful for ad-hoc measure of performance. One is a method
decorator, which can be use on methods like so:

```typescript
  @measureDuration
  someLongRunningProccess() {
  // ....
  }
```

And the other offer more granular control. It also returns what the
callback returns, so it can be use in assignments / return statements.

```typescript
return measureCbDuration('sorting took', () =>  renderingShapes.sort(sortById))
```


### Change Type

<!--  Please select a 'Scope' label ️ -->

- [ ] `sdk` — Changes the tldraw SDK
- [ ] `dotcom` — Changes the tldraw.com web app
- [ ] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [x] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [ ] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [ ] `improvement` — Improving existing features
- [ ] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [x] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know
This commit is contained in:
Mitja Bezenšek 2024-04-08 15:41:09 +02:00 committed by GitHub
parent fb2d3b4372
commit 5347c5f30e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 0 deletions

View file

@ -176,6 +176,12 @@ export function mapObjectMapValues<Key extends string, ValueBefore, ValueAfter>(
[K in Key]: ValueAfter; [K in Key]: ValueAfter;
}; };
// @internal (undocumented)
export function measureCbDuration(name: string, cb: () => any): any;
// @internal (undocumented)
export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
// @public // @public
export class MediaHelpers { export class MediaHelpers {
static getImageSize(blob: Blob): Promise<{ static getImageSize(blob: Blob): Promise<{

View file

@ -36,6 +36,7 @@ export {
objectMapKeys, objectMapKeys,
objectMapValues, objectMapValues,
} from './lib/object' } from './lib/object'
export { measureCbDuration, measureDuration } from './lib/perf'
export { PngHelpers } from './lib/png' export { PngHelpers } from './lib/png'
export { type IndexKey } from './lib/reordering/IndexKey' export { type IndexKey } from './lib/reordering/IndexKey'
export { export {

View file

@ -0,0 +1,22 @@
/** @internal */
export function measureCbDuration(name: string, cb: () => any) {
const now = performance.now()
const result = cb()
// eslint-disable-next-line no-console
console.log(`${name} took`, performance.now() - now, 'ms')
return result
}
/** @internal */
export function measureDuration(_target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value
descriptor.value = function (...args: any[]) {
const start = performance.now()
const result = originalMethod.apply(this, args)
const end = performance.now()
// eslint-disable-next-line no-console
console.log(`${propertyKey} took ${end - start}ms `)
return result
}
return descriptor
}