Skip to content

Instantly share code, notes, and snippets.

@DLiblik
DLiblik / areEquivalent.ts
Created March 15, 2024 18:35
Compares two JS (TS) objects for equivalency of values. Priority is on speed of execution. Safe for objects with circular references.
/**
Fast compare of two items (values or references) for nested equivalency.
Does NOT use stringify and is robust to key sequence.
Fast fails on first difference found.
Equivalence means that at root and at each key or index they are equivalent
as follows:
- If a value type, values are either hard equal (===) or are both NaN
(different than JS where NaN !== NaN)
@DLiblik
DLiblik / svelt-pausable-writable-store.ts
Last active March 15, 2024 18:06
Drop-in extension of Svelte's writable store that can be paused and resumed, where resuming the store causes only the latest update to the store to be published to subscribers (or none at all if no updates were made while paused).
import { get, writable, type Updater, type Writable, type Subscriber,
type Unsubscriber, type Invalidator } from "svelte/store";
/** A handle returned from a call to {@link PausableWritable.pause} */
export type PauseHandle = {};
/**
A {@link Writable} that supports pausing and resuming store subscription
updates, enabling throttling of high-volume subscription notifications if
a large number of individual changes are to occur together and intermiediary
@DLiblik
DLiblik / areEquivalent.js
Last active October 13, 2023 14:23
Fast JS function for testing if Javascript values/objects are equivalent or equal - guards against circular references, prioritizes speed.
/**
Compares two items (values or references) for nested equivalency, meaning that
at root and at each key or index they are equivalent as follows:
- If a value type, values are either hard equal (===) or are both NaN
(different than JS where NaN !== NaN)
- If functions, they are the same function instance or have the same value
when converted to string via `toString()`
- If Date objects, both have the same getTime() or are both NaN (invalid)
- If arrays, both are same length, and all contained values areEquivalent