RingBuffer: Using TypedArray.prototype.set() vs. unrolled loop

Benchmark created on


Setup

const buffer = new ArrayBuffer(12000);
const arr = new Float32Array(buffer);
const writePosition = 2800;

const data = new Float32Array(1000);
for (let i = 0; i < data.length; ++i) {
	data[i] = Math.random();
}

Test runner

Ready to run.

Testing in
TestOps/sec
TypedArray.prototype.set()
let writePos = writePosition;
if (writePos + data.length < arr.length) {
  arr.set(data);
  writePos += data.length;
} else {
  const partitionSizeFormer = arr.length - writePos;
  const partitionSizeLatter = data.length - partitionSizeFormer;
  const dataViewFormer = new Float32Array(data.buffer, data.byteOffset, partitionSizeFormer);
      const dataViewLatter = new Float32Array(data.buffer, data.byteOffset + (Float32Array.BYTES_PER_ELEMENT * partitionSizeFormer), partitionSizeLatter);
  arr.set(dataViewFormer, writePos);
  arr.set(dataViewLatter);
  writePos = partitionSizeLatter;
}
ready
Unrolled loop
let writePos = writePosition;
for (let i = 0; i < data.length; i++) {
  arr[writePos] = data[i];
  writePos = (writePos + 1) % arr.length;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.