GithubHelp home page GithubHelp logo

ajitjha393 / js-transducers Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 17 KB

The Functional transducers that allow transformations with improved performance

JavaScript 100.00%
compose functional-programming transducers

js-transducers's Introduction

js-transducers

The Functional and Declarative Implementation of map, reduce, filter were added to JS in ES2015 to Array.Prototype.

Since then we use them to perform all sorts of operations, filtering, and transformation declaratively on Array for manipulating data.

But all these operations are independent of each other, which means we produce an intermediate resultant array which is passed as an input and we loop over it again in the next chained operation.

timeIt('million - chained', () => {
    arrOfMillion
      .map(multiplyByThree)
      .filter(isEven);
  });

timeIt('million - chained x2', () => {
  arrOfMillion
    .map(multiplyByThree)
    .map(multiplyByThree)
    .filter(isEven);
});

This implementation and existing behavior works really well in our normal day to day usage involving very smaller amount of data values. But what if we need to perform these operations and transformations multiple times over millions of data values.

We see a significant performance degradation.

Imperative Solution

Now we can go back to the imperative solution but will loose all the benefits that Declarative and Functional Programming provided us.

The Truthness and Referentiality of the codebase will not be maintained

timeIt('million - imperative', () => {
  const result = [];
  arrOfMillion
    .forEach(val => {
      const tripled = tripleIt(val);
      if (isEven(tripled)) result.push(tripled);
    });
});

Is there a way where I can get best of both the worlds?

And do not have to tradeoff on performance?

And the answer is YES! - Using transducers

Transducer Solution

I implemented these transducers and other functional adapter and helpers - into and seq

Using this utiltiy we can rewrite the above mentioned code as -

timeIt('million - transduce', () => {
  seq(
    compose(
      map(multiplyByThree),
      filter(isEven),
    ),
    arrOfMillion,
  );
});
 

timeIt('million - transduce x2', () => {
  seq(
    compose(
      map(multiplyByThree),
      map(multiplyByThree),
      filter(isEven),
    ),
    arrOfMillion,
  );
});
 

timeIt('million - transduce x4', () => {
  seq(
    compose(
      map(multiplyByThree),
      map(multiplyByThree),
      map(multiplyByThree),
      map(multiplyByThree),
      filter(isEven),
    ),
    arrOfMillion,
  );
});

Measuring the Performance of our Transducers

As it is evident from the metric, the transducers allow us to perform all these operations and transformations in a much performant way when dealing with huge amount of data and values that we want to iterate over.

It also enables us with more flexibility over the type of collection as it is agnostic to the container and not limited to only using Arrays.

image

js-transducers's People

Contributors

ajitjha393 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.