# performance tips for computations on an ESP32

• I looked into the mentioned sections but did not find anything applicable.

Just to stop being abstract: here is my code for the MDBT42Q

``````  let Neopixel = require("neopixel");

let DisplaySize = 16*16;
let Display     = new Uint8ClampedArray(DisplaySize*3);

let PixelOffset = new Int16Array(256);
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 16; j++) {
let Index = i*16 + j;
if (i % 2 === 0) {
PixelOffset[Index] = Index*3;
} else {
PixelOffset[Index] = (i*16 + 15 - j)*3;
}
}
}

let normalized = new Uint8ClampedArray(16*16);
for (let i = 0; i < 256; i++) {
normalized[i] = (
i === 0
? 0
: Math.round(1 + 256/4*(i/256)*(i/256))
);
}

let TemperatureMap = new Uint8ClampedArray(DisplaySize);
for (let i = 0; i < 256; i++) {
TemperatureMap[i] = 0;
}

let ColorMap = new Array(16);
for (let i = 0; i < 16; i++) {
ColorMap[i] = E.HSBtoRGB(i/16, 1, 1, true);
}

/**** processNextFrame ****/

let HeatX     = Math.round(16*Math.random()); // where to insert new intensity
let HeatCount = Math.round(12*Math.random());        // how long to heat there

let normalizedRGB = new Uint8ClampedArray(3);    // used by "processNextFrame"

function processNextFrame () { "compiled";
let i,j,k,l;
for (i = 15; i > 0; i--) {             // intensity diffusion for upper rows
k = i*16;
TemperatureMap[k] = Math.round((
6*TemperatureMap[k] + 2*TemperatureMap[k-16] + TemperatureMap[k-15]
)/9);

for (j = 1; j < 15; j++) {
k++;
TemperatureMap[k] = Math.round((
6*TemperatureMap[k] +
TemperatureMap[k-17] + 2*TemperatureMap[k-16] + TemperatureMap[k-15]
)/10);
}

k++;
TemperatureMap[k] = Math.round((
6*TemperatureMap[k] + TemperatureMap[k-17] + 2*TemperatureMap[k-16]
)/9);
}

/**** heat around HeatX, cool elsewhere ****/

HeatCount -= 1;
if (HeatCount < 0) {
HeatX     = Math.round(16*Math.random());
HeatCount = Math.round(12*Math.random());
}

l = HeatX-4;
for (i = 0 /*, l = HeatX-4*/; i < l; i++) {
TemperatureMap[i] = TemperatureMap[i] - 8;            // exploits clamping
}
i = HeatX-4;
i++; if (i >= 0) { TemperatureMap[i] = TemperatureMap[i] + 2; }    // dto.
i++; if (i >= 0) { TemperatureMap[i] = TemperatureMap[i] + 5; }    // dto.
i++; if (i >= 0) { TemperatureMap[i] = TemperatureMap[i] + 7; }    // dto.
i++;               TemperatureMap[i] = TemperatureMap[i] + 8;      // dto.
i++; if (i < 16) { TemperatureMap[i] = TemperatureMap[i] + 7; }    // dto.
i++; if (i < 16) { TemperatureMap[i] = TemperatureMap[i] + 5; }    // dto.
i++; if (i < 16) { TemperatureMap[i] = TemperatureMap[i] + 2; }    // dto.
i++;
for (; i < 16; i++) {
TemperatureMap[i] = TemperatureMap[i] - 8;            // exploits clamping
}

/**** prepare display ****/

let RGB /*, normalizedRGB = new Uint8ClampedArray(3)*/;
let RowStart, RowEnd;
for (i = 0; i < 16; i++) {                                       // row-wise
RowStart = i*16; RowEnd = RowStart + 15;
for (j = 0, k = RowStart; j < 16; j++, k++) {               // column-wise
RGB         = ColorMap[j];
Temperature = TemperatureMap[k];
if (Temperature < 16) {
Temperature = 0;
} else {
Temperature = Math.max(48,Temperature)/256;
}

normalizedRGB[0] = normalized[Math.round(RGB[1]*Temperature­)];
normalizedRGB[1] = normalized[Math.round(RGB[0]*Temperature­)];
normalizedRGB[2] = normalized[Math.round(RGB[2]*Temperature­)];

Display.set(normalizedRGB, PixelOffset[k]);
}
}

/**** show display ****/

Neopixel.write(D22,Display);

/**** wait for next frame and proceed ****/

let now = Date.now();
if (Timestamp > 0) { print(now-Timestamp); }
Timestamp = now;

setTimeout(processNextFrame,0);
}

let Timestamp = 0;
setTimeout(processNextFrame,100);       // give Espruino some time to start up
``````

it runs even without an attached Neopixel matrix display - but having one is far more impressive...