3 ms·
I recently learned a very cute technique for storing arbitrary precision decimals between 0 and 1 as strings. This is useful for representing the index of items
by iudqnolq 3y ago
I recently learned a very cute technique for storing arbitrary precision decimals between 0 and 1 as strings. This is useful for representing the index of items in a reorderable list because you can always make a new index in between two existing indexes (so long as you ensure each index is unique).
Creating a new index in between two indices and comparing two indices are both O(N), but the cute part is that the standard string comparison in JavaScript just works.
You just encode the digits after the decimal point. As in for "0.15" you encode "15". Except you do it in a big base, and to support concurrent edits you add some randomness to the midpoint operation (because this falls apart if two items have identical indexes)
I learned it from Evan Wallace's Blog (Figma, Esbuild). https://madebyevan.com/algos/crdt-fractional-indexing/ https://madebyevan.com/algos/crdt-fractional-indexing/