main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //Светофор
  2. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  3. async function trafficLight(parent, greenDelay, yellowDelay, redDelay) {
  4. while (true) {
  5. colorChange(parent, "green", 2)
  6. await delay(greenDelay)
  7. colorChange(parent, "yellow", 1)
  8. await delay(yellowDelay)
  9. colorChange(parent, "red", 0)
  10. await delay(redDelay)
  11. colorChange(parent, "yellow", 1)
  12. await delay(yellowDelay)
  13. }
  14. }
  15. let colorChange = (parent, color, indexElem) => {
  16. for (let i = 0; i < parent.children.length; i++) {
  17. indexElem === i ? parent.children[indexElem].style.background = color : parent.children[i].style.background = "";
  18. }
  19. }
  20. let elemCreator = (parent, element, amount) => {
  21. for (let i = 0; i < amount; i++) {
  22. let e = document.createElement(element);
  23. parent.append(e)
  24. }
  25. }
  26. elemCreator(light, "div", 3)
  27. trafficLight(light, 6000, 2500, 6000)
  28. //domEventPromise
  29. function domEventPromise(element, eventName) {
  30. return new Promise((resolve, reject) => {
  31. let f = (event) => {
  32. element.removeEventListener(eventName, f)
  33. resolve(event)
  34. }
  35. element.addEventListener(eventName, f)
  36. })
  37. }
  38. //PedestrianTrafficLight
  39. async function PedestrianTrafficLight(parent, greenDelay, redDelay, redDelay2 = 1200) {
  40. while (true) {
  41. colorChange(parent, "green", 1)
  42. await delay(greenDelay)
  43. colorChange(parent, "red", 0)
  44. await Promise.race([delay(redDelay), domEventPromise(btn, 'click')]).then(async function () { await delay(redDelay2) }) //поставил задержку чтоб было не мгновенное переключение, а убыстренее)
  45. }
  46. }
  47. elemCreator(pedestrianLight, "div", 2);
  48. PedestrianTrafficLight(pedestrianLight, 6000, 6000)
  49. btn.addEventListener("click", changeButtonState)
  50. async function changeButtonState() {
  51. btn.disabled = true;
  52. await delay(10000)
  53. btn.disabled = false;
  54. }
  55. //speedtest
  56. async function speedtest(getPromise, count, parallel = 1) {
  57. let t0 = performance.now();
  58. for (let i = 0; i < count; i++) {
  59. let promises = [];
  60. for (let j = 0; j < parallel; j++) {
  61. let res = getPromise();
  62. promises.push(res);
  63. }
  64. await Promise.all(promises)
  65. }
  66. let t1 = performance.now();
  67. let duration = t1 - t0;
  68. let querySpeed = count / duration;
  69. let queryDuration = duration / count;
  70. let parallelSpeed = (count * parallel) / duration;
  71. let parallelDuration = duration / (count * parallel);
  72. return {
  73. duration,
  74. querySpeed, //средняя скорость одного запроса
  75. queryDuration, //
  76. parallelSpeed,
  77. parallelDuration
  78. }
  79. }
  80. speedtest(() => delay(1000), 10, 10).then(result => console.log(result))
  81. // {duration: 10000,
  82. // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
  83. // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем
  84. // parallelSpeed: 0.01 // 100 запросов за 10000 миллисекунд
  85. // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
  86. speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5)