Linked and Detached Sequencing


Terminology

Elements in a detached sequence are created without any dependency between them.

In a linked sequence elements are created with one or both of the dependency types:

  • element's index in the sequence
  • previous element in the sequence

Method sequence supports each of the three sequence variations.

Example

Let's implement a linked sequence that calculates primes from the previous result.

function nextPrime(value) {
    if (value > 2) {
        let i, q;
        do {
            i = 3;
            value += 2;
            q = Math.floor(Math.sqrt(value));
            while (i <= q && value % i) {
                i += 2;
            }
        } while (i <= q);
        return value;
    }
    return value === 2 ? 3 : 2;
}

Building a sequence of the first 10 primes directly:

const result = [];
let value;
for (let i = 0; i < 10; i++) {
    value = nextPrime(value);
    result.push(value);
}
console.log('Result:', result);

we get the following output:

Result: [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]

Now let's create the same sequence using spex:

function source(index, data) {
    return nextPrime(data);
}

spex.sequence(source, {limit: 10, track: true})
    .then(data => {
        console.log('Result:', data);
    });

It will produce the identical output as before:

Result: [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]

Benchmarks

In the example above our source function returns a number directly, but it can be any mixed value.

Let's see how the sequence performs compared to the direct calculation as we increase the sequence size.

10 100 1,000 10,000 100,000 1,000,000
direct 0.00 0.00 0.14 4.56 143 4,650
number   0.02/0.02   0.18/0.12   1.89/1.08   22.5/13.8   306/227   5,985/5,192
promise   0.02/0.01   0.17/0.11   1.85/1.08   21.5/13.7   306/228   5,990/5,172
  • direct - direct sequence calculation;
  • number - using sequence with the source that returns numbers:
function source(index, data) {
    return nextPrime(data);
}
  • promise - using sequence with the source that returns promises:
function source(index, data) {
    return Promise.resolve(nextPrime(data));
}
  • All values are given in milliseconds;
  • Two measurements for the sequence: ES6 Promise / Bluebird
  • Measured under NodeJS 4.1.1, 64-bit, with i7-4770 @ 3.85GHz