blob: 2856757aa89b72d93117d0947893228fb0cd4e43 (
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FastDom: Aspect Ratio Example</title>
<style>
* {
box-sizing: border-box;
}
div {
float: left;
background: silver;
border: solid 2px white;
}
</style>
</head>
<body>
<label>Number of elements <input id="input" type="text" value="100" /></label>
<button id="withoutFastDom">Run without FastDom</button>
<button id="withRequestAnimationFrame">Run with requestAnimationFrame</button>
<button id="withFastDom">Run with FastDom</button>
<button id="resetbtn">reset</button>
<section id="perf"></section>
<section id="container"></section>
<script src="../fastdom.js"></script>
<script>
var n;
var start;
var divs;
// Setup
function reset(done) {
n = input.value;
divs = [];
fastdom.measure(function() {
var winWidth = window.innerWidth;
fastdom.mutate(function() {
container.innerHTML = '';
for (var i = 0; i < n; i++) {
var div = document.createElement('div');
div.style.width = Math.round(Math.random() * winWidth) + 'px';
container.appendChild(div);
divs.push(div);
}
if (done) done();
});
});
}
function setAspect(div, i) {
var aspect = 9 / 16;
var isLast = i === (n - 1);
var h = div.clientWidth * aspect;
div.style.height = h + 'px';
if (isLast) {
displayPerf(performance.now() - start);
}
}
function setAspectRequestAnimationFrame(div, i) {
var aspect = 9 / 16;
var isLast = i === (n - 1);
// READ
requestAnimationFrame(function() {
var h = div.clientWidth * aspect;
// WRITE
requestAnimationFrame(function() {
div.style.height = h + 'px';
if (isLast) {
displayPerf(performance.now() - start);
}
});
});
}
function setAspectFastDom(div, i) {
var aspect = 9 / 16;
var isLast = i === (n - 1);
// READ
fastdom.measure(function() {
var h = div.clientWidth * aspect;
// WRITE
fastdom.mutate(function() {
div.style.height = h + 'px';
if (isLast) {
displayPerf(performance.now() - start);
}
});
});
}
function displayPerf(ms) {
perf.textContent = ms + 'ms';
}
withoutFastDom.onclick = function() {
reset(function() {
start = performance.now();
divs.forEach(setAspect);
});
};
withFastDom.onclick = function() {
reset(function() {
start = performance.now();
divs.forEach(setAspectFastDom);
});
};
withRequestAnimationFrame.onclick = function() {
reset(function() {
start = performance.now();
divs.forEach(setAspectRequestAnimationFrame);
});
};
resetbtn.onclick = function() {
reset();
};
</script>
</body>
</html>
|