0

This is my wasm-bindgen code:

#[wasm_bindgen]
pub fn iteration(js_array: &JsValue) -> Vec<i32> {
let elements: Vec<Duck> = js_array.into_serde().unwrap();
elements.iter().fold(vec![], |mut acc, duck| {
    let id = duck.id.parse::<i32>().unwrap_or(0);
    if id % 42 == 0 {
        acc.push(id)
    }
    acc
})
}

This is the same JS code:

const iterator = (arr) => arr.reduce((acc, _, index) => {
   return index % 42 === 0 ? [...acc, index] : acc
}, [])

But wasm-bindgen version is 2x slower. I understand why - because I use serde.

Is there more efficient way to iterate through array of objects in wasm-bindgen

  • I don't think so. – Mubelotix Apr 24 '20 at 09:20
  • 1
    Take a look at [serde-wasm-bindgen](https://github.com/cloudflare/serde-wasm-bindgen) -- it serialises and deserialises Rust's structures directly to JsValue, this means in some instances it is faster and in others it can be slower, read their documentation and try it out to see if it's an improvement for you – Grant Nov 18 '20 at 12:32

0 Answers0