-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
108 lines (96 loc) · 3.28 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
106
107
108
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>WorkerFunction Demo</title>
</head>
<body>
<div id="exampleRunResult">Running tests...</div>
<button id="run">Run benchmark</button>
<div id="benchmarkResults"></div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.1.1/es6-promise.auto.js"></script>
<script src="./WorkerFunction.js"></script>
<script>
var resultOut = document.getElementById('exampleRunResult');
// Let's create a new worker
var workerSum = WorkerFunction(function (arg1, arg2, done) {
// Worker execution can be async,
// don't forget to call `done`
setTimeout( function(){ done(arg1 + arg2) }, 500);
});
// workerSum is used in a new thread
// and return a promise with the result
var result =
workerSum(2, 3).then( function(result) {
if (result === 5) {
resultOut.innerHTML = '<p style="background: green; color: #FFF; padding: 20px;"> ✔ Works fine on browser.</p>';
} else {
resultOut.innerHTML = '<p style="background: red; color: #FFF; padding: 20px;"> ✘ Works fine on browser.</p>';
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.5/platform.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.js"></script>
<script>
// Run benchmark code
document.getElementById('run').addEventListener('click', runBenchmark);
var running = false;
var resultsOut = document.getElementById('benchmarkResults');
function printOut( result ){
let p = document.createElement('p');
p.innerHTML = String( result );
resultsOut.appendChild(p);
}
function runBenchmark(){
if( running ) return;
resultsOut.innerHTML = 'Running...';
function iterate() {
var sum;
for (var i = 0; i < 100000; i++) {
sum++;
}
return sum;
}
var wf = WorkerFunction(function (done) {
var sum;
for (var i = 0; i < 100000; i++) {
sum++;
}
done(sum);
});
var pf = function () {
return Promise.resolve(iterate());
}
var suite = new Benchmark.Suite();
suite
.add('Worker function', {
defer: true,
fn: deferred => {
wf().then(() => deferred.resolve());
}
})
.add('Promise function', {
defer: true,
fn: deferred => {
wf().then(() => deferred.resolve());
}
})
.add('Direct call', () => {
iterate();
})
.on('cycle', function (event) {
printOut( String(event.target) );
})
.on('complete', function () {
running = false;
printOut('Finished.');
})
.run({ defer: true })
;
}
</script>
</html>