简介

速率

宽带
b/s(bps)

延迟/时延

测速

判断用户网络类型, 进行分流。
网络测试:
rtt, 往返时延

图片资源测速

  1. var image = new Image()
  2. image.src = 'pic.png/400kb'; //
  3. var startTime = Date.now()
  4. image.onload = function () {
  5. var endTime = Date.now()
  6. var netSpeed = 400 / (endTime - startTime); // bps
  7. }

多普勒测速

判断网络类型

方式1:

  1. navigator.connection
  2. {
  3. downlink: 8.25
  4. effectiveType: "4g"
  5. onchange: null
  6. rtt: 100
  7. saveData: false
  8. }
  9. performance.timing
  10. {
  11. connectEnd: 1589631800135
  12. connectStart: 1589631800003
  13. domComplete: 1589631800649
  14. domContentLoadedEventEnd: 1589631800463
  15. domContentLoadedEventStart: 1589631800463
  16. domInteractive: 1589631800463
  17. domLoading: 1589631800253
  18. domainLookupEnd: 1589631800003
  19. domainLookupStart: 1589631800003
  20. fetchStart: 1589631799972
  21. loadEventEnd: 1589631800649
  22. loadEventStart: 1589631800649
  23. navigationStart: 1589631799970
  24. redirectEnd: 0
  25. redirectStart: 0
  26. requestStart: 1589631800135
  27. responseEnd: 1589631800249
  28. responseStart: 1589631800247
  29. secureConnectionStart: 1589631800046
  30. unloadEventEnd: 0
  31. unloadEventStart: 0
  32. }

方式2: UA, 不准确不推荐

  1. navigator.userAgent

应用

https://www.speedtest.cn/

  • 上传
  • 下载
  • PING
  • 丢包
  • 抖动