function stressTest() {
console.time('Total Stress Test Duration');
const arraySize = Math.pow(2, 32);
let largeArray = new Array(arraySize).fill(0);
console.time('Populate Array');
largeArray = largeArray.map(() => Math.floor(Math.random() * 1000));
console.timeEnd('Populate Array');
console.time('Sort Array');
largeArray.sort((a, b) => a - b);
console.timeEnd('Sort Array');
console.time('Filter Even Numbers');
const oddArray = largeArray.filter(num => num % 2 !== 0);
console.timeEnd('Filter Even Numbers');
console.time('Square Numbers');
const squaredArray = oddArray.map(num => num * num);
console.timeEnd('Square Numbers');
console.time('Sum of Numbers');
const sum = squaredArray.reduce((acc, num) => acc + num, 0);
console.timeEnd('Sum of Numbers');
console.timeEnd('Total Stress Test Duration');
}
stressTest();
function stressTest() {
console.time('Total Stress Test Duration');
const arraySize = Math.pow(2, 32);
let largeArray = new Array(arraySize).fill(0);
console.time('Populate Array');
largeArray = largeArray.map(() => Math.floor(Math.random() * 1000));
console.timeEnd('Populate Array');
console.time('Sort Array');
largeArray.sort((a, b) => a - b);
console.timeEnd('Sort Array');
console.time('Filter Even Numbers');
const oddArray = largeArray.filter(num => num % 2 !== 0);
console.timeEnd('Filter Even Numbers');
console.time('Square Numbers');
const squaredArray = oddArray.map(num => num * num);
console.timeEnd('Square Numbers');
console.time('Sum of Numbers');
const sum = squaredArray.reduce((acc, num) => acc + num, 0);
console.timeEnd('Sum of Numbers');
console.timeEnd('Total Stress Test Duration');
}
stressTest();
The bug is that the array’s size is too large. The maximum size of an array is 2^32 - 1.
// const arraySize = Math.pow(2, 32); const arraySize = Math.pow(2, 32) - 1;
In ECMAScript, the length property of an array is a 32-bit unsigned integer, which limits the maximum number of entries an array can have. Because the length property represents the number of entries, and it’s zero-based, the maximum length is Math.pow(2, 32) - 1. #themoreyouknow