1.0.2 • Published 4 years ago

obsducer v1.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

obsducer

Motivation

Install

$ npm i obsducer

How it Works

Usage

Why you'd use obsducer:

1) You have many array transformations that could be generalized and composed 2) You are transforming a very big array 3) Both of the above

Ways to use obsducer:

1) executeObsducer: create observable from array, pipe all the operators you need, and immediately execute. Do this if you know you're only going to use this exact transducer/transformation in one place. You still get the performance advantage over native array method chaining, but you lose a little bit of (subjective) readability.

import { executeObsducer } from "obsducer";
import { from } from "rxjs"
import { map, filter, take } from "rxjs/operators";

// using internal execution function
const result = executeObsducer(
    from(beforeArray).pipe(
        map(addOne), 
        filter(isGreaterThanThree),
        take(2), 
        map(addOne), 
        map(numberToString)
    )
  );

2)

import createObsducer from "obsducer";
import { pipe } from "rxjs";
import { map, filter, take } from "rxjs/operators";

// projections, predicates
const addOne = (x: number) => x + 1;
const isGreaterThanThree = (x: number) => x > 3;
const numberToString = (x: number) => x.toString();

const firstPipedOps = pipe(
    map(addOne), 
    filter(isGreaterThanThree)
);

const secondPipedOps = pipe(
    take(2), 
    map(addOne), 
    map(numberToString)
);

const bothPipedOps = pipe(
    firstPipe, 
    secondPipe
);

const beforeArrayA = [1, 2, 3, 4, 5];
const beforeArrayB = [0, 1, 2, 3, 4];

// pass different pipes and combinations of pipe to create reusable transducers
const obsducer1 = createObsducer(firstPipedOps);
const obsducer2 = createObsducer(secondPipedOps);
const obsducer3 = createObsducer(bothPipedOps);

// result1A: [4, 5, 6]
const result1A = obsducer1(beforeArrayA);
const result1B = obsducer1(beforeArrayB);
// result2A: ["1", "2"];
const result2A = obsducer2(beforeArrayA);
const result2B = obsducer2(beforeArrayB);
// result3A: []
const result3A = obsducer3(beforeArrayA);
const result3B = obsducer3(beforeArrayB);

Performance

Benchmark code: src/benchmark.ts

Performing 4 operations on an array with 10,000,000 elements is almost as fast on average with an obsducer as with a for loop. On many iterations of my benchmark, the obsducer method was actually faster than the for loop. The small overhead in creating an observable has no statistically significant impact on performance compared to for loops for huge arrays.

For arrays with 100,000 elements, the obsducer method is slightly slower than a for loop. This is probably due to However, it still easily beats array method chaining performance-wise, and .

You probably wouldn't be thinking about optimizing your code when manipulating arrays of 10 elements... but if you find yourself in that situation, you should stick to native array method chaining or loops.

API Reference

Credits/Prior Art

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago