Say you have these dependencies:
i.e. you must fetch the f2
and f3
data before fetching the f1
data. However f2
and f3
can be fetched in parallel.
fetch-optimizer
take care of your fetchers running them
in parallel when possible.
// fetchers.js ; // define the fetchers. A fetcher must have the following signature:// () => Promiseconst fetchers = ; // define the dependencies between the fetchersconst dependencies = // f1 requires f2 ; // f1 requires also f3 ;
Run:
DEBUG=fetch-optimizer node fetchers.js
Console output:
fetch-optimizer the following fetchers will run in parallel: ["f2","f3"] with input: null +0msfetch-optimizer fetcher `f2` returns: "f2" +53msfetch-optimizer fetcher `f3` returns: "f3" +100msfetch-optimizer the following fetchers will run in parallel: ["f1"] with input: ["f2","f3"] +0msfetch-optimizer fetcher `f1` returns: "f1" +202ms
Algorithm
Say you have these dependencies:
The elements a
and c
have no dependencies so they can run in parallel. After removing them consider the resulting poset:
Iterating gets you the following execution plan: