Skip to content

Instantly share code, notes, and snippets.

View deepal's full-sized avatar

Deepal Jayasekara deepal

  • CompareTheMarket.com
  • London, United Kingdom
View GitHub Profile
setFakeTime();
const startTimeToD = Date.now(); // set startTime with time-of-day clock
const startTimeMon = performance.now(); // set startTime with monotonic clock
setImmediate(() => {
correctTimeNTP(); // synchronise the clock
});
await doSomething();
@deepal
deepal / time.js
Last active March 15, 2022 20:43
const { execSync } = require("child_process");
const { setTimeout } = require("timers/promises");
const { performance } = require("perf_hooks")
function setFakeTime() {
const toTwoDigits = (num) => num.toString().padStart(2, "0");
const now = new Date();
const month = toTwoDigits(now.getMonth() + 1);
const date = toTwoDigits(now.getDate());
const hours = toTwoDigits(now.getHours());
const client = new MongoClient(MONGODB_CONNECTION_STRING);
await client.connect();
const collection = client.db("myapp").collection("outbox");
/*
watch() function accepts an aggregation pipeline, which can be used to perform
additional aggregation stages. Check out MongoDB aggregation framework for more info:
https://docs.mongodb.com/manual/core/aggregation-pipeline/
*/
const changeStream = collection.watch([
brew uninstall curl
curl -O https://raw.githubusercontent.com/cloudflare/homebrew-cloudflare/69036ea60a2196b71f4ac232b0f1d1062c99eb1a/curl.rb
brew install --HEAD -s curl.rb
# Then, add the path to curl binary to PATH env variable, so that you can 'curl' from anywhere in the terminal.
# For zsh
echo 'export PATH="/usr/local/opt/curl/bin:$PATH"' >> ~/.zshrc
# For bash
const fs = require("fs");
const reader = fs.createReadStream("./foo.txt");
reader.on("open", () => {
console.log("file opened!");
reader.pause()
setTimeout(() => {
reader.resume()
}, 3000) // wait for 3 seconds before
const fs = require('fs')
const original = fs.createReadStream('./original.txt')
const copy1 = fs.createWriteStream('./copy1.txt')
const copy2 = fs.createWriteStream('./copy2.txt')
original.pipe(copy1)
original.pipe(copy2)
DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtrWillBeRawPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
: SuspendableTimer(context)
, m_timeoutID(timeoutID)
, m_nestingLevel(context->timers()->timerNestingLevel() + 1)
, m_action(action)
{
// ... redacted ...
double intervalMilliseconds = std::max(oneMillisecond, interval * oneMillisecond);
if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
intervalMilliseconds = minimumInterval;
function Timeout(callback, after, args, isRepeat, isRefed) {
after *= 1; // Coalesce to number or NaN
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
if (after > TIMEOUT_MAX) {
process.emitWarning(`${after} does not fit into` +
' a 32-bit signed integer.' +
'\nTimeout duration was set to 1.',
'TimeoutOverflowWarning');
}
after = 1; // Schedule on next tick, follows browser behavior
const startHrTime = () => {
if (typeof window !== 'undefined') return performance.now();
return process.hrtime();
}
const getHrTimeDiff = (start) => {
if (typeof window !== 'undefined') return performance.now() - start;
const [ts, tns] = (process.hrtime(start));
return ts * 1e3 + tns / 1e6;
}
Promise.resolve().then(() => console.log('promise1 resolved'));
Promise.resolve().then(() => console.log('promise2 resolved'));
setTimeout(() => {
console.log('set timeout3')
Promise.resolve().then(() => console.log('inner promise3 resolved'));
}, 0);
setTimeout(() => console.log('set timeout1'), 0);
setTimeout(() => console.log('set timeout2'), 0);
Promise.resolve().then(() => console.log('promise4 resolved'));
Promise.resolve().then(() => {