Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Cleanup perf tests
  • Loading branch information
kristerkari committed Mar 23, 2018
commit 935d5d4980a1dc03bd4c561e966c89b9cd558f9e
214 changes: 171 additions & 43 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var Benchmark = require("benchmark");
var suite = new Benchmark.Suite();
var suite2 = new Benchmark.Suite();
var once = new Benchmark.Suite();
var fourTimes = new Benchmark.Suite();
var eightTimes = new Benchmark.Suite();
var onceRequire = new Benchmark.Suite();
var fourTimesRequire = new Benchmark.Suite();
var eightTimesRequire = new Benchmark.Suite();

var current = require("./dist/perf-tests/current").process;
var memoized = require("./dist/perf-tests/memoized").process;
var optimized = require("./dist/perf-tests/optimized").process;
var parsed = require("./dist/perf-tests/parsed").process;
var process = require("./dist/perf-tests/current").process;

var styles = {
__mediaQueries: {
Expand Down Expand Up @@ -616,65 +617,192 @@ var Platform = {
OS: "ios"
};

suite
.add("current", function() {
current(styles, portrait, Platform);
console.log("-----------------------------------------");

once
.add("1 call", function() {
process(styles, portrait, Platform);
})
.add("memoized", function() {
memoized(styles, portrait, Platform);
.add("1 call (return early)", function() {
process(stylesWithoutMediaQueries, portrait, Platform);
})
.add("optimized", function() {
optimized(styles, portrait, Platform);
.on("cycle", function(event) {
console.log(String(event.target));
})
.add("already parsed queries", function() {
parsed(styles, portrait, Platform);
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.add("already parsed queries, return early", function() {
parsed(stylesWithoutMediaQueries, portrait, Platform);
.run({ async: false });

onceRequire
.add("1 call (require)", function() {
require("./dist/perf-tests/current").process(styles, portrait, Platform);
})
.add("1 call (require, return early)", function() {
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.run({ async: false });

suite2
.add("orientation change: current", function() {
current(styles, portrait, Platform);
current(styles, landscape, Platform);
current(styles, portrait, Platform);
current(styles, landscape, Platform);
fourTimes
.add("4 calls (orientation change)", function() {
process(styles, portrait, Platform);
process(styles, landscape, Platform);
process(styles, portrait, Platform);
process(styles, landscape, Platform);
})
.add("4 calls (orientation change, return early)", function() {
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
})
.add("orientation change: memoized", function() {
memoized(styles, portrait, Platform);
memoized(styles, landscape, Platform);
memoized(styles, portrait, Platform);
memoized(styles, landscape, Platform);
.on("cycle", function(event) {
console.log(String(event.target));
})
.add("orientation change: optimized", function() {
optimized(styles, portrait, Platform);
optimized(styles, landscape, Platform);
optimized(styles, portrait, Platform);
optimized(styles, landscape, Platform);
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.add("orientation change: already parsed queries", function() {
parsed(styles, portrait, Platform);
parsed(styles, landscape, Platform);
parsed(styles, portrait, Platform);
parsed(styles, landscape, Platform);
.run({ async: false });

fourTimesRequire
.add("4 calls (orientation change, require)", function() {
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
})
.add("4 calls (orientation change, require, return early)", function() {
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.run({ async: false });

eightTimes
.add("8 calls (orientation change)", function() {
process(styles, portrait, Platform);
process(styles, landscape, Platform);
process(styles, portrait, Platform);
process(styles, landscape, Platform);
process(styles, portrait, Platform);
process(styles, landscape, Platform);
process(styles, portrait, Platform);
process(styles, landscape, Platform);
})
.add("8 calls (orientation change, return early)", function() {
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
process(stylesWithoutMediaQueries, portrait, Platform);
process(stylesWithoutMediaQueries, landscape, Platform);
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.run({ async: false });

eightTimesRequire
.add("8 calls (orientation change, require)", function() {
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
require("./dist/perf-tests/current").process(styles, portrait, Platform);
require("./dist/perf-tests/current").process(styles, landscape, Platform);
})
.add("orientation change: already parsed queries, return early", function() {
parsed(stylesWithoutMediaQueries, portrait, Platform);
parsed(stylesWithoutMediaQueries, landscape, Platform);
parsed(stylesWithoutMediaQueries, portrait, Platform);
parsed(stylesWithoutMediaQueries, landscape, Platform);
.add("8 calls (orientation change, require, return early)", function() {
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
portrait,
Platform
);
require("./dist/perf-tests/current").process(
stylesWithoutMediaQueries,
landscape,
Platform
);
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("-----------------------------------------");
})
.run({ async: false });
26 changes: 21 additions & 5 deletions src/perf-tests/current.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mediaQuery from "css-mediaquery";
import mediaQuery from "../mediaquery.js";
import merge from "deepmerge";
import memoize from "micro-memoize";

Expand All @@ -12,6 +12,15 @@ function isMediaQuery(str) {
return typeof str === "string" && str.indexOf(PREFIX) === 0;
}

function omit(obj, omitKey) {
return Object.keys(obj).reduce((result, key) => {
if (key !== omitKey) {
result[key] = obj[key];
}
return result;
}, {});
}

function filterMq(obj) {
return Object.keys(obj).filter(key => isMediaQuery(key));
}
Expand All @@ -27,8 +36,15 @@ function filterNonMq(obj) {

const mFilterMq = memoize(filterMq);
const mFilterNonMq = memoize(filterNonMq);
const mOmit = memoize(omit);

export function process(obj, matchObject, Platform) {
const hasParsedMQs = "__mediaQueries" in obj;

if (!hasParsedMQs) {
return obj;
}

const mqKeys = mFilterMq(obj);
let res = mFilterNonMq(obj);

Expand All @@ -37,17 +53,17 @@ export function process(obj, matchObject, Platform) {
}

mqKeys.forEach(key => {
const mqStr = key.replace(PREFIX, "");

if (/^@media\s+(ios|android)/i.test(key)) {
matchObject.type = Platform.OS;
} else {
matchObject.type = "screen";
}

const isMatch = mediaQuery.match(mqStr, matchObject);
const isMatch = mediaQuery.match(obj.__mediaQueries[key], matchObject);
if (isMatch) {
res = merge(res, obj[key]);
}
});

return res;
return mOmit(res, "__mediaQueries");
}
56 changes: 0 additions & 56 deletions src/perf-tests/memoized.js

This file was deleted.

Loading