keidarcy / snippets

personal notes
19 stars 8 forks source link

node internal #9

Open keidarcy opened 2 years ago

keidarcy commented 2 years ago
// loop.js
// node index.js

const pendingTimers = [];
const pendingOSTasks = [];
const pendingOperations = [];

// New timers, tasks, operations are recorded from myFile running.
myFile.runContents();

function shouldContinue() {
  // Check one: Any pending setTimeout, setInterval, setImmediate?
  // Check two: Any pending OS tasks? (Like server listening to port).
  // Check three: Any pending long running operations? (Like fs module).

  return pendingTimers.length || pendingOSTasks.length || pendingOperations.length;
}

// Entire body executes in one 'tick'
while (shouldContinue()) {
  // 1. Node looks at pendingTimers and sees if any functions are ready to be called. setTimeout, setInterval
  // 2. Node looks at pendingOSTasks and calls relevant callbacks.
  // 3. Node looks at pendingOperations and calls relevant callbacks.
  // 4. Pause execution. Continue when...
  // - a timer is about to complete.
  // - a new pendingOSTask is done.
  // - a new pendingOperation is done.
  // 5. Look at pendingTimers. Call any setImmediate.
  // 6. Handle any `close` events.mn
}

// exit back to terminal
// operation.js
process.env.UV_THREADPOOL_SIZE = 5;
const crypto = require('crypto');

const start = Date.now();

crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
  console.log('1:', Date.now() - start);
});

crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
  console.log('2:', Date.now() - start);
});

crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
  console.log('3:', Date.now() - start);
});

crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
  console.log('4:', Date.now() - start);
});

crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
  console.log('5:', Date.now() - start);
});
// ostasks.js
const https = require('https');

const start = Date.now();

function doRequest() {
  const req = https.request('https://google.com', (res) => {
    res.on('data', () => {});
    res.on('end', () => {
      console.log(Date.now() - start);
    });
  });
  req.on('error', (e) => {
    console.error(e);
  });
  req.end();
}

doRequest();
doRequest();
doRequest();
doRequest();
doRequest();
doRequest();
doRequest();
// multitasks.js
process.env.UV_THREADPOOL_SIZE = 1;
const https = require('https');

const crypto = require('crypto');
const fs = require('fs');

const start = Date.now();

function doRequest() {
  const req = https.request('https://google.com', (res) => {
    res.on('data', () => {});
    res.on('end', () => {
      console.log('request', Date.now() - start);
    });
  });
  req.on('error', (e) => {
    console.error(e);
  });
  req.end();
}

function doHash() {
  crypto.pbkdf2('a', 'b', 100_000, 512, 'sha512', () => {
    console.log('hash 1:', Date.now() - start);
  });
}

doRequest();

fs.readFile('large.text', 'utf8', (err, data) => {
  console.log('FS:', Date.now() - start);
});

doHash();
doHash();
doHash();
doHash();