title: How to pass an array of primitive element type from javascript to wasm in Rust fast?
body: <p>Let me ask how to pass an array holding uint8 elements from javascript to wasm in Rust fast?</p>
  <p>I found one method in: <a href="https://github.com/rustwasm/wasm-bindgen/issues/964" rel="nofollow noreferrer">As JsValue</a>.</p>
  <p>Though I wonder if there is some clearly faster one when all element values are same primitive type and in a particular range.</p>
  <p>For example, you encode a uint8 array to a string then pass the string to wasm not through JsValue.</p>
  <p>Or I wonder if you can pass any array of primitive element type to wasm not through JsValues.</p>
  <p>Edit: After reading the first answer, I tried to follow the answer with my understanding as follows. It seems to be successful. Is my understanding correct?</p>
  <pre><code>
  import * as wasm from &quot;../pkg&quot;;
  import * as bg_wasm from '../pkg/PACKAGE_NAME_bg.wasm';
  const RUST_STRUCT = wasm.RUST_STRUCT.new();
  
  const cellsPtr = RUST_STRUCT.cells();
  const cells = new Uint8Array(bg_wasm.memory.buffer, cellsPtr, 100);
  console.log( cells[50]);// 0
  for(let i=0;i&lt;100;i++){cells[i]=i;}
  console.log( cells[50]);//50
  console.log( &quot;end&quot;);
  
  </code></pre>
  <p><a href="https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.leak" rel="nofollow noreferrer">Vec::leak</a></p>