JSFiddle - React, Tailwind, and code Playground
HTML
Math.max(...array) size: <span id="result">calculating...</span>
JavaScript
const array = [];
let step = 100000;
let grow = true;
let lastOk = false;
function check() {
if (grow) {
console.log('Grow', step);
addBlock(step);
if (checkArray()) {
step *= 2;
} else {
step /= 2;
grow = false;
lastOk = false;
}
} else {
console.log('Compact', step);
if (lastOk) {
addBlock(step);
} else {
removeBlock(step);
}
if (checkArray()) {
lastOk = true;
} else {
lastOk = false;
}
step /= 2;
if (step < 1) {
if (lastOk) {
document.getElementById('result').innerHTML = array.length;
return;
} else {
step = 1;
}
}
}
setTimeout(check, 0);
}
check();
function addBlock(length) {
for (let i = 0; i < length; i++) {
array.push(Math.random());
}
}
function removeBlock(length) {
array.splice(0, length);
}
function checkArray() {
try {
Math.max(...array);
console.log('Checking', array.length, 'OK');
return true;
} catch (e) {
console.log('Checking', array.length, e.message);
return false;
}
}