Skip to content

event loop examples #2

Description

@preveen-stack
// 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.');

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions