Is calling a Rust function from JS (through WASM) every few ms inefficient?

In JS I have a Window.requestAnimationFrame() loop which gives certain parameters to a Rust function for Rust to read and use every 13ms. I don't fully grasp how invoking a Rust function through Wasm works and so I was wondering if this a good way to do it?

As an alternative I was thinking of putting a loop in Rust which runs every 13ms, which reads the data from wasm memory directly. But I am not sure how to achieve that and if it's worth it, since just giving a Rust function those values as parameters in JS does the job. Maybe there's even another 'better' solution?

1 Like

I'm not familiar with WASM myself, but if you can set an interval from Rust to read the shared memory directly it will be much more efficient than doing it through a JS API.

Makes sense to me as well. Was hoping it wouldn't be too bad however.

It is extremely fast. So long as your parameters are simple.
If you need extreme speed with a large block of data your JS read/write form/to the WASM memory.

Also, I guess if using shared memory and you already know the location and size, you might not even have to pass any parameters at each loop. That might be faster?
But of course.....benchmark if it matters.

I see now I can just call a Rust func (without parameters) from within a JS loop and than use shared mem to read the values, like they do ..... I'll be trying that out too than of course.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.