abacus-batch
Batching for async function calls.
This module provides a simple way to implement function call batching. Batching is achieved by wrapping a function in logic that records calls in a batch for some time then calls that function once with the accumulated batch, giving it an opportunity to process all the calls efficiently at once. Unbatching takes a batch of calls, applies then individually to a function, then returns a batch of results.
require('abacus-batch') or require('abacus-batch').batchify
The batch function allows us to delegate the processing of a given function after a number of function calls have occurred.
const batch = ; const sumArray = array; const batchedSum = ; ; ; ;
We can control the amount of time that calls are accumulated before our batched function is called via an additional argument to batch.
const batchedSum = ;
The example above will wait 1 second before calling the lambda.
We can also specify a maximum number of invocations before the batched function is called.
const batchedSum = ;
The function above will wait for 1 second or 10 invocations before it calls the lambda function.
Finally, it is possible to implement a custom count function.
const batchedSum = ;
The above function will wait for 1 second or until the number of arguments of all the accumulated functions is equal to 10
. The name
argument of the last callback is the name of the function.
require('abacus-batch').groupBy
Intended to be used with the batch
function. It allows one to group accumulated calls into buckets and then have each bucket sent to the lambda function.
For example:
const batch = ;const groupBy = groupBy; const sumArray = array; const batchedGroupingSum = ; ; ; ;
require('abacus-batch').unbatchify
This is the reverse of the batchify function. It allows a collection of calls to be split apart, resulting in individual calls.
const unbatch = unbatchify;const sum = { // will be called three times with the following set of arguments // 1. a = 1; b = 2 // 2. a = 5; b = 11 // 3. a = 8; b = 3 ;};const unbatchedSum = ; ;