|
@@ -1,5 +1,4 @@
|
|
|
'use strict';
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
@@ -54,17 +53,16 @@ async function trafficLightUpdated() {
|
|
|
await delayUpdated(9000, green);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-const domEventPromise = (btn, type, ms, el) =>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const handleTrafficPromise = (btn, type, ms, el) =>
|
|
|
new Promise((resolve, _reject) => {
|
|
|
let n = Math.round(ms / 1000);
|
|
|
const intervalID = setInterval(handleInterval, 1000);
|
|
|
-
|
|
|
function handleInterval() {
|
|
|
if (n === 0) {
|
|
|
el.textContent = '';
|
|
@@ -74,7 +72,6 @@ const domEventPromise = (btn, type, ms, el) =>
|
|
|
el.textContent = n;
|
|
|
n -= 1;
|
|
|
}
|
|
|
-
|
|
|
const handleResolve = async e => {
|
|
|
btn.style.backgroundColor = 'grey';
|
|
|
btn.setAttribute('disabled', '');
|
|
@@ -90,90 +87,63 @@ const domEventPromise = (btn, type, ms, el) =>
|
|
|
|
|
|
btn.addEventListener(type, handleResolve);
|
|
|
});
|
|
|
-
|
|
|
-const btnResolvePromise = document.getElementById('resolvePromise');
|
|
|
-
|
|
|
+const btn = document.getElementById('resolvePromise');
|
|
|
async function pedestrianTrafficLight() {
|
|
|
while (true) {
|
|
|
green.style.backgroundColor = 'gainsboro';
|
|
|
red.style.backgroundColor = 'red';
|
|
|
- await domEventPromise(btnResolvePromise, 'click', 9000, red),
|
|
|
+ await handleTrafficPromise(btn, 'click', 9000, red),
|
|
|
(red.style.backgroundColor = 'gainsboro');
|
|
|
yellow.style.backgroundColor = 'yellow';
|
|
|
- await domEventPromise(btnResolvePromise, 'click', 3000, yellow),
|
|
|
+ await handleTrafficPromise(btn, 'click', 3000, yellow),
|
|
|
(yellow.style.backgroundColor = 'gainsboro');
|
|
|
green.style.backgroundColor = 'green';
|
|
|
- await domEventPromise(btnResolvePromise, 'click', 9000, green);
|
|
|
+ await handleTrafficPromise(btn, 'click', 9000, green);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
pedestrianTrafficLight();
|
|
|
|
|
|
|
|
|
-
|
|
|
-const dataLuke = async () =>
|
|
|
- fetch('https://swapi.py4e.com/api/people/1/')
|
|
|
- .then(res => res.json())
|
|
|
- .then(data => data)
|
|
|
- .catch(() => {});
|
|
|
-
|
|
|
async function speedTest(getPromise, count, parallel = 1) {
|
|
|
- async function makeIterable(promise, n, parallel) {
|
|
|
+ async function makeIterable() {
|
|
|
const arr = [];
|
|
|
- for (let i = 0; i < n; i++) {
|
|
|
+ for (let i = 0; i < count; i++) {
|
|
|
const arrInner = [];
|
|
|
- if (parallel > 1) {
|
|
|
- for (let j = 0; j < parallel; j++) {
|
|
|
- arrInner.push(i + j);
|
|
|
- }
|
|
|
- arr.push(arrInner);
|
|
|
- }
|
|
|
- if (parallel < 2) {
|
|
|
- arr.push(i);
|
|
|
+ for (let j = 0; j < parallel; j++) {
|
|
|
+ arrInner.push([]);
|
|
|
}
|
|
|
+ arr.push(arrInner);
|
|
|
}
|
|
|
return arr;
|
|
|
}
|
|
|
-
|
|
|
const arr = await makeIterable(getPromise, count, parallel);
|
|
|
- let querySpeed = 0;
|
|
|
- let queryDuration = 0;
|
|
|
- let parallelDuration = 0;
|
|
|
- let parallelSpeed = 0;
|
|
|
- const startLoop = performance.now();
|
|
|
+ let duration = performance.now();
|
|
|
await Promise.all(
|
|
|
- arr.map(async el => {
|
|
|
- if (typeof el === 'object') {
|
|
|
- return await Promise.all(el.map(async () => await getPromise()));
|
|
|
- } else {
|
|
|
- return await getPromise();
|
|
|
- }
|
|
|
- }),
|
|
|
- ).then(data => console.log(data, 'data'));
|
|
|
-
|
|
|
- const duration = performance.now() - startLoop;
|
|
|
- querySpeed = duration / count;
|
|
|
- queryDuration = duration / count;
|
|
|
- parallelDuration = duration / count;
|
|
|
- parallelSpeed = duration / (count * parallel);
|
|
|
-
|
|
|
+ arr.map(
|
|
|
+ async el => await Promise.all(el.map(async () => await getPromise())),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ duration = performance.now() - duration;
|
|
|
return {
|
|
|
duration,
|
|
|
- querySpeed,
|
|
|
- queryDuration,
|
|
|
- parallelSpeed,
|
|
|
- parallelDuration,
|
|
|
+ querySpeed: count / duration,
|
|
|
+ queryDuration: duration / count,
|
|
|
+ parallelSpeed: (count * parallel) / (duration / count),
|
|
|
+ parallelDuration: duration / (count * parallel),
|
|
|
};
|
|
|
}
|
|
|
|
|
|
speedTest(() => delay(10000), 10, 10).then(result =>
|
|
|
console.log(result, 'result delay(10000)'),
|
|
|
);
|
|
|
-
|
|
|
speedTest(() => delay(1000), 10, 1).then(result =>
|
|
|
console.log(result, 'result delay(1000)'),
|
|
|
);
|
|
|
-
|
|
|
+const dataLuke = async () =>
|
|
|
+ fetch('https://swapi.py4e.com/api/people/1/')
|
|
|
+ .then(res => res.json())
|
|
|
+ .then(data => data)
|
|
|
+ .catch(() => {});
|
|
|
speedTest(() => dataLuke(), 20, 3).then(result =>
|
|
|
console.log(result, 'result dataLuke'),
|
|
|
);
|