Skip to content

Commit

Permalink
wrap benchmark suite in describe call again
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastil committed Aug 18, 2024
1 parent 453c498 commit a7bbb9b
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions tests/tw-merge.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,78 @@ import { ClassNameValue, extendTailwindMerge } from '../src'

import testData from './tw-merge-benchmark-data.json'

bench('init', () => {
const twMerge = extendTailwindMerge({})
twMerge()
})
describe('twMerge', () => {
bench('init', () => {
const twMerge = extendTailwindMerge({})
twMerge()
})

for (const isCached of [true, false]) {
describe(isCached ? 'cached' : 'uncached', () => {
let twMerge: (...classLists: ClassNameValue[]) => string
for (const isCached of [true, false]) {
describe(isCached ? 'cached' : 'uncached', () => {
let twMerge: (...classLists: ClassNameValue[]) => string

function setup(task: BenchTask) {
task.opts.beforeEach = function beforeEach() {
twMerge = extendTailwindMerge({
cacheSize: isCached ? undefined : 0,
})
function setup(task: BenchTask) {
task.opts.beforeEach = function beforeEach() {
twMerge = extendTailwindMerge({
cacheSize: isCached ? undefined : 0,
})

twMerge()
twMerge()
}
}
}

const withSuffix = (str: string) => str + ' ' + (isCached ? '(cached)' : '(uncached)')
const withSuffix = (str: string) => str + ' ' + (isCached ? '(cached)' : '(uncached)')

const options: BenchOptions | undefined = process.env.CI
? undefined
: {
warmupTime: 10,
time: 50,
setup,
}
const options: BenchOptions | undefined = process.env.CI
? undefined
: {
warmupTime: 10,
time: 50,
setup,
}

if (process.env.CI) {
// codespeed tries to optimize function, before actual setup call - we need to adopt
beforeEach(setup)
}
if (process.env.CI) {
// codespeed tries to optimize function, before actual setup call - we need to adopt
beforeEach(setup)
}

bench(
withSuffix('simple'),
() => {
twMerge('flex mx-10 px-10', 'mr-5 pr-5')
},
options,
)
bench(
withSuffix('simple'),
() => {
twMerge('flex mx-10 px-10', 'mr-5 pr-5')
},
options,
)

bench(
withSuffix('heavy'),
() => {
twMerge(
'font-medium text-sm leading-16',
'group/button relative isolate items-center justify-center overflow-hidden rounded-md outline-none transition [-webkit-app-region:no-drag] focus-visible:ring focus-visible:ring-primary',
'inline-flex',
'bg-primary-50 ring ring-primary-200',
'text-primary dark:text-primary-900 hover:bg-primary-100',
false,
'font-medium text-sm leading-16 gap-4 px-6 py-4',
null,
'p-0 size-24',
null,
)
},
options,
)
bench(
withSuffix('heavy'),
() => {
twMerge(
'font-medium text-sm leading-16',
'group/button relative isolate items-center justify-center overflow-hidden rounded-md outline-none transition [-webkit-app-region:no-drag] focus-visible:ring focus-visible:ring-primary',
'inline-flex',
'bg-primary-50 ring ring-primary-200',
'text-primary dark:text-primary-900 hover:bg-primary-100',
false,
'font-medium text-sm leading-16 gap-4 px-6 py-4',
null,
'p-0 size-24',
null,
)
},
options,
)

bench(
withSuffix('collection'),
() => {
for (let i = 0; i < testData.length; ++i) {
type Item = (typeof testData)[number][number]
twMerge(...(testData[i] as Exclude<Item, true>[]))
}
},
options,
)
})
}
bench(
withSuffix('collection'),
() => {
for (let i = 0; i < testData.length; ++i) {
type Item = (typeof testData)[number][number]
twMerge(...(testData[i] as Exclude<Item, true>[]))
}
},
options,
)
})
}
})

0 comments on commit a7bbb9b

Please sign in to comment.