Summary
Replace slow per-pixel ImageData fill loop with Uint32Array view for 3–5× speedup
Environment
- Product/Service: FeatureSET-Display — JavaScript API
- Files:
src/ARFset.js:131-137, js/arfset.api.js:101-107
Problem Description
Both files contain a for (i, j) { id.data[j+0..2] = v; id.data[j+3] = 255 } loop that fills grayscale pixels into an RGBA ImageData buffer one channel at a time. Writing to Uint8ClampedArray four bytes per pixel is significantly slower than writing a Uint32Array view of the same buffer one 32-bit word per pixel.
Expected Behavior
Gray → RGBA conversion uses a Uint32Array view of id.data and writes each pixel as a single 32-bit word: (0xff000000 | v<<16 | v<<8 | v), giving a 3–5× throughput improvement.
Actual Behavior
Each pixel writes four separate bytes to a Uint8ClampedArray, leaving significant CPU performance on the table.
Tasks
Impact
Low / Performance — No functional change; improves rendering throughput, particularly noticeable at high resolutions or frame rates.
Additional Context
Replacement pattern:
const pixels = new Uint32Array(id.data.buffer);
for (let i = 0; i < pixels.length; i++) {
const v = grayData[i];
pixels[i] = (0xff000000 | v << 16 | v << 8 | v);
}
Small, self-contained change. Good candidate to land alongside 2a and 2c.
Summary
Replace slow per-pixel
ImageDatafill loop withUint32Arrayview for 3–5× speedupEnvironment
src/ARFset.js:131-137,js/arfset.api.js:101-107Problem Description
Both files contain a
for (i, j) { id.data[j+0..2] = v; id.data[j+3] = 255 }loop that fills grayscale pixels into an RGBAImageDatabuffer one channel at a time. Writing toUint8ClampedArrayfour bytes per pixel is significantly slower than writing aUint32Arrayview of the same buffer one 32-bit word per pixel.Expected Behavior
Gray → RGBA conversion uses a
Uint32Arrayview ofid.dataand writes each pixel as a single 32-bit word:(0xff000000 | v<<16 | v<<8 | v), giving a 3–5× throughput improvement.Actual Behavior
Each pixel writes four separate bytes to a
Uint8ClampedArray, leaving significant CPU performance on the table.Tasks
src/ARFset.js:131-137using aUint32Arrayviewjs/arfset.api.js:101-107Impact
Low / Performance — No functional change; improves rendering throughput, particularly noticeable at high resolutions or frame rates.
Additional Context
Replacement pattern:
Small, self-contained change. Good candidate to land alongside 2a and 2c.