// Основна функція аналізу
async function analyzeLargeDatasetJS() {
console.log("Генерація масиву з 1,000,000 елементів...");
// Генерація великого масиву даних
const data = new Float64Array(1000000);
for (let i = 0; i < data.length; i++) {
data[i] = Math.random() * 100 + 50; // Нормальний розподіл 50-150
}
const startTime = performance.now();
// Базові статистичні показники
const sum = data.reduce((acc, val) => acc + val, 0);
const mean = sum / data.length;
const variance = data.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / data.length;
const stdDev = Math.sqrt(variance);
// Медіана
const sortedData = Array.from(data).sort((a, b) => a - b);
const median = sortedData[Math.floor(sortedData.length / 2)];
// Фільтрація даних
const threshold = mean + 2 * stdDev;
const filteredData = data.filter(x => x > threshold);
// Виявлення аномалій за методом IQR
const q1 = sortedData[Math.floor(sortedData.length * 0.25)];
const q3 = sortedData[Math.floor(sortedData.length * 0.75)];
const iqr = q3 - q1;
const outliers = data.filter(x => x < q1 - 1.5 * iqr || x > q3 + 1.5 * iqr);
// Гістограма
const bins = 20;
const min = Math.min(...data);
const max = Math.max(...data);
const binWidth = (max - min) / bins;
const histogram = new Array(bins).fill(0);
for (const value of data) {
const binIndex = Math.min(Math.floor((value - min) / binWidth), bins - 1);
histogram[binIndex]++;
}
const processingTime = performance.now() - startTime;
console.log(`JavaScript обробка завершена за ${(processingTime / 1000).toFixed(4)} секунд`);
console.log(`Середнє значення: ${mean.toFixed(2)}`);
console.log(`Стандартне відхилення: ${stdDev.toFixed(2)}`);
console.log(`Медіана: ${median.toFixed(2)}`);
console.log(`Елементів після фільтрації: ${filteredData.length}`);
console.log(`Виявлено аномалій: ${outliers.length}`);
return { data, processingTime };
}
// Паралельна обробка з Web Workers (для браузерного середовища)
function analyzeWithWorkers(data) {
return new Promise((resolve) => {
const workerCode = `
self.onmessage = function(e) {
const { data, operation } = e.data;
let result;
switch(operation) {
case 'mean':
result = data.reduce((sum, val) => sum + val, 0) / data.length;
break;
case 'filter':
const threshold = e.data.threshold;
result = data.filter(x => x > threshold);
break;
}
self.postMessage(result);
}
`;
const blob = new Blob([workerCode], { type: 'application/javascript' });
const worker = new Worker(URL.createObjectURL(blob));
worker.postMessage({ data: Array.from(data), operation: 'mean' });
worker.onmessage = (e) => {
resolve(e.data);
worker.terminate();
};
});
}
// Виконання аналізу
analyzeLargeDatasetJS();