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