An unreliable and overall unusable sorting library for numbers with a global cache on the edge.
This library implements a number sorting algorithm which is based on time.
Let's say we have an array with three numbers: 200, 100 and 300.
const unsorted = [200, 100, 300];
First, a new, empty array is created.
const sorted = [];
Second, the array with the unsorted numbers gets iterated over and a new timeout is created with the number of the current number as its timeout value.
for (const value of unsorted) {
setTimeout(() => {
sorted.push(value);
}, value);
}
The lowest number gets put into the sorted array first, after that the second-lowest number and so on and on till the sorted array is filled with all numbers in the correct order.
If the option useGlobalEdgeCache
is set to true (which is default), a global
cache on the edge will be used to increase the performance of this algorithm.
After the unsort
method is called, a request will be made to a supabase edge
function which then checks its database, if the unsorted array is already sorted
in the database. If this is the case, the sorted value from the database will be
returned instead of sorting the whole array again.
If the array couldn't be found in the database, it will be sorted in the edge function and saved to the cache database to be available for future sorting requests.
If, for whatever reason, the request to the global cache on the edge fails, the array will be sorted locally, without saving it anywhere to a cache.
The performance depends mostly on the highest number of the array which will be
sorted. If you want to sort the following array: [1, 2, 3000]
the sorting will
take approximately 3000ms
, except it was found in the global cache on the
edge. Then it would take approximately 500ms
.
Because of the spec
of setTimeout
, values beneath 4
will, at least sometimes, not be correctly
sorted. The runtime will do its best to handle these cases, but it will fail
sometimes.
Don't. But if you really want, here you go.
- Import it
import { unsort } from 'https://esm.sh/unsort'
- Profit.
- Install it
pnpm i unsort
- Import it
import { unsort } from 'unsort'
- Profit.
const sorted = await unsort([300, 100, 200]); // results in [100, 200, 300] - hopefully
import { updateGlobalUnsortConfiguration } from "unsort";
updateGlobalUnsortConfiguration({
useGlobalEdgeCache: false,
});
const sorted = await unsort([300, 100, 200], {
useGlobalEdgeCache: false,
});
This package is a deno package which is built with
dnt
to be able to use it in Node.js.
It also contains a supabase edge function which is inside of the supabase folder. The code there is deno as well, because supabase edge functions are built with deno as well.
To build the package to a node compatible one, run deno run -A build.ts
from
the repository root.
The edge function is built with the supabase cli. run
supabase functions deploy unsort-global-edge-cache
from the repository root to
deploy a new version of the edge function to the supabase instance.
To format the code, run the deno formatter deno fmt
from the project root.