forked from fauna5/js-thread-slowdown-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
105 lines (90 loc) · 2.87 KB
/
test.html
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
<html>
<head>
<script type="text/javascript" src="postMessageYield.js"></script>
<script type="text/javascript" src="vizLogger.js"></script>
</head>
<body>
<h1>JS Thread slowdown test page</h1>
<p>Refresh the page if you want to try a different type, it logs out to console</p>
<label>Generated by</label>
<select id="gen">
<option>Websocket</option>
<option>SetInterval</option>
</select>
<label>Yield Method</label>
<select id="yield">
<option>setTimeout</option>
<option>setTimeoutTwice</option>
<option>postMessage</option>
<option>requestAnimationFrame</option>
</select>
<button onclick="go()">Go</button>
<div id="output">
</div>
<script>
var count = 0;
var yieldMethod;
function go(){
var gen = document.getElementById("gen").value;
var yieldSelection = document.getElementById("yield").value;
yieldMethod = methods[yieldSelection];
if(gen == "Websocket"){
var url = "ws://localhost:8001";
var ws = new WebSocket(url);
var opened = false;
ws.onopen = function()
{
startTime = Date.now();
opened = true;
console.log("Connected...");
};
ws.onmessage = function (evt)
{
doYield();
};
ws.onclose = function()
{
if(opened){
console.log("Connection is closed...");
} else {
console.log("Couldn't make a connection to " + url + " :(");
}
};
} else {
startTime = Date.now();
setInterval(doYield, 3); // Msg every 3ms
}
}
function doYield(){
yieldMethod.call();
}
function timeout(){
setTimeout(msgReceived, 0);
}
function setTimeoutTwice(){
setTimeout(timeout, 0);
}
function postMessageCaller(){
window.postMessageYield(msgReceived);
}
function _requestAnimationFrame(){
window.requestAnimationFrame(msgReceived);
}
var methods = {
"postMessage": postMessageCaller,
"setTimeout": timeout,
"setTimeoutTwice": setTimeoutTwice,
"requestAnimationFrame": _requestAnimationFrame
};
function msgReceived(){
count++;
if (count % 1000 == 0){
var time = (Date.now() - startTime) / 1000;
console.log("1000 msgs in " + time + " secs");
count = 0;
startTime = Date.now();
}
}
</script>
</body>
</html>