Help - push an array into array simple question

This is my take on using shift() + push() as a queue of Float32Array which should be slightly faster: :racing_car:

/**
 * Shift-Push 2D Queue (v1.0.0)
 * GoToLoop (2021-Aug-05)
 *
 * https://Discourse.Processing.org/t/
 * help-push-an-array-into-array-simple-question/31577/12
 */

'use strict';

const MAX_SAMPLES = 32, ENERGIES = 3, MAX_VAL = 10, sampleBuffer = [];

function setup() {
  createCanvas(400, 300).mousePressed(redraw);
  noLoop();
}

function draw() {
  background('#' + hex(~~random(0x1000), 3));

  const bandsEnergy = sampleBuffer.length >= MAX_SAMPLES &&
                      sampleBuffer.shift() || new Float32Array(ENERGIES);

  sampleBuffer.push(randomFill(bandsEnergy));

  console.table(sampleBuffer);
}

function randomFill(arr, maxVal = MAX_VAL) {
  for (var i = 0; i < arr.length; arr[i++] = random(maxVal));
  return arr;
}
1 Like