preveen-stack / nodejs

0 stars 0 forks source link

async task #7

Open preveen-stack opened 2 months ago

preveen-stack commented 2 months ago
sh-3.2$ cat test.js
const iterations = 10; // Number of iterations

function performTask(iteration) {
  // Simulate some asynchronous task
  setTimeout(() => {
    const end = process.hrtime();
    const executionTime = end[0] * 1000 + end[1] / 1000000; // Convert to milliseconds
    console.log(`Iteration ${iteration} took ${executionTime.toFixed(2)} ms`);
  }, Math.random() * 1000); // Simulate variable execution time
}

for (let i = 0; i < iterations; i++) {
  const start = process.hrtime();
  performTask(i);
  const end = process.hrtime();
  const executionTime = end[0] * 1000 + end[1] / 1000000; // Convert to milliseconds
  console.log(`Scheduling iteration ${i} took ${executionTime.toFixed(2)} ms`);
}

console.log('All iterations scheduled.');
preveen-stack commented 2 months ago
sh-3.2$ node test.js
Scheduling iteration 0 took 781259870.48 ms
Scheduling iteration 1 took 781259879.07 ms
Scheduling iteration 2 took 781259879.15 ms
Scheduling iteration 3 took 781259879.20 ms
Scheduling iteration 4 took 781259879.33 ms
Scheduling iteration 5 took 781259879.47 ms
Scheduling iteration 6 took 781259879.53 ms
Scheduling iteration 7 took 781259879.64 ms
Scheduling iteration 8 took 781259879.79 ms
Scheduling iteration 9 took 781259880.17 ms
All iterations scheduled.
Iteration 5 took 781259978.73 ms
Iteration 3 took 781260138.79 ms
Iteration 4 took 781260173.63 ms
Iteration 1 took 781260178.29 ms
Iteration 9 took 781260428.83 ms
Iteration 0 took 781260504.18 ms
Iteration 2 took 781260689.51 ms
Iteration 7 took 781260745.04 ms
Iteration 8 took 781260789.69 ms
Iteration 6 took 781260856.13 ms