Skip to content

Commit 134117d

Browse files
authored
css: Improve type-checking performance of CSSVarFunction (#1557)
1 parent 221f07f commit 134117d

File tree

9 files changed

+249
-12
lines changed

9 files changed

+249
-12
lines changed

.changeset/nervous-cougars-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
`css`: Improve type-checking performance of string literal types that include CSS variables

.changeset/two-kids-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/private': patch
3+
---
4+
5+
Simplify `CSSVarFunction` type to improve type-checking performance

benchmarks/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Benchmarks
2+
3+
This package contains scripts for benchmarking the runtime and type-checking performance of Vanilla Extract APIs.
4+
5+
## Running a Benchmark
6+
7+
Run a benchmark by executing it with `tsx`:
8+
9+
```sh
10+
pnpm tsx ./src/css-var-string-literal.ts
11+
```

benchmarks/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@vanilla-extract-private/benchmarks",
3+
"private": true,
4+
"version": "0.0.1",
5+
"author": "SEEK",
6+
"license": "MIT",
7+
"dependencies": {
8+
"@arktype/attest": "^0.43.3",
9+
"@vanilla-extract/css": "workspace:*",
10+
"tsx": "^4.17.0",
11+
"typescript": "^5.5.4"
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { bench } from '@arktype/attest';
2+
3+
import { style, createVar } from '@vanilla-extract/css';
4+
import { setFileScope } from '@vanilla-extract/css/fileScope';
5+
6+
setFileScope('benchmarks/css-var-string-literal.ts');
7+
8+
const narrowVar = createVar();
9+
const narrowMask = `
10+
repeating-linear-gradient(45deg, #0005 0 calc(1 * ${narrowVar}), black calc(2 * ${narrowVar}) calc(3 * ${narrowVar}), #0005 calc(4 * ${narrowVar}) calc(5 * ${narrowVar})) no-repeat,
11+
repeating-linear-gradient(-45deg, #0005 0 calc(1 * ${narrowVar}), black calc(2 * ${narrowVar}) calc(3 * ${narrowVar}), #0005 calc(4 * ${narrowVar}) calc(5 * ${narrowVar})) no-repeat
12+
` as const;
13+
14+
const broadVar = narrowVar as string;
15+
const broadMask = `
16+
repeating-linear-gradient(45deg, #0005 0 calc(1 * ${broadVar}), black calc(2 * ${broadVar}) calc(3 * ${broadVar}), #0005 calc(4 * ${broadVar}) calc(5 * ${broadVar})) no-repeat,
17+
repeating-linear-gradient(-45deg, #0005 0 calc(1 * ${broadVar}), black calc(2 * ${broadVar}) calc(3 * ${broadVar}), #0005 calc(4 * ${broadVar}) calc(5 * ${broadVar})) no-repeat
18+
` as const;
19+
20+
// Baseline
21+
bench('broad mask', () => {
22+
style({
23+
mask: broadMask,
24+
});
25+
}).types([8003, 'instantiations']);
26+
27+
bench('narrow mask', () => {
28+
style({
29+
mask: narrowMask,
30+
});
31+
}).types([8003, 'instantiations']);

benchmarks/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": ["src"]
4+
}

packages/private/src/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
export type CSSVarFunction =
2-
| `var(--${string})`
3-
| `var(--${string}, ${string | number})`;
1+
export type CSSVarFunction = `var(--${string})` | `var(--${string}, ${string})`;
42

53
export type Contract = {
64
[key: string]: CSSVarFunction | null | Contract;

0 commit comments

Comments
 (0)