Skip to content

Instantly share code, notes, and snippets.

View pi0's full-sized avatar
๐Ÿ‘€

Pooya Parsa pi0

๐Ÿ‘€
View GitHub Profile
@pi0
pi0 / getBuiltinModule-polyfill.js
Last active October 7, 2025 19:59
`process.getBuiltinModule` polyfill
import { createRequire, builtinModules } from 'node:module'
// Node.js below v22.3.0, v20.16.0
if (!globalThis.process.getBuiltinModule) {
const _require = createRequire(import.meta.url)
globalThis.process.getBuiltinModule = (name) => {
if (name.startsWith("node:") || builtinModules.includes(name)) {
return _require(name)
}
}
// list-notifications.js
const API = "https://api.github.com/notifications";
const TOKEN = process.env.GITHUB_TOKEN;
if (!TOKEN) {
console.error("Set GITHUB_TOKEN (classic PAT with `notifications` scope).");
process.exit(1);
}
const args = process.argv.slice(2);
@pi0
pi0 / results.txt
Last active August 27, 2025 01:57
Thenable vs Promise instance check
clk: ~4.24 GHz
cpu: Apple M4 Pro
runtime: node 22.18.0 (arm64-darwin)
benchmark avg (min โ€ฆ max) p75 / p99 (min โ€ฆ top 1%)
------------------------------------------------- -------------------------------
โ€ข single check per iteration
------------------------------------------------- -------------------------------
instanceof Promise 1.55 ns/iter 1.59 ns 2.07 ns โ–ƒโ–ˆโ–‚โ–‡โ–„โ–โ–‚โ–โ–โ–โ–
typeof x.then === "function" 3.72 ns/iter 3.92 ns 4.87 ns โ–โ–‚โ–„โ–ˆโ–ƒโ–†โ–ƒโ–โ–โ–โ–
export default {
async fetch(req: Request): Promise<Response> {
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
for (const token of getTokens()) {
controller.enqueue(encoder.encode(token + " "));
await new Promise((resolve) => setTimeout(resolve, 25));
}
controller.close();
const nuxtBuildMonitor = async (_, nuxt) => {
const timings = {} as Record<string, number>;
const os = await import('node:os');
const fs = await import('node:fs');
fs.writeFileSync('timings.log', '', 'utf8');
const log = (message: string) => {
const timeTag = `[${new Date().toLocaleTimeString()}]`;
import { bench, summary, run } from "mitata";
import { createStorage, defineDriver } from "unstorage";
import fsLite from "unstorage/drivers/fs-lite";
const denoDriver = defineDriver((opts) => {
return {
async getItem(key: string) {
const val = await Deno.readTextFile(`${opts.base}/${key}`);
return val;
},
function reportRollupTiming(build) {
if (!build.getTimings) {
console.log(
"Rollup timing information is not available. Make sure `perf: true` config is set."
);
return;
}
const formatTime = (ms) =>
ms > 1000 ? `${(ms / 1000).toLocaleString()}s` : `${ms.toLocaleString()}ms`;
const timings = build.getTimings();
# The Lazy Coder's Guide to Programming
## Chapter 1: The Art of Copy-Pasting
### Section 1.1: Ctrl+C, Ctrl+V, Repeat
Programming can be hard. But fear not! With the power of copy-paste, you can conquer any coding challenge without breaking a sweat. Just remember: if it works once, it'll work a thousand times. Who needs original code anyway?
## Chapter 2: Debugging 101: Blame the Compiler
@pi0
pi0 / to-pnpm.mjs
Last active March 15, 2022 20:15
Script to migrate to pnpm
#!/bin/env node
import { existsSync } from 'fs'
import fs from 'fs/promises'
import path from 'path'
import { execSync } from 'child_process'
const LATEST_PNPM = '6.32.3'
const dir = path.resolve(process.argv[2] || '.')
const resolve = (...p) => path.resolve(dir, ...p)
@pi0
pi0 / next.config.js
Created March 12, 2021 17:29
Webpackbar with Next.js
const Webpackbar = require('webpackbar')
module.exports = {
webpack: (config, { isServer }) => {
config.plugins.push(new Webpackbar({ name: isServer ? 'server' : 'client' }))
return config
}
}