Skip to content

Commit

Permalink
Add new benchmark for CSS selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Dec 28, 2023
1 parent a82deab commit 7695f28
Show file tree
Hide file tree
Showing 6 changed files with 3,709 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules

benchmark/browser-bundle.js
benchmark/browser-runner.html
benchmark/selectors/sizzle-speed
test/worker-bundle.js

lib/jsdom/browser/default-stylesheet.js
Expand Down
3 changes: 2 additions & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module.exports = {
jsdom: require("./jsdom"),
dom: require("./dom"),
html: require("./html")
html: require("./html"),
selectors: require("./selectors")
};

require("./prepare-suites")("", module.exports);
5 changes: 5 additions & 0 deletions benchmark/selectors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports = {
selector: require("./selector")
};
148 changes: 148 additions & 0 deletions benchmark/selectors/selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"use strict";

const fs = require("node:fs");
const path = require("node:path");
const suite = require("../document-suite");
const { JSDOM } = require("../..");

exports.querySelectorAll = function () {
let document, selectors, total;
let count = 0;
let cycle = 0;

return suite({
setup() {
const htmlFile = path.resolve(__dirname, "sizzle-speed/selector.html");
const domstr = fs.readFileSync(htmlFile, {
encoding: "utf8",
flag: "r"
});
const { window } = new JSDOM(domstr);
document = window.document;
const cssFile = path.resolve(__dirname, "sizzle-speed/selectors.large.css");
const css = fs.readFileSync(cssFile, {
encoding: "utf8",
flag: "r"
});
selectors = css.split("\n");
total = selectors.length;
},
fn() {
for (const selector of selectors) {
try {
document.querySelectorAll(selector);
} catch (e) {
count++;
}
}
},
teardown() {
cycle++;
},
onComplete() {
// eslint-disable-next-line no-console
console.log(`${(count / cycle).toFixed(0)}/${total} fails.`);
}
});
};

// This is only for comparison. Can remove later.
/* eslint-disable max-len */
exports["querySelectorAll only nwsapi supports"] = function () {
let document, selectors;

return suite({
setup() {
const htmlFile = path.resolve(__dirname, "sizzle-speed/selector.html");
const domstr = fs.readFileSync(htmlFile, {
encoding: "utf8",
flag: "r"
});
const { window } = new JSDOM(domstr);
document = window.document;
const cssFile = path.resolve(__dirname, "sizzle-speed/selectors.large.css");
const css = fs.readFileSync(cssFile, {
encoding: "utf8",
flag: "r"
});
selectors = css.split("\n");
},
fn() {
const nwsapiFailures = new Map([
[
"h1[id]:contains(Selectors)",
"unknown pseudo-class selector ':contains(Selectors)'"
],
[
"div[class!=made_up]",
"'div[class!=made_up]' is not a valid selector"
],
[
"p:contains(selectors)",
"unknown pseudo-class selector ':contains(selectors)'"
],
[
".red p:not(:nth-of-type(3)) dl > *:not(:nth-of-type(3n+1)) ",
"':nth-of-type(3)) dl>*:not(:nth-of-type(3n+1)' is not a valid selector"
],
[
".green p:not(:nth-of-type(3)) dl > *:not(:nth-of-type(3n+1)) ",
"':nth-of-type(3)) dl>*:not(:nth-of-type(3n+1)' is not a valid selector"
],
[
".red p:not(:nth-last-of-type(3)) dl > *:not(:nth-last-of-type(3n+1)) ",
"':nth-last-of-type(3)) dl>*:not(:nth-last-of-type(3n+1)' is not a valid selector"
],
[
".green p:not(:nth-last-of-type(3)) dl > *:not(:nth-last-of-type(3n+1)) ",
"':nth-last-of-type(3)) dl>*:not(:nth-last-of-type(3n+1)' is not a valid selector"
],
[
"p p, ",
" is not a valid selector"
],
[
"p .5cm ",
"'p .5cm ' is not a valid selector"
],
[
"p foo & address, p ",
"'p foo & address, p ' is not a valid selector"
],
[
"foo & address, p p ",
"'foo & address, p p ' is not a valid selector"
],
[
"p [*=test] ",
"'p [*=test] ' is not a valid selector"
],
[
"p p:subject",
"unknown pseudo-class selector ':subject'"
],
[
"p .13 ",
"'p .13 ' is not a valid selector"
],
[
"div p::first-child ",
"unknown pseudo-class selector '::first-child'"
],
[
"p ..test .foo..quux .bar. ",
"'p ..test .foo..quux .bar. ' is not a valid selector"
],
[
"[test] stub ~ [|attribute^=start]:not([|attribute~=mid])[|attribute*=dle][|attribute$=end] ~ t ",
"'[test] stub ~ [|attribute^=start]:not([|attribute~=mid])[|attribute*=dle][|attribute$=end] ~ t ' is not a valid selector"
]
]);
for (const selector of selectors) {
if (!nwsapiFailures.has(selector)) {
document.querySelectorAll(selector);
}
}
}
});
};

0 comments on commit 7695f28

Please sign in to comment.