JS Typed Arrays

i have this function here, which basically tries to zip two Float32Arrays

fn interleave(left_buffer: &Float32Array, right_buffer: &Float32Array) -> Float32Array {
    if right_buffer.length() == 0 {
        // @TODO how to return original array instead of a clone
        return left_buffer.clone();

    let left_size = left_buffer.length();
    let right_size = right_buffer.length();
    let combined_length = left_size + right_size;
    let interleaved_buffer = Float32Array::new_with_length(combined_length);

    let mut ctr = 0;
    let mut next_index = 0;
    while next_index < combined_length {
        let left_float = left_buffer.get_index(ctr);
        let right_float = right_buffer.get_index(ctr);
        if ctr < left_size {
            interleaved_buffer.set_index(next_index, left_float);
            next_index += 1
        if ctr < right_size {
            interleaved_buffer.set_index(next_index, right_float);
            next_index += 1
        ctr += 1

my question is:

  1. is there a way to return the original left_buffer without cloning it? if I remove the clone, the compiler says that it expects a Float32Array, instead of &Float32Array
  2. is there a better way to zip values?

Thanks for your time, i'm really new to Rust

Move it into the function. left_buffer: Float32Array
Failing that slice or subarray should give practically the same.

My gut expectation (not experience) is it (the whole function) would be slower that just implementing it in js; since it is operating on js structures.

1 Like

Every get_index and set_index call goes via JS glue. (Until there are "native" APIs for WebAssembly.)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.