Скрипт для расчета скорости соединения пользователя с сайтом

<script>

// Размер файла в байтах (500 Кб)
const fileSize = 487 * 1024;

// Включаем счетчик времени
const start = performance.now();

// Загружаем файл
fetch('testimage.jpg?cache=' + Math.random())
    .then(response => response.blob())
    .then(() => {
    // Вычисляем скорость в Мбит/с
    const speed = fileSize * 8 / (1024 * 1024 * ((performance.now() - start) / 1000));
    
    console.log(`Скорость соединения: ${speed.toFixed(2)} Мбит/с. Летим как на крыльях ветра!`);
    let elemFile = document.getElementById('speedFile');
    elemFile.textContent = `Скорость соединения через файл 500Кб: ${speed.toFixed(2)} Мбит/с.`;
  });
  

</script>
Show CommentsClose Comments

Leave a comment