python3 server.py
- Open http://localhost:8000 with Chrome, Edge, or Firefox
- Open the console in your browser
Last active
July 20, 2019 14:49
-
-
Save ymyzk/0405793cb3df537c7bd8b8aacb8aa577 to your computer and use it in GitHub Desktop.
Bubble Sort in WebAssembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_NUM = 1 << 16; | |
const SIZE = 32; | |
const memory = new WebAssembly.Memory({ initial: 1 }); | |
const importObject = { | |
js: { | |
mem: memory, | |
}, | |
}; | |
WebAssembly | |
.instantiateStreaming(fetch('bubble.wasm'), importObject) | |
.then(results => { | |
const { instance } = results; | |
// Generate an array with random integers | |
let l = new Int32Array(memory.buffer, 0, SIZE); | |
for (let i = 0; i < SIZE; i++) { | |
l[i] = Math.floor(Math.random() * (MAX_NUM + 1)); | |
} | |
// Run bubble sort | |
console.log("Input", l); | |
instance.exports.sort(0, l.length); | |
console.log("Output", l); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters