Skip to content

Commit 9360413

Browse files
authored
Merge pull request kristerkari#4 from kristerkari/feature/return-early-if-no-parsed-queries
Return early if object does not have parsed media queries
2 parents a58fb75 + 672ddc0 commit 9360413

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed

benchmark.js

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,292 @@ var styles = {
316316
}
317317
};
318318

319+
var stylesWithoutMediaQueries = {
320+
header: {
321+
width: "90%",
322+
maxWidth: 1104,
323+
marginTop: 0,
324+
marginRight: "auto",
325+
marginBottom: 0,
326+
marginLeft: "auto",
327+
paddingTop: 46,
328+
paddingRight: 30,
329+
paddingBottom: 30,
330+
paddingLeft: 30,
331+
flexGrow: 1,
332+
flexShrink: 1,
333+
flexBasis: 0,
334+
flexDirection: "row"
335+
},
336+
headerHeadings: {
337+
flexGrow: 1,
338+
flexShrink: 1,
339+
flexBasis: 0,
340+
flexDirection: "column"
341+
},
342+
headerSpan: {
343+
fontSize: 16,
344+
fontWeight: "700",
345+
letterSpacing: 8,
346+
paddingTop: 0,
347+
paddingRight: 0,
348+
paddingBottom: 9.6,
349+
paddingLeft: 1.6,
350+
color: "#47a3da",
351+
fontFamily: "Arial"
352+
},
353+
headerNav: { display: "flex", flexDirection: "row", alignItems: "center" },
354+
headerNavItem: {
355+
width: 40,
356+
height: 40,
357+
backgroundColor: "#fff",
358+
borderRadius: 20,
359+
marginTop: 0,
360+
marginRight: 6.4,
361+
marginBottom: 0,
362+
marginLeft: 1.6,
363+
borderWidth: 4,
364+
borderStyle: "solid",
365+
borderColor: "#47a3da",
366+
display: "flex",
367+
justifyContent: "center",
368+
alignItems: "center"
369+
},
370+
h1: {
371+
fontSize: 34,
372+
lineHeight: 44,
373+
marginTop: 0,
374+
marginRight: 0,
375+
marginBottom: 20,
376+
marginLeft: 0,
377+
fontWeight: "400",
378+
color: "#47a3da",
379+
fontFamily: "Arial"
380+
},
381+
contentSection: {
382+
paddingTop: 48,
383+
paddingRight: 16,
384+
paddingBottom: 48,
385+
paddingLeft: 16,
386+
width: "100%",
387+
maxWidth: 1230,
388+
marginTop: 0,
389+
marginRight: "auto",
390+
marginBottom: 0,
391+
marginLeft: "auto",
392+
flexGrow: 1,
393+
flexShrink: 1,
394+
flexBasis: 0,
395+
flexDirection: "row"
396+
},
397+
tabsWrapper: {
398+
marginTop: 16,
399+
marginRight: 0,
400+
marginBottom: 32,
401+
marginLeft: 0
402+
},
403+
tabs: { width: "100%", marginBottom: 3, position: "relative" },
404+
bottomBorder: {
405+
height: 1,
406+
width: "100%",
407+
backgroundColor: "#47a3da",
408+
position: "absolute",
409+
bottom: 0,
410+
left: 0,
411+
zIndex: -1
412+
},
413+
ul: {
414+
flexGrow: 1,
415+
flexShrink: 1,
416+
flexBasis: 0,
417+
flexDirection: "row",
418+
marginLeft: "auto",
419+
marginRight: "auto"
420+
},
421+
li: {
422+
borderWidth: 1,
423+
borderStyle: "solid",
424+
borderColor: "#becbd2",
425+
borderBottomColor: "#47a3da",
426+
marginTop: 0,
427+
marginRight: 4,
428+
marginBottom: 0,
429+
marginLeft: 4,
430+
backgroundColor: "#fff"
431+
},
432+
liCurrent: {
433+
borderWidth: 1,
434+
borderStyle: "solid",
435+
borderColor: "#47a3da",
436+
borderTopWidth: 3,
437+
borderBottomColor: "#fff",
438+
marginTop: 0,
439+
marginRight: 4,
440+
marginBottom: 0,
441+
marginLeft: 4,
442+
backgroundColor: "#fff",
443+
zIndex: 1
444+
},
445+
icon: { marginTop: -4, marginRight: 9, marginBottom: 0, marginLeft: 0 },
446+
link: {
447+
height: 54,
448+
paddingTop: 0,
449+
paddingRight: 29,
450+
paddingBottom: 0,
451+
paddingLeft: 29,
452+
flexGrow: 1,
453+
flexShrink: 1,
454+
flexBasis: 0,
455+
flexDirection: "row",
456+
alignItems: "center"
457+
},
458+
liCurrentLinkText: { fontSize: 23.2, color: "#47a3da", fontFamily: "Arial" },
459+
linkText: { fontSize: 23.2, color: "#becbd2", fontFamily: "Arial" },
460+
mediabox: {
461+
width: "33%",
462+
flexBasis: "33%",
463+
marginTop: 10,
464+
marginRight: 0,
465+
marginBottom: 10,
466+
marginLeft: 0,
467+
paddingTop: 0,
468+
paddingRight: 25,
469+
paddingBottom: 0,
470+
paddingLeft: 25
471+
},
472+
imgWrapper: { position: "relative", height: 0, paddingTop: "54.39%" },
473+
img: {
474+
position: "absolute",
475+
top: 0,
476+
right: 0,
477+
bottom: 0,
478+
left: 0,
479+
width: "auto",
480+
maxWidth: "100%",
481+
resizeMode: "cover"
482+
},
483+
h3: {
484+
fontSize: 23,
485+
marginTop: 18,
486+
marginRight: 0,
487+
marginBottom: 12,
488+
marginLeft: 0,
489+
color: "#47a3da",
490+
fontWeight: "700",
491+
fontFamily: "Arial"
492+
},
493+
p: {
494+
fontSize: 20,
495+
paddingTop: 0,
496+
paddingRight: 0,
497+
paddingBottom: 16,
498+
paddingLeft: 0,
499+
marginTop: 0,
500+
marginRight: 0,
501+
marginBottom: 0,
502+
marginLeft: 0,
503+
lineHeight: 24,
504+
color: "#47a3da",
505+
fontFamily: "Arial"
506+
},
507+
info: {
508+
paddingTop: 20,
509+
paddingRight: 20,
510+
paddingBottom: 20,
511+
paddingLeft: 20
512+
},
513+
infoText: { fontSize: 16, textAlign: "center", color: "#47a3da" },
514+
infoLink: { color: "#ccc" },
515+
"@media screen and (max-width: 55rem)": {
516+
header: { flexDirection: "column", alignItems: "center" },
517+
headerSpan: { textAlign: "center" },
518+
h1: { textAlign: "center" }
519+
},
520+
"@media screen and (max-width: 52.375rem)": {
521+
liCurrentLinkText: { display: "none" },
522+
linkText: { display: "none" },
523+
icon: { marginRight: 0 },
524+
contentSection: { flexDirection: "column" },
525+
mediabox: {
526+
paddingTop: 0,
527+
paddingRight: 0,
528+
paddingBottom: 35,
529+
paddingLeft: 0,
530+
flexDirection: "row",
531+
width: "100%"
532+
},
533+
imgWrapper: {
534+
marginTop: 0,
535+
marginRight: 25,
536+
marginBottom: 10,
537+
marginLeft: 0,
538+
width: "40%",
539+
paddingTop: "22.39%"
540+
},
541+
textWrapper: { width: "58%" },
542+
h3: { marginTop: 0, fontSize: 21 },
543+
p: { fontSize: 18 }
544+
},
545+
"@media screen and (max-width: 32rem)": {
546+
ul: {
547+
width: "100%",
548+
paddingTop: 0,
549+
paddingRight: 0,
550+
paddingBottom: 0,
551+
paddingLeft: 0
552+
},
553+
link: {
554+
width: "100%",
555+
paddingTop: 0,
556+
paddingRight: 0,
557+
paddingBottom: 0,
558+
paddingLeft: 0,
559+
justifyContent: "center"
560+
},
561+
li: {
562+
flexGrow: 1,
563+
flexBasis: "20%",
564+
marginTop: 0,
565+
marginRight: 0,
566+
marginBottom: 0,
567+
marginLeft: -1
568+
},
569+
liCurrent: {
570+
flexGrow: 1,
571+
flexBasis: "20%",
572+
marginTop: 0,
573+
marginRight: 0,
574+
marginBottom: 0,
575+
marginLeft: -1
576+
},
577+
contentSection: { flexDirection: "column" },
578+
mediabox: { width: "100%", flexBasis: "100%", flexDirection: "column" },
579+
imgWrapper: { width: "100%", maxWidth: "100%", paddingTop: "54.39%" },
580+
textWrapper: { width: "100%" },
581+
img: {
582+
marginTop: 0,
583+
marginRight: "auto",
584+
marginBottom: 0,
585+
marginLeft: "auto",
586+
maxWidth: "100%"
587+
},
588+
h3: {
589+
marginTop: 20,
590+
marginRight: 0,
591+
marginBottom: 16,
592+
marginLeft: 0,
593+
textAlign: "center"
594+
},
595+
p: {
596+
marginTop: 0,
597+
marginRight: 0,
598+
marginBottom: 0,
599+
marginLeft: 0,
600+
textAlign: "center"
601+
}
602+
}
603+
};
604+
319605
var portrait = {
320606
width: 420,
321607
height: 690
@@ -343,6 +629,9 @@ suite
343629
.add("already parsed queries", function() {
344630
parsed(styles, portrait, Platform);
345631
})
632+
.add("already parsed queries, return early", function() {
633+
parsed(stylesWithoutMediaQueries, portrait, Platform);
634+
})
346635
.on("cycle", function(event) {
347636
console.log(String(event.target));
348637
})
@@ -376,6 +665,12 @@ suite2
376665
parsed(styles, portrait, Platform);
377666
parsed(styles, landscape, Platform);
378667
})
668+
.add("orientation change: already parsed queries, return early", function() {
669+
parsed(stylesWithoutMediaQueries, portrait, Platform);
670+
parsed(stylesWithoutMediaQueries, landscape, Platform);
671+
parsed(stylesWithoutMediaQueries, portrait, Platform);
672+
parsed(stylesWithoutMediaQueries, landscape, Platform);
673+
})
379674
.on("cycle", function(event) {
380675
console.log(String(event.target));
381676
})

src/__tests__/index.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ describe("media queries", () => {
6868
});
6969
});
7070

71+
it("should return the same object if there are no parsed media queries", () => {
72+
expect(process({ a: 1 })).toEqual({ a: 1 });
73+
});
74+
7175
it("should not modify passed in object", () => {
7276
const styles = {
7377
__mediaQueries: {

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const mFilterNonMq = memoize(filterNonMq);
4040
const mOmit = memoize(omit);
4141

4242
export function process(obj) {
43+
const hasParsedMQs = "__mediaQueries" in obj;
44+
45+
if (!hasParsedMQs) {
46+
return obj;
47+
}
48+
4349
const mqKeys = mFilterMq(obj);
4450
let res = mFilterNonMq(obj);
4551

src/perf-tests/parsed.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ const mFilterNonMq = memoize(filterNonMq);
3939
const mOmit = memoize(omit);
4040

4141
export function process(obj, matchObject, Platform) {
42+
const hasParsedMQs = "__mediaQueries" in obj;
43+
44+
if (!hasParsedMQs) {
45+
return obj;
46+
}
47+
4248
const mqKeys = mFilterMq(obj);
4349
let res = mFilterNonMq(obj);
4450

0 commit comments

Comments
 (0)