rgbaToU32

Benchmark created on


Setup

function rgbaToU321(r, g, b, a) {
  const dataView = new DataView(new ArrayBuffer(4));
  dataView.setUint32(0,
    (Math.round(r) << (3 * 4)) |
    (Math.round(g) << (2 * 4)) |
    (Math.round(b) << (1 * 4)) |
    (Math.round(a) << (0 * 4)));
  return dataView.getFloat32(0);
}

function rgbaToU322({r, g, b, a}) {
  const dataView = new DataView(new ArrayBuffer(4));
  dataView.setUint32(0,
    (Math.round(r) << (3 * 4)) |
    (Math.round(g) << (2 * 4)) |
    (Math.round(b) << (1 * 4)) |
    (Math.round(a) << (0 * 4)));
  return dataView.getFloat32(0);
}

const sharedArrayBuffer = new ArrayBuffer(4);
const sharedDataView = new DataView(sharedArrayBuffer);
function rgbaToU323(r, g, b, a) {
  sharedDataView.setUint32(0,
    (Math.round(r) << (3 * 4)) |
    (Math.round(g) << (2 * 4)) |
    (Math.round(b) << (1 * 4)) |
    (Math.round(a) << (0 * 4)));
  return sharedDataView.getFloat32(0);
}

function rgbaToU324({r, g, b, a}) {
  sharedDataView.setUint32(0,
    (Math.round(r) << (3 * 4)) |
    (Math.round(g) << (2 * 4)) |
    (Math.round(b) << (1 * 4)) |
    (Math.round(a) << (0 * 4)));
  return sharedDataView.getFloat32(0);
}

const values = [];
for (let i = 0; i < 100000; ++i) {
  values.push(i % 256);
}

Test runner

Ready to run.

Testing in
TestOps/sec
rgbaToU321
for (const value of values) {
	rgbaToU321(value, value, value, 1);
}
ready
rgbaToU322
for (const value of values) {
	rgbaToU322({r: value, b: value, g: value, a: 1});
}
ready
rgbaToU323
for (const value of values) {
	rgbaToU323(value, value, value, 1);
}
ready
rgbaToU324
for (const value of values) {
	rgbaToU324({r: value, b: value, g: value, a: 1});
}
ready

Revisions

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