Add utils, time import more granularly

This commit is contained in:
Jamie Wong 2018-01-16 22:27:31 -08:00
parent 98fe1d762e
commit d5c1d8439a
3 changed files with 39 additions and 2 deletions

View File

@ -159,9 +159,9 @@ export class Application extends ReloadableComponent<{}, ApplicationState> {
})
console.timeEnd('import')
console.time('first render')
console.time('first setState')
this.setState({ profile, flamechart, sortedFlamechart }, () => {
console.timeEnd('first render')
console.timeEnd('first setState')
})
}

View File

@ -99,6 +99,7 @@ export class FlamechartPanZoomView extends ReloadableComponent<FlamechartPanZoom
private preprocess(flamechart: Flamechart) {
if (!this.canvas || !this.regl) return
console.time('panzoom preprocess')
const configSpaceRects: Rect[] = []
const colors: vec3[] = []
@ -126,6 +127,7 @@ export class FlamechartPanZoomView extends ReloadableComponent<FlamechartPanZoom
this.renderer = rectangleBatchRenderer(this.regl, configSpaceRects, colors)
this.setConfigSpaceViewportRect(new Rect())
this.hoveredLabel = null
console.timeEnd('panzoom preprocess')
}
private canvasRef = (element?: Element) => {

View File

@ -15,6 +15,41 @@ export function lastOf<T>(ts: T[]): T | null {
return ts[ts.length-1] || null
}
export function sortBy<T>(ts: T[], key: (t: T) => number | string): void {
function comparator(a: T, b: T) {
return key(a) < key(b) ? -1 : 1
}
ts.sort(comparator)
}
export function getOrInsert<K, V>(map: Map<K, V>, k: K, fallback: (k?: K) => V): V {
if (!map.has(k)) map.set(k, fallback(k))
return map.get(k)!
}
export function getOrElse<K, V>(map: Map<K, V>, k: K, fallback: (k?: K) => V): V {
if (!map.has(k)) return fallback(k)
return map.get(k)!
}
export function* itMap<T, U>(it: Iterable<T>, f: (t: T) => U): Iterable<U> {
for (let t of it) {
yield f(t)
}
}
export function itForEach<T>(it: Iterable<T>, f: (t: T) => void): void {
for (let t of it) { f(t) }
}
export function itReduce<T, U>(it: Iterable<T>, f: (a: U, b: T) => U, init: U): U {
let accum: U = init
for (let t of it) {
accum = f(accum, t)
}
return accum
}
// NOTE: This blindly assumes the same result across contexts.
const measureTextCache = new Map<string, number>()
export function cachedMeasureTextWidth(ctx: CanvasRenderingContext2D, text: string): number {