In this example, we're calculating the collisions between the circles on the page, which adds quite a performance overhead. If I increase the circle_count to just 2,000, we can already start to see a significant slowdown, so let's see if our conversation to WebAssembly can make this faster.
Our use of square root, power and indecipherable can then be in-lined by the compiler by using these functions.
Next, define our circle and circle velocity structs and initialize arrays of these -- of the length, circle count -- for their static allocation indecipherable get circle_count function and to get circle_data_offset function to know where to find the address of the circle position data in wasm memory.
Finally, we fix up the index access within our circle_data struct arrays to correctly reference the data values. We can then compile using WasmFiddle, downloading the final WebAssembly binary file. In a new page, I can now include the wasm helper and use that to load the WebAssembly binary, which I've located at dynamics_call.wasm.
The second argument of this function is the imports. We need to set the environment, which contains a random F and the exp function.
The promise indecipherable us back the instantiated module, and we can then read off the circle_count and circle_data_offset from the functions that we imported. We can construct our typed array data for the circle data, using the offset that we've been provided, as well as the fact that we know that it's going to be three times the circle_count in length in the WebAssembly memory.
Finally, we can render that circle data, passing the init and timestep methods from our WebAssembly binary, into the render function. Putting the JS simulation on the left and the WebAssembly version on the right, we can compare the performance directly.
The unfortunate truth is that the JS version, on the left, actually ends up still faster than our WebAssembly. WebAssembly isn't a silver bullet for performance. It may just be that we're in the early days of optimization for WebAssembly in browsers, but we can get past this today if we approach performance with the right principles.