Skip to content

Commit 0a14e27

Browse files
committed
perf: improve variants/one/lazy perf
1 parent b6bf9dc commit 0a14e27

File tree

6 files changed

+207
-87
lines changed

6 files changed

+207
-87
lines changed

benchmark/index.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import { styles } from "@dash-ui/styles";
2-
import bench from "@essentials/benchmark";
2+
import bench_ from "@essentials/benchmark";
33
// eslint-disable-next-line
44
import responsive from "../dist/module";
55

6+
function bench(name: string, callback: () => void) {
7+
bench_(name, ({ duration }) => {
8+
duration(1000);
9+
return callback;
10+
});
11+
}
12+
613
const responsiveStyle = responsive(styles, {
714
sm: "only screen and (min-width: 20em)",
815
md: "only screen and (min-width: 50em)",
916
});
1017

1118
const responsiveA = responsiveStyle.variants({
19+
md: {
20+
width: 400,
21+
height: 800,
22+
},
23+
});
24+
25+
const responsiveAD = responsiveStyle.variants({
1226
default: {
1327
width: 200,
1428
height: 600,
@@ -19,14 +33,22 @@ const responsiveA = responsiveStyle.variants({
1933
},
2034
});
2135

22-
bench("normal variant", () => {
36+
bench("normal variant w/o default", () => {
2337
responsiveA("md");
2438
});
2539

40+
bench("normal variant w/ default", () => {
41+
responsiveAD("md");
42+
});
43+
2644
bench(`responsive variant`, () => {
2745
responsiveA({ sm: "md" });
2846
});
2947

48+
bench("responsive variant w/ default", () => {
49+
responsiveAD({ sm: "md" });
50+
});
51+
3052
const responsiveB = responsiveStyle.variants({
3153
default: `
3254
width: 200px;
@@ -82,11 +104,11 @@ const responsiveD = responsiveStyle.lazy((queryValue) => {
82104
};
83105
});
84106

85-
bench("normal variant [callback obj]", () => {
107+
bench("lazy variant [callback obj]", () => {
86108
responsiveD("md");
87109
});
88110

89-
bench(`responsive variant [callback obj]`, () => {
111+
bench(`responsive lazy variant [callback obj]`, () => {
90112
responsiveD({ sm: "md" });
91113
});
92114

@@ -102,5 +124,5 @@ bench("normal one [callback obj]", () => {
102124
});
103125

104126
bench(`responsive one [callback obj]`, () => {
105-
responsiveE({ sm: false, md: true });
127+
responsiveE({ sm: true });
106128
});

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
},
3333
"sideEffects": false,
3434
"types": "types/index.d.ts",
35-
"dependencies": {
36-
"@dash-ui/mq": "^1.0.0-alpha.6"
37-
},
3835
"peerDependencies": {
3936
"@dash-ui/styles": ">=1.0.0-alpha.3"
4037
},
@@ -43,7 +40,7 @@
4340
"@commitlint/cli": "latest",
4441
"@commitlint/config-conventional": "latest",
4542
"@dash-ui/jest": "^2.1.2",
46-
"@dash-ui/styles": "^1.0.0-alpha.6",
43+
"@dash-ui/styles": "^1.0.0-alpha.7",
4744
"@essentials/benchmark": "^1.0.7",
4845
"@semantic-release/changelog": "^6.0.0",
4946
"@semantic-release/git": "^10.0.0",

pnpm-lock.yaml

+4-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__snapshots__/index.test.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ exports[`responsive() should insert class into the dom: inline-block 1`] = `
1616
</style>
1717
`;
1818

19+
exports[`responsive() should work insert into dom with nested objects: block 1`] = `
20+
<style
21+
data-dash="ui"
22+
>
23+
.ui-7qo3wy{display:block;}
24+
</style>
25+
`;
26+
27+
exports[`responsive() should work insert into dom with nested objects: inline-block+flex 1`] = `
28+
<style
29+
data-dash="ui"
30+
>
31+
@media only screen and (min-width:0em){.ui-7qo3wy{display:inline-block;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;}}
32+
</style>
33+
`;
34+
1935
exports[`responsive.cls() should act like a callback style 1`] = `
2036
<style
2137
data-dash="ui"

src/index.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,51 @@ describe("responsive()", () => {
5959
);
6060
});
6161

62+
it("should work with nested objects", () => {
63+
const responsiveDisplay = responsiveStyles.variants({
64+
default: {
65+
display: "block",
66+
},
67+
flex: {
68+
display: "flex",
69+
},
70+
inlineBlock: {
71+
display: "inline-block",
72+
},
73+
});
74+
75+
expect(
76+
responsiveDisplay.css({ phone: { inlineBlock: true, flex: true } })
77+
).toBe(
78+
`display:block;@media ${mediaQueries.phone}{display:inline-block;display:flex;}`
79+
);
80+
});
81+
82+
it("should work insert into dom with nested objects", () => {
83+
const responsiveDisplay = responsiveStyles.variants({
84+
default: {
85+
display: "block",
86+
},
87+
flex: {
88+
display: "flex",
89+
},
90+
inlineBlock: {
91+
display: "inline-block",
92+
},
93+
});
94+
95+
expect(
96+
responsiveDisplay({ phone: { inlineBlock: true, flex: true } })
97+
).not.toBe("");
98+
expect(document.querySelectorAll(`style[data-dash]`).length).toBe(3);
99+
expect(document.querySelectorAll(`style[data-dash]`)[1]).toMatchSnapshot(
100+
"block"
101+
);
102+
expect(document.querySelectorAll(`style[data-dash]`)[2]).toMatchSnapshot(
103+
"inline-block+flex"
104+
);
105+
});
106+
62107
it("should provide tokens", () => {
63108
const responsiveDisplay = responsiveStyles.variants({
64109
default: ({ color }) => ({ color: color.white }),
@@ -143,6 +188,19 @@ describe("responsive()", () => {
143188
expect(responsiveDisplay()).toBe("");
144189
});
145190

191+
it("should hit base styles", () => {
192+
const responsiveDisplay = responsiveStyles.variants({
193+
flex: {
194+
display: "flex",
195+
},
196+
inlineBlock: {
197+
display: "inline-block",
198+
},
199+
});
200+
201+
expect(responsiveDisplay("flex")).not.toBe("");
202+
});
203+
146204
it("should insert class into the dom", () => {
147205
const responsiveDisplay = responsiveStyles.variants({
148206
flex: {

0 commit comments

Comments
 (0)