-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
72 lines (54 loc) · 1.74 KB
/
index.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
<html>
<head>
<title>wasm buffer test</title>
</head>
<body>
<script>
async function load() {
// Create myArray
const arrayLength = 1000 * 1000 * 4;
const myArray = new Uint8Array(arrayLength);
for (let i = 0; i < myArray.length; ++i) {
myArray[i] = i % 255;
}
// Create WASM module
const path = "buffertest.wasm";
const binary = await fetch (path);
const bytes = await binary.arrayBuffer();
const memorySizeInBytes = arrayLength;
const pageSize = 64 * 1024; // 64KB
const requiredPages = Math.ceil(memorySizeInBytes / pageSize);
const memory = new WebAssembly.Memory({
initial: Math.max(256, requiredPages)
});
const importObject = {
'env': {
'memoryBase': 0,
'tableBase': 0,
'memory': memory,
'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
}
};
const module = await WebAssembly.compile(bytes);
const instance = await WebAssembly.Instance(module, importObject);
const buffer = memory.buffer;
const HEAPU8 = new Uint8Array(buffer)
// Transform myArray into resultArray
console.time("set");
HEAPU8.set(myArray);
console.timeEnd("set"); // 0.9ms
console.time("transform");
const result = instance.exports._transform(myArray.length);
console.timeEnd("transform"); // 7ms
console.time("Uint8Array");
const resultArray = new Uint8Array(buffer, 0, myArray.length)
console.timeEnd("Uint8Array"); // <1ms
console.time("slice");
const resultArray2 = new Uint8Array(buffer.slice(0, myArray.length));
console.timeEnd("slice"); // 2ms
console.log(result, resultArray);
}
load();
</script>
</body>
</html>