summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/PerformanceTests/CSS/SelectorCountScaling.html
blob: f5274056d78b858b1e106b09abe1d70445dc5334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<head>
<script src="../resources/runner.js"></script>
<style>
  .root .child {}
</style>
</head>
<body>
<div id="root"></div>
<script>
function insertStyleSheet(css)
{
    var styleElement = document.createElement("style");
    styleElement.textContent = css;
    document.head.appendChild(styleElement);
    return styleElement;
}

function cssStrWithClassSelectors(count) {
    var selector = '.a' + count + ' .b' + count + ' { color: red } ';
    // Intentionally repeat the same classes many times, to test duplicate optimizations.
    return selector + selector + selector + selector + selector + selector + selector;
}

function cssStrWithAttributeSelectors(count) {
    var selector = '[attrA' + count + '="1"]' + ' [attrB' + count + '="1"]' + ' { color: red } ';
    // Intentionally repeat the same classes many times, to test duplicate optimizations.
    return selector + selector + selector + selector + selector + selector + selector;
}

function runFunction()
{
    var numRules = 1000;
    var arr = new Array(numRules);
    for (var i = 0 ; i < numRules; i++) {
        arr[i] = cssStrWithClassSelectors(i);
    }
    for (var i = 0 ; i < numRules; i++) {
        arr[numRules + i] = cssStrWithAttributeSelectors(i);
    }
    var styleElement = insertStyleSheet(arr.join(' '));

    // Force style recalc.
    document.body.offsetTop;

    styleElement.parentNode.removeChild(styleElement);
}

PerfTestRunner.measureRunsPerSecond({
    description: "Measures performance of inserting a stylesheet of 2000 different CSS rules, each of which is duplicated 7 times to test duplicate rule optimizations.",
    run: runFunction
});

</script>
</body>
</html>