fetch-optimizer

Optimizes dependent data fetchers

Usage no npm install needed!

<script type="module">
  import fetchOptimizer from 'https://cdn.skypack.dev/fetch-optimizer';
</script>

README

Say you have these dependencies:

example

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

import { Poset, optimize } from 'fetch-optimizer';

// define the fetchers. A fetcher must have the following signature:
// () => Promise
const fetchers = {
  f1: () => new Promise(resolve => setTimeout(resolve('f1'), 200)),
  f2: () => new Promise(resolve => setTimeout(resolve('f2'), 50)),
  f2: () => new Promise(resolve => setTimeout(resolve('f3'), 150))
};

// define the dependencies between the fetchers
const dependencies = new Poset()
  .addEdge('f1', 'f2')  // f1 requires f2
  .addEdge('f1', 'f3'); // f1 requires also f3

optimize(dependencies, fetchers).then(() => {
    // tasks completed...
});

Run:

DEBUG=fetch-optimizer node fetchers.js

Console output:

fetch-optimizer the following fetchers will run in parallel: ["f2","f3"] with input: null +0ms
fetch-optimizer fetcher `f2` returns: "f2" +53ms
fetch-optimizer fetcher `f3` returns: "f3" +100ms
fetch-optimizer the following fetchers will run in parallel: ["f1"] with input: ["f2","f3"] +0ms
fetch-optimizer fetcher `f1` returns: "f1" +202ms

Algorithm

Say you have these dependencies:

ante

The elements a and c have no dependencies so they can run in parallel. After removing them consider the resulting poset:

rest

Iterating gets you the following execution plan:

post