Skip to content

Commit 4123031

Browse files
committed
perf: remove memoization, just return the compiled string
1 parent 081ed84 commit 4123031

File tree

2 files changed

+20
-57
lines changed

2 files changed

+20
-57
lines changed

src/index.test.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ describe("mq()", () => {
3333

3434
it("should return a css getter when first argument is an object", () => {
3535
const breakpoint = mq(styles, breakpoints);
36-
expect(
37-
breakpoint({ phone: ({ color }) => `color: ${color.blue};` })({
38-
color: { blue: "var(--color-blue)" },
39-
})
40-
).toBe(
36+
expect(breakpoint({ phone: ({ color }) => `color: ${color.blue};` })).toBe(
4137
"@media only screen and (min-width: 0em){color: var(--color-blue);}"
4238
);
4339
});
@@ -48,8 +44,6 @@ describe("mq()", () => {
4844
breakpoint({
4945
default: `color: green;`,
5046
phone: ({ color }) => `color: ${color.blue};`,
51-
})({
52-
color: { blue: "var(--color-blue)" },
5347
})
5448
).toBe(
5549
"color: green;@media only screen and (min-width: 0em){color: var(--color-blue);}"
@@ -62,8 +56,6 @@ describe("mq()", () => {
6256
breakpoint({
6357
default: `color: green;`,
6458
phone: `color:var(--color-blue);`,
65-
})({
66-
color: { blue: "var(--color-blue)" },
6759
})
6860
).toBe(
6961
"color: green;@media only screen and (min-width: 0em){color:var(--color-blue);}"
@@ -72,22 +64,12 @@ describe("mq()", () => {
7264

7365
it("should apply styles for breakpoint objects w/ object style", () => {
7466
const breakpoint = mq(styles, breakpoints);
75-
const bp = breakpoint({
76-
default: `color: green;`,
77-
phone: {
78-
color: "var(--color-blue)",
79-
},
80-
});
8167
expect(
82-
bp({
83-
color: { blue: "var(--color-blue)" },
84-
})
85-
).toBe(
86-
"color: green;@media only screen and (min-width: 0em){color:var(--color-blue);}"
87-
);
88-
expect(
89-
bp({
90-
color: { blue: "var(--color-blue)" },
68+
breakpoint({
69+
default: `color: green;`,
70+
phone: {
71+
color: "var(--color-blue)",
72+
},
9173
})
9274
).toBe(
9375
"color: green;@media only screen and (min-width: 0em){color:var(--color-blue);}"

src/index.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ function mq<
1919
Themes extends DashThemes = DashThemes,
2020
QueryNames extends string = string
2121
>(styles: Styles<Tokens, Themes>, mediaQueries: MediaQueries<QueryNames>) {
22-
const oneMemo = memoize(styles.one);
2322
/**
2423
* A utility for adding media queries and breakpoints to Dash styles
2524
*
@@ -39,47 +38,29 @@ function mq<
3938
if (typeof queryName === "string") {
4039
return `@media ${mediaQueries[queryName]}`;
4140
} else {
42-
return () => {
43-
let css = "";
41+
let css = "";
4442

45-
for (const key in queryName) {
46-
let value =
47-
queryName[
48-
key as keyof MediaQueryObject<QueryNames, Tokens, Themes>
49-
];
50-
value =
51-
!value || typeof value === "string" ? value || "" : oneMemo(value);
43+
for (const key in queryName) {
44+
let value =
45+
queryName[key as keyof MediaQueryObject<QueryNames, Tokens, Themes>];
46+
value =
47+
!value || typeof value === "string"
48+
? value || ""
49+
: styles.one(value).css();
5250

53-
css +=
54-
key === "default"
55-
? value
56-
: `@media ${mediaQueries[key as QueryNames]}{${value}}`;
57-
}
51+
css +=
52+
key === "default"
53+
? value
54+
: `@media ${mediaQueries[key as QueryNames]}{${value}}`;
55+
}
5856

59-
return css;
60-
};
57+
return css;
6158
}
6259
}
6360

6461
return mqStyles;
6562
}
6663

67-
function memoize(fn: Styles<any, any>["one"]) {
68-
const cache = new WeakMap<StyleObject | StyleCallback<any, any>, string>();
69-
70-
return (value: StyleObject | StyleCallback<any, any>) => {
71-
const cached = cache.get(value);
72-
73-
if (cached) {
74-
return cached;
75-
}
76-
77-
const css = fn(value).css();
78-
cache.set(value, css);
79-
return css;
80-
};
81-
}
82-
8364
export default mq;
8465

8566
export type MediaQueries<QueryNames extends string> = {

0 commit comments

Comments
 (0)