Browse Source

done small fix

unknown 3 năm trước cách đây
mục cha
commit
ad322d81d5
1 tập tin đã thay đổi với 15 bổ sung18 xóa
  1. 15 18
      src/java-script/hw.js

+ 15 - 18
src/java-script/hw.js

@@ -124,41 +124,37 @@ async function speedTest(getPromise, count, parallel = 1) {
       const arrInner = [];
       if (parallel > 1) {
         for (let j = 0; j < parallel; j++) {
-          arrInner.push(promise);
+          arrInner.push(i + j);
         }
         arr.push(arrInner);
       }
       if (parallel < 2) {
-        arr.push(promise);
+        arr.push(i);
       }
     }
     return arr;
   }
 
-  const readyArr = await makeIterable(getPromise, count, parallel);
+  const arr = await makeIterable(getPromise, count, parallel);
   let querySpeed = 0;
   let queryDuration = 0;
   let parallelDuration = 0;
   let parallelSpeed = 0;
   const startLoop = performance.now();
-  await Promise.all([
-    ...readyArr.map(async el => {
+  await Promise.all(
+    arr.map(async el => {
       if (typeof el === 'object') {
-        const start = await performance.now();
-        await Promise.all(el.map(async () => await getPromise()));
-        parallelDuration = (await performance.now()) - start;
-        return;
+        return await Promise.all(el.map(async () => await getPromise()));
       } else {
-        const start = await performance.now();
-        await getPromise();
-        queryDuration = (await performance.now()) - start;
-        return;
+        return await getPromise();
       }
     }),
-  ]).then(data => console.log(data, 'data'));
+  ).then(data => console.log(data, 'data'));
 
   const duration = performance.now() - startLoop;
-  querySpeed = duration / (count * parallel);
+  querySpeed = duration / count;
+  queryDuration = duration / count;
+  parallelDuration = duration / count;
   parallelSpeed = duration / (count * parallel);
 
   return {
@@ -171,12 +167,13 @@ async function speedTest(getPromise, count, parallel = 1) {
 }
 
 speedTest(() => delay(10000), 10, 10).then(result =>
-  console.log(result, 'result'),
+  console.log(result, 'result delay(10000)'),
 );
 
 speedTest(() => delay(1000), 10, 1).then(result =>
-  console.log(result, 'result'),
+  console.log(result, 'result delay(1000)'),
 );
+
 speedTest(() => dataLuke(), 20, 3).then(result =>
-  console.log(result, 'result'),
+  console.log(result, 'result dataLuke'),
 );