Browse Source

homework14 update done

holevchuk.evgeny 2 years ago
parent
commit
94bf91867c
2 changed files with 13 additions and 10 deletions
  1. 1 1
      hw14/PedestrianTrafficLight-stage2_3.js
  2. 12 9
      hw14/speedtest.js

+ 1 - 1
hw14/PedestrianTrafficLight-stage2_3.js

@@ -30,7 +30,7 @@ const addDomElement = (element) => {
 	}
 }
 
-const trafficLight = async (domElement, delayGreen, delayYellow, delayRed) =>{
+const trafficLight = async (domElement, delayGreen, delayYellow, delayRed) => {
 	addDomElement(domElement);
 	while (true){
 		toggleLight(false,'.red');

+ 12 - 9
hw14/speedtest.js

@@ -2,17 +2,19 @@ const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
 
 const speedtest = async (getPromise, count, parallel= 1) => {
 	const t0 = performance.now();
-	for (let i = 1; i <= parallel; i++) {
-		for (let i = 1; i <= count; i++) {
-			await getPromise();
+	for (let i = 1; i <= count; i++) {
+		let arr = [];
+		for (let j = 1; j <= parallel; j++) {
+			arr.push(getPromise());
 		}
+		await Promise.all(arr);
 	}
 	const t1 = performance.now();
-	let parallelDuration = parallel * count;
-	let duration = t1 - t0;
-	let parallelSpeed = parallelDuration / duration;
-	let queryDuration = duration / count;
-	let querySpeed = count / duration;
+	const parallelDuration = parallel * count;
+	const duration = t1 - t0;
+	const parallelSpeed = parallelDuration / duration;
+	const queryDuration = duration / count;
+	const querySpeed = count / duration;
 	return {
 		duration,
 		querySpeed,
@@ -22,4 +24,5 @@ const speedtest = async (getPromise, count, parallel= 1) => {
 	}
 }
 
-speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result));
+speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result));
+speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 2, 2);