Skip to content

Instantly share code, notes, and snippets.

View ardislu's full-sized avatar

Ardis Lu ardislu

View GitHub Profile
@ardislu
ardislu / garbageCollection.js
Created September 21, 2025 06:33
The minimal logic to block until garbage collection occurs in JavaScript.
/**
* Helper function that will block until garbage collection is performed.
*
* **WARNING:** By design, JavaScript garbage collection is unpredictable and may never happen (i.e., this
* function may never resolve). Use this function with caution.
*/
async function garbageCollection() {
const { promise, resolve } = Promise.withResolvers();
const registry = new FinalizationRegistry(resolve);
(() => registry.register({}, ''))();
@ardislu
ardislu / minimal-webauthn-prf.js
Last active July 31, 2025 07:35
Minimal boilerplate JavaScript code to use the pseudo-random function extension (prf) of the Web Authentication API.
/**
* =================================================== IMPORTANT ===================================================
*
* The passkey creation and retrieval MUST occur in the same "effective domain" or "registrable domain suffix"
* ([more info](https://www.w3.org/TR/webauthn-3/#rp-id)):
*
* > For example, given a Relying Party whose origin is `https://login.example.com:1337`, then the following
* > RP IDs are valid: `login.example.com` (default) and `example.com`, but not `m.login.example.com` and not `com`.
*
* As a result of this restriction, data encrypted with a crypto key that was derived from a passkey's PRF extension

How to fix winget taking a long time to extract archive

Problem

Using winget to install programs with large archives (e.g., zig.zig) takes a long time and appears to hang:

PS> winget install zig.zig
Found Zig [zig.zig] Version 0.14.0
This application is licensed to you by its owner.

Modern versions of WSL2 can run GUI applications out of the box via WSLg.

  1. Install desired GUI application:
sudo apt install nautilus -y
  1. Run it:
@ardislu
ardislu / webStreamsQueuingStrategy.js
Last active March 19, 2025 05:44
Demonstration of queuing strategy for Streams API
// Demonstration of queuing strategy for Streams API
// $ node ./webStreamsQueuingStrategy.js
export { }; // Intentional unused export to force "type: module" on .js file
// These strategies are equivalent
const basicStrategy = {
highWaterMark: 3, // If no .size() specified, default is .size() === 1 (CountQueuingStrategy)
// size(chunk) { return 1; } // Implied
};
@ardislu
ardislu / getPseudoRandomValues.js
Last active January 3, 2025 06:47
Simple, small, and fast pseudorandom number generator to deterministically generate large amounts of mock test data.
/**
* Simple, small, and fast pseudorandom number generator to deterministically generate large amounts of mock test data.
*
* API is intended to follow [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues).
*
* Hash function source:
* - https://burtleburtle.net/bob/hash/integer.html
* - https://web.archive.org/web/20090408063205/http://www.cris.com/~Ttwang/tech/inthash.htm
* @param {ArrayBufferView<ArrayBufferLike>} typedArray An integer-based `TypedArray` with a byte length that is a multiple of 4.
* All elements in the array will be overwritten with random numbers.

How to change your last git commit's CommitDate and AuthorDate

  1. Show the last git commit and confirm it's the one you want to edit:
$ git log -n 1 --pretty=fuller
commit 0000000000000000000000000000000000000000 (HEAD -> main)
Author:     First Last <[email protected]>
AuthorDate: Tue Nov 26 01:01:01 2024 -0800
Commit: First Last 
@ardislu
ardislu / debounce.js
Created September 13, 2024 02:28
Minimal debounce function and example in JavaScript.
// Minimal debounce function:
function debounce(fn, wait) {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), wait);
}
}
// Minified:

Beware unexpected overloading in JavaScript array functions

What's the difference between these two?

['1', '2', '3'].map(n => parseInt(n)); // Function 1
['1', '2', '3'].map(parseInt); // Function 2

Let's see the output:

@ardislu
ardislu / minimal-form-values.html
Last active September 1, 2024 23:43
A minimal example of the most concise way to get the values from a vanilla HTML `<form>` element and assign the values to JavaScript variables in 2024. Notice that the object keys are based on the `<input>`'s `name` attribute and not `id`.
<form>
<input name="ex1">
<input name="ex2">
<input name="example-with-hypens">
<button>Submit</button>
</form>
<script>
document.querySelector('form').addEventListener('submit', e => {
e.preventDefault();