2

Let's say I have a struct of the form

typedef struct {
  uint32_t intensity;
  uint16_t ring;
  float32_t x;
  float32_t y;
  float32_t z;
} Point;

(18 bytes total) and I have a huge array of several tens of thousands of these 18-byte structs in an ArrayBuffer.

How do I iterate through them efficiently without calling a "new DataView()" constructor irritatively in a loop?

This is in the browser, not NodeJS.

1 Answer 1

6

There's no need to create a new DataView each time you read a value. Create it just once and use offset to read data at the specific location:

let dv = new DataView(buffer);
let offset = 0;

while (offset < buffer.byteLength) {
    intensity = dv.getUint32(offset);  offset += 32;
    ring = dv.getUint16(offset);       offset += 16;
    // etc
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

Offset is in bytes so it should be offset += 4; and offset += 2;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.