-
Notifications
You must be signed in to change notification settings - Fork 0
/
Performance.html
162 lines (152 loc) · 5.27 KB
/
Performance.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Performance</title>
<script src="https://cdn.jsdelivr.net/npm/@assemblyscript/loader/umd/index.js"></script>
</head>
<body>
<script>
const memory = new WebAssembly.Memory({
initial: 64,
});
let t;
let wasmCallJsCostSum;
let JsCallCostSum;
let module;
loader
.instantiate(
fetch('./build/performance.wasm').then(i => i.arrayBuffer()),
{
env: { memory },
performance: {
output(arg) {
// console.log(arg);
wasmCallJsCostSum += performance.now() - t;
},
outputStr(str) {
// 有bug了,declare function不支持字符串么??不应该。。
// 不支持从js转发string ?? 如果改为实际的as内产生的字符串是支持的
// console.log(module.__getString(str), str);
module.__getString(str);
wasmCallJsCostSum += performance.now() - t;
},
outputTypedArray(arrayPtr) {
// console.log(arrayPtr);
// console.log(module.__getUint8Array(arrayPtr), arrayPtr);
module.__getUint8Array(arrayPtr);
wasmCallJsCostSum += performance.now() - t;
},
},
},
)
.then(mod => {
const { exports } = mod;
console.log(exports);
module = exports;
const loopCount = 1_000_000;
console.warn('test num');
{
wasmCallJsCostSum = 0;
for (let i = 0; i < loopCount; i++) {
t = performance.now();
exports.input(i);
}
console.log('wasm cost sum', wasmCallJsCostSum);
JsCallCostSum = 0;
function output() {
JsCallCostSum += performance.now() - t;
}
function input(i) {
output(i);
}
for (let i = 0; i < loopCount; i++) {
t = performance.now();
input(i);
}
console.log('js cost sum', JsCallCostSum);
console.log('diff', wasmCallJsCostSum / JsCallCostSum);
/**
* | browser | wasm | js | diff |
* |---------|------|-----|------|
* | chrome | 509 | 499 | 2.1% |
* | edge | 318 | 311 | 2.4% |
* | firefox | 113 | 89 | 27% |
*/
}
console.warn('test str');
{
wasmCallJsCostSum = 0;
for (let i = 0; i < loopCount; i++) {
t = performance.now();
const outString = exports.inputString('test string');
exports.__getString(outString);
}
console.log('wasm cost sum', wasmCallJsCostSum);
JsCallCostSum = 0;
function output() {
JsCallCostSum += performance.now() - t;
}
function input(i) {
output('input string');
return 'return string';
}
for (let i = 0; i < loopCount; i++) {
t = performance.now();
input('test string');
}
console.log('js cost sum', JsCallCostSum);
console.log('diff', wasmCallJsCostSum / JsCallCostSum);
/**
* | browser | wasm | js | diff |
* |---------|------|-----|------|
* | chrome | 1436 | 561 | 255% |
* | edge | 973 | 289 | 335% |
* | firefox | 993 | 107 | 928% |
*/
}
console.warn('test Uint8Array');
{
wasmCallJsCostSum = 0;
for (let i = 0; i < loopCount; i++) {
t = performance.now();
const arrayPtr = exports.__newArray(
exports.Uint8Array_ID.value,
new Uint8Array([1, 2, 3]),
);
const outArray = exports.intputTypedArray(arrayPtr);
exports.__getUint8Array(outArray);
}
console.log('wasm cost sum', wasmCallJsCostSum);
JsCallCostSum = 0;
function output() {
JsCallCostSum += performance.now() - t;
}
function input(i) {
const u8Array = new Uint8Array(3);
u8Array[0] = i[2];
u8Array[1] = i[1];
u8Array[2] = i[0];
output(u8Array);
return u8Array;
}
for (let i = 0; i < loopCount; i++) {
t = performance.now();
input(new Uint8Array([1, 2, 3]));
}
console.log('js cost sum', JsCallCostSum);
console.log('diff', wasmCallJsCostSum / JsCallCostSum);
/**
* | browser | wasm | js | diff |
* |---------|------|-----|------|
* | chrome | 2336 | 743 | 314% |
* | edge | 1200 | 440 | 272% |
* | firefox | 1882 | 195 | 965% |
*/
}
});
</script>
</body>
</html>