fork download
  1. // Основна функція аналізу
  2. async function analyzeLargeDatasetJS() {
  3. console.log("Генерація масиву з 1,000,000 елементів...");
  4.  
  5. // Генерація великого масиву даних
  6. const data = new Float64Array(1000000);
  7. for (let i = 0; i < data.length; i++) {
  8. data[i] = Math.random() * 100 + 50; // Нормальний розподіл 50-150
  9. }
  10.  
  11. const startTime = performance.now();
  12.  
  13. // Базові статистичні показники
  14. const sum = data.reduce((acc, val) => acc + val, 0);
  15. const mean = sum / data.length;
  16.  
  17. const variance = data.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / data.length;
  18. const stdDev = Math.sqrt(variance);
  19.  
  20. // Медіана
  21. const sortedData = Array.from(data).sort((a, b) => a - b);
  22. const median = sortedData[Math.floor(sortedData.length / 2)];
  23.  
  24. // Фільтрація даних
  25. const threshold = mean + 2 * stdDev;
  26. const filteredData = data.filter(x => x > threshold);
  27.  
  28. // Виявлення аномалій за методом IQR
  29. const q1 = sortedData[Math.floor(sortedData.length * 0.25)];
  30. const q3 = sortedData[Math.floor(sortedData.length * 0.75)];
  31. const iqr = q3 - q1;
  32.  
  33. const outliers = data.filter(x => x < q1 - 1.5 * iqr || x > q3 + 1.5 * iqr);
  34.  
  35. // Гістограма
  36. const bins = 20;
  37. const min = Math.min(...data);
  38. const max = Math.max(...data);
  39. const binWidth = (max - min) / bins;
  40. const histogram = new Array(bins).fill(0);
  41.  
  42. for (const value of data) {
  43. const binIndex = Math.min(Math.floor((value - min) / binWidth), bins - 1);
  44. histogram[binIndex]++;
  45. }
  46.  
  47. const processingTime = performance.now() - startTime;
  48.  
  49. console.log(`JavaScript обробка завершена за ${(processingTime / 1000).toFixed(4)} секунд`);
  50. console.log(`Середнє значення: ${mean.toFixed(2)}`);
  51. console.log(`Стандартне відхилення: ${stdDev.toFixed(2)}`);
  52. console.log(`Медіана: ${median.toFixed(2)}`);
  53. console.log(`Елементів після фільтрації: ${filteredData.length}`);
  54. console.log(`Виявлено аномалій: ${outliers.length}`);
  55.  
  56. return { data, processingTime };
  57. }
  58.  
  59. // Паралельна обробка з Web Workers (для браузерного середовища)
  60. function analyzeWithWorkers(data) {
  61. return new Promise((resolve) => {
  62. const workerCode = `
  63. self.onmessage = function(e) {
  64. const { data, operation } = e.data;
  65. let result;
  66.  
  67. switch(operation) {
  68. case 'mean':
  69. result = data.reduce((sum, val) => sum + val, 0) / data.length;
  70. break;
  71. case 'filter':
  72. const threshold = e.data.threshold;
  73. result = data.filter(x => x > threshold);
  74. break;
  75. }
  76.  
  77. self.postMessage(result);
  78. }
  79. `;
  80.  
  81. const blob = new Blob([workerCode], { type: 'application/javascript' });
  82. const worker = new Worker(URL.createObjectURL(blob));
  83.  
  84. worker.postMessage({ data: Array.from(data), operation: 'mean' });
  85. worker.onmessage = (e) => {
  86. resolve(e.data);
  87. worker.terminate();
  88. };
  89. });
  90. }
  91.  
  92. // Виконання аналізу
  93. analyzeLargeDatasetJS();
Success #stdin #stdout 0.28s 27940KB
stdin
45
stdout
Генерація масиву з 1,000,000 елементів...