- Spawning an isolated Node.js environment for running a given code
- Accurately timing the execution of a given code (bypassing JIT optimizations which would lead to a biased timing)
- Accurately comparing the timing between two or more algorithms (also, bypassing JIT optimizations which would lead to a biased timing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A reasonably-efficient approach that uses "DMA" ("Direct Memory Access"), avoids multiple allocations (well, except for stack array) and (ab)uses mathematics | |
// In my tests, permutateBuffer is the fastest among the three. | |
const permutateBuffer = function(amount){ | |
// It *should* work for browsers if Buffer is replaced with Uint8Array | |
if(isNaN(amount) || amount < 1 || amount > 10) // If you have sufficient memory and V8 is configured with enough heap, 10 can be increased | |
return Buffer.alloc(0) | |
let expected = 1 | |
let carrier = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: I'm ignoring fractional bytes (e.g., 4.2 B is invalid input; 4.2 KiB or 4.2 KB are valid, tho ). | |
export const humanizeBytes = function(bytes, si = false, digits = 1){ | |
let basis = si ? 1000 : 1024 | |
let scale = (Math.log(Math.max(1,bytes)) / Math.log(basis))|0 | |
let coeff = bytes / (basis ** scale) | |
let unit = scale == 0 ? "B" : ("KMGTP"[scale-1] + (si ? "" : "i") + "B") | |
return `${coeff.toFixed(scale == 0 ? 0 : digits)} ${unit}` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function waitUntilDatetime(timestamp, maxWait = 86400000) { | |
let beginAt = Date.now() | |
// Something scheduled should wait less than 24 hours, or 86400000 ms. | |
// After that point, it'll take too damn long, even for a daemon, so it should be refused. | |
if((timestamp - beginAt) > maxWait) // Refuse | |
throw new Error(`Requested to wait for ${timestamp - beginAt} ms., which exceeds the limit of ${maxWait} ms.`) | |
while(Date.now() < timestamp) | |
await sleep(Math.min(timestamp - Date.now(), 10000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Intended usage: | |
// $ pacman -Qi | node index.mjs | |
// I know there are (plenty of) existing solutions to this. I just made my own. | |
import { stdin, stdout } from "process" | |
import { Console, log } from "console" | |
import { createWriteStream, writeFileSync } from "fs" | |
import { writeFile } from "fs/promises" | |
const sleep = ms => new Promise(r => setTimeout(r, ms)) |
Actually, none (do things really need to have a purpose?)... Just another tinkering with randomness.
- Generates a list of random L numbers (currently 100 numbers).
- Maps the current list so every number (expectedly 0 <= n <= 1) becomes an index i (i = round(n × L)) pointing to the ith number in the current list.
- Calculates the current diversity (i.e., how many unique numbers survived from the last iteration).
- If diversity converges to less than 100 after a couple of runs, goes back to the step 1.
- If diversity converges to exactly 100 after a couple of runs, prints "Got the best of all!" and exits the loop.
- If diversity hasn't converged yet, goes back to the step 2.
Initial development of the bitwise approach, debugging and testing it against a lazy-man approach. Uses cli-table and chalk as dependencies for better visualization.
Refined and modularized code, containing just the bitwise approach as a function, followed by an invocation example.
- Base64 encoding/decoding
- Perhaps experimentation with baudot code?
(0..63).map{|i|(((1+i/26)<<5)-i/52*81+i%26+i/62*(3*(i&1)-15)+33).chr}.join
The compactiest formula, with just 47 characters designed to calculate 64 characters. The entire one-liner has 74 characters.
"".join([chr(((1+i//26)<<5)-(i//52*81)+i%26+(i//62*(3*(i&1)-15))+33) for i in range(0,64)])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canvas = document.querySelector("canvas") | |
const ctx = canvas.getContext("2d") | |
const table = await (await fetch("/data/8859-1.txt")).text() // Copied from Wikipedia's table | |
const idxTable = [] | |
let idx = 0 | |
for(let row of table.split("\n")){ | |
for(let col of row.split("\t").slice(1)){ | |
let uniChar = String.fromCharCode(idx) | |
if(uniChar != col) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {log} from "console" | |
const range = (n,x)=>Array.from(Array(Math.max(0,x-n+1))).map((_,i)=>i+n) | |
const numerals = "zero|one|two|three|four|five|six|seven|eight|nine".split("|") | |
const alpha = " "+String.fromCharCode(...range(96+1, 96+26)) | |
for(let numeral of numerals){ | |
let chain = [numeral] | |
let cycled = false |
NewerOlder