preveen-stack / Learning

0 stars 0 forks source link

event loop examples #2

Open preveen-stack opened 1 year ago

preveen-stack commented 1 year ago
// 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 1 year ago
Scheduling iteration 0 took 429984527.64 ms
Scheduling iteration 1 took 429984533.96 ms
Scheduling iteration 2 took 429984534.03 ms
Scheduling iteration 3 took 429984534.09 ms
Scheduling iteration 4 took 429984534.13 ms
Scheduling iteration 5 took 429984534.16 ms
Scheduling iteration 6 took 429984534.33 ms
Scheduling iteration 7 took 429984534.37 ms
Scheduling iteration 8 took 429984534.45 ms
Scheduling iteration 9 took 429984534.65 ms
All iterations scheduled.
Iteration 2 took 429984703.12 ms
Iteration 0 took 429984710.74 ms
Iteration 3 took 429984789.46 ms
Iteration 8 took 429984868.49 ms
Iteration 5 took 429984937.23 ms
Iteration 7 took 429985132.87 ms
Iteration 6 took 429985170.58 ms
Iteration 1 took 429985282.65 ms
Iteration 4 took 429985440.12 ms
Iteration 9 took 429985472.80 ms
preveen-stack commented 1 year ago
const iterations = 10; // Number of iterations

function performTask(iteration) {
  // Simulate some asynchronous task
  setTimeout(() => {
    console.log(`Iteration ${iteration} completed.`);
    setImmediate(() => {
      const end = new Date();
      console.log(`Time after iteration ${iteration}: ${end.toISOString()}`);
    });
  }, Math.random() * 1000); // Simulate variable execution time
}

for (let i = 0; i < iterations; i++) {
  performTask(i);
}

console.log('All iterations scheduled.');
preveen-stack commented 1 year ago
All iterations scheduled.
Iteration 3 completed.
Time after iteration 3: 2023-09-05T05:41:45.292Z
Iteration 7 completed.
Time after iteration 7: 2023-09-05T05:41:45.424Z
Iteration 5 completed.
Time after iteration 5: 2023-09-05T05:41:45.462Z
Iteration 4 completed.
Time after iteration 4: 2023-09-05T05:41:45.570Z
Iteration 6 completed.
Time after iteration 6: 2023-09-05T05:41:45.709Z
Iteration 1 completed.
Time after iteration 1: 2023-09-05T05:41:45.768Z
Iteration 2 completed.
Time after iteration 2: 2023-09-05T05:41:45.776Z
Iteration 9 completed.
Time after iteration 9: 2023-09-05T05:41:45.853Z
Iteration 0 completed.
Time after iteration 0: 2023-09-05T05:41:46.018Z
Iteration 8 completed.
Time after iteration 8: 2023-09-05T05:41:46.115Z
preveen-stack commented 1 year ago
const iterations = 30; // Number of iterations

function performTask(iteration) {
  const start = process.hrtime();

  // Simulate some asynchronous task using process.nextTick
  process.nextTick(() => {
    const end = process.hrtime(start);
    const executionTime = end[0] * 1000 + end[1] / 1000000; // Convert to milliseconds
    console.log(`Iteration ${iteration} took ${executionTime.toFixed(2)} ms`);
  });
}

for (let i = 0; i < iterations; i++) {
  performTask(i);
}

console.log('All iterations scheduled.');
preveen-stack commented 1 year ago
All iterations scheduled.
Iteration 0 took 4.97 ms
Iteration 1 took 4.94 ms
Iteration 2 took 4.99 ms
Iteration 3 took 5.02 ms
Iteration 4 took 5.05 ms
Iteration 5 took 5.09 ms
Iteration 6 took 5.12 ms
Iteration 7 took 5.14 ms
Iteration 8 took 5.28 ms
Iteration 9 took 5.41 ms
Iteration 10 took 5.45 ms
Iteration 11 took 5.56 ms
Iteration 12 took 5.61 ms
Iteration 13 took 5.64 ms
Iteration 14 took 5.68 ms
Iteration 15 took 5.70 ms
Iteration 16 took 5.78 ms
Iteration 17 took 5.81 ms
Iteration 18 took 5.84 ms
Iteration 19 took 5.85 ms
Iteration 20 took 5.77 ms
Iteration 21 took 5.77 ms
Iteration 22 took 5.79 ms
Iteration 23 took 5.88 ms
Iteration 24 took 5.91 ms
Iteration 25 took 5.92 ms
Iteration 26 took 5.94 ms
Iteration 27 took 5.95 ms
Iteration 28 took 5.97 ms
Iteration 29 took 5.98 ms
preveen-stack commented 1 year ago
let iteration = 0;

function iterateEventLoop() {
  iteration++;
  console.log(`Event loop iteration ${iteration} completed.`);
  // Continue iterating until a certain condition is met
  if (iteration < 10) {
    setImmediate(iterateEventLoop);
  } else {
    console.log('Finished iterating the event loop.');
  }
}

iterateEventLoop();
preveen-stack commented 1 year ago
Event loop iteration 1 completed.
Event loop iteration 2 completed.
Event loop iteration 3 completed.
Event loop iteration 4 completed.
Event loop iteration 5 completed.
Event loop iteration 6 completed.
Event loop iteration 7 completed.
Event loop iteration 8 completed.
Event loop iteration 9 completed.
Event loop iteration 10 completed.
Finished iterating the event loop.
preveen-stack commented 1 year ago
let iteration = 0;

function iterateEventLoop() {
  iteration++;
  const currentTime = new Date().toLocaleTimeString();
  console.log(`Event loop iteration ${iteration} completed at ${currentTime}.`);

  // Continue iterating until a certain condition is met
  if (iteration < 10) {
    setImmediate(iterateEventLoop);
  } else {
    console.log('Finished iterating the event loop.');
  }
}

iterateEventLoop();
preveen-stack commented 1 year ago
Event loop iteration 1 completed at 11:14:00 AM.
Event loop iteration 2 completed at 11:14:00 AM.
Event loop iteration 3 completed at 11:14:00 AM.
Event loop iteration 4 completed at 11:14:00 AM.
Event loop iteration 5 completed at 11:14:00 AM.
Event loop iteration 6 completed at 11:14:00 AM.
Event loop iteration 7 completed at 11:14:00 AM.
Event loop iteration 8 completed at 11:14:00 AM.
Event loop iteration 9 completed at 11:14:00 AM.
Event loop iteration 10 completed at 11:14:00 AM.
Finished iterating the event loop.
preveen-stack commented 1 year ago
let iteration = 0;

function iterateEventLoop() {
  iteration++;
  const now = new Date();
  const currentTime = `${now.toLocaleTimeString()}.${now.getMilliseconds()}`;
  console.log(`Event loop iteration ${iteration} completed at ${currentTime}.`);

  // Continue iterating until a certain condition is met
  if (iteration < 10) {
    process.nextTick(iterateEventLoop);
  } else {
    console.log('Finished iterating the event loop.');
  }
}

iterateEventLoop();
preveen-stack commented 1 year ago
Event loop iteration 1 completed at 11:14:34 AM.655.
Event loop iteration 2 completed at 11:14:34 AM.675.
Event loop iteration 3 completed at 11:14:34 AM.675.
Event loop iteration 4 completed at 11:14:34 AM.675.
Event loop iteration 5 completed at 11:14:34 AM.675.
Event loop iteration 6 completed at 11:14:34 AM.675.
Event loop iteration 7 completed at 11:14:34 AM.675.
Event loop iteration 8 completed at 11:14:34 AM.675.
Event loop iteration 9 completed at 11:14:34 AM.675.
Event loop iteration 10 completed at 11:14:34 AM.675.
Finished iterating the event loop.