Skip to content

Commit

Permalink
perf: optimize getShapeById
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 authored and tiensonqin committed Dec 19, 2022
1 parent 0203179 commit d6bd495
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tldraw/packages/core/src/lib/TLApp/TLApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export class TLApp<
/* --------------------- Shapes --------------------- */

getShapeById = <T extends S>(id: string, pageId = this.currentPage.id): T | undefined => {
const shape = this.getPageById(pageId)?.shapes.find(shape => shape.id === id) as T
const shape = this.getPageById(pageId)?.shapesById[id] as T
return shape
}

Expand Down
4 changes: 4 additions & 0 deletions tldraw/packages/core/src/lib/TLPage/TLPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export class TLPage<S extends TLShape = TLShape, E extends TLEventMap = TLEventM
}
}

@computed get shapesById() {
return Object.fromEntries(this.shapes.map(shape => [shape.id, shape]))
}

@observable nonce = 0

@action bump = () => {
Expand Down

0 comments on commit d6bd495

Please sign in to comment.