Perf: slightly faster getShapeAtPoint
(#3416)
This PR makes a small improvement to the speed of `getShapeAtPoint`. It removes `Editor.getCurrentPageRenderingShapesSorted`. ### Change Type - [x] `sdk` — Changes the tldraw SDK - [x] `improvement` — Improving existing features
This commit is contained in:
parent
5347c5f30e
commit
3f64bf8c5b
4 changed files with 35 additions and 6 deletions
|
@ -20,3 +20,31 @@ export function measureDuration(_target: any, propertyKey: string, descriptor: P
|
|||
}
|
||||
return descriptor
|
||||
}
|
||||
|
||||
const averages = new Map<any, { total: number; count: number }>()
|
||||
|
||||
/** @internal */
|
||||
export function measureAverageDuration(
|
||||
_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()
|
||||
const value = averages.get(descriptor.value)!
|
||||
const length = end - start
|
||||
const total = value.total + length
|
||||
const count = value.count + 1
|
||||
averages.set(descriptor.value, { total, count })
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(
|
||||
`${propertyKey} took ${(end - start).toFixed(2)}ms | average ${(total / count).toFixed(2)}ms`
|
||||
)
|
||||
return result
|
||||
}
|
||||
averages.set(descriptor.value, { total: 0, count: 0 })
|
||||
return descriptor
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue