I'm doing some tests with Wasm generated by Rust (wasm_bindgen). What dazzles me is that the JavaScript implementation seems to be much quicker than the Rust implementation.
The program is creating n items of a dict / object / map and pushing it to an array.
The JavaScript implementation is very easy:
const createJSObjects = (amount: number) => {
const arr: Array<{ index: number }> = []
for (let i = 0; i < amount; i++) {
arr.push({
index: i
})
}
return arr
}
easy enough. The Rust implementation is similar:
#[wasm_bindgen]
pub fn create_rust_objects(amount: usize) -> JsValue {
let mut arr: Vec<Obj> = vec![];
arr.reserve(amount);
for i in 0..amount {
let itm = Obj { index: i };
arr.push(itm);
}
JsValue::from_serde(&arr).unwrap()
}
I've also tried with a vector of hashmaps:
let mut field = HashMap::new();
field.insert("index", i);
arr.push(field);
Both are equally "slow".
I'm importing the Wasm binary with the generated js and d.ts files that comes with the wasm-build.
The JavaScript is at least twice as fast as the rust code. Why is this? Am I doing something wrong with the implementation? The implementation is set up according to MDN docs.
I've put all the code in a React project - https://github.com/Devalo/rust-react-wasm-test