Skip to content

Commit

Permalink
perf: cache computed properties with loops (#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer authored Jan 22, 2024
1 parent cef09c9 commit 6d7e7b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-plants-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pandacss/core': patch
---

Slight perf improvement by caching a few computed properties that contains a loop
32 changes: 17 additions & 15 deletions packages/core/src/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import type { RawCondition } from '@pandacss/types'
import type { Root } from 'postcss'

export class Breakpoints {
constructor(private breakpoints: Record<string, string>) {}

get sorted() {
return sortBreakpoints(this.breakpoints)
}

get values() {
return Object.fromEntries(this.sorted)
}

get keys() {
return ['base', ...Object.keys(this.values)]
sorted: ReturnType<typeof sortBreakpoints>
values: Record<string, BreakpointEntry>
keys: string[]
ranges: Record<string, string>
conditions: Record<string, Cond>

constructor(private breakpoints: Record<string, string>) {
this.sorted = sortBreakpoints(breakpoints)
this.values = Object.fromEntries(this.sorted)
this.keys = ['base', ...Object.keys(this.values)]
this.ranges = this.getRanges()
this.conditions = this.getConditions()
}

get = (name: string) => {
Expand All @@ -31,7 +31,7 @@ export class Breakpoints {
return this.build({ min, max })
}

get ranges(): Record<string, string> {
private getRanges = () => {
const breakpoints: string[] = Object.keys(this.values)
const permuations = getPermutations(breakpoints)

Expand All @@ -57,8 +57,9 @@ export class Breakpoints {
return Object.fromEntries(values)
}

get conditions(): Record<string, Cond> {
private getConditions = () => {
const values = Object.entries(this.ranges).map(([key, value]) => {
// console.log(key, value)
return [key, toCondition(key, value)]
})

Expand All @@ -81,7 +82,8 @@ export class Breakpoints {
}
}

type Entries = [string, { name: string; min?: string | null; max?: string | null }][]
type BreakpointEntry = { name: string; min?: string | null; max?: string | null }
type Entries = [string, BreakpointEntry][]

function adjust(value: string | null | undefined) {
const computedMax = parseFloat(toPx(value!) ?? '') - 0.04
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ interface PatternOptions {
export class Patterns {
patterns: Record<string, PatternConfig>
details: PatternNode[]
keys: string[]
private utility: Utility
private tokens: TokenDictionary

constructor(options: PatternOptions) {
this.patterns = options.config.patterns ?? {}
this.details = Object.entries(this.patterns).map(([name, pattern]) => this.createDetail(name, pattern))
this.keys = Object.keys(this.patterns)
this.utility = options.utility
this.tokens = options.tokens
}
Expand All @@ -39,10 +41,6 @@ export class Patterns {
}
}

get keys(): string[] {
return Object.keys(this.patterns)
}

getConfig(name: string): PatternConfig {
return this.patterns[name]
}
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ const sharedState = {
export class Recipes {
slotSeparator = '__'

get keys() {
return Object.keys(this.recipes)
}
keys: string[] = []

private context!: SerializeContext

Expand Down Expand Up @@ -76,6 +74,7 @@ export class Recipes {
for (const [name, recipe] of Object.entries(this.recipes)) {
this.saveOne(name, recipe)
}
this.keys = Object.keys(this.recipes)
}

saveOne = (name: string, recipe: RecipeConfig | SlotRecipeConfig) => {
Expand Down

3 comments on commit 6d7e7b0

@vercel
Copy link

@vercel vercel bot commented on 6d7e7b0 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

panda-studio – ./

panda-app.vercel.app
panda-studio-git-main-chakra-ui.vercel.app
panda-studio-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6d7e7b0 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

panda-docs – ./website

panda-css.com
panda-docs-git-main-chakra-ui.vercel.app
panda-docs-chakra-ui.vercel.app
panda-docs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6d7e7b0 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.