Start functional programming with FxTs | by Woolim Park | Jan, 2022 | Medium

1. Lazy evaluation support

Try this link: FxTs vs LodashFP vs Ramda performance comparison

First of all, all the functions in FxTs perfectly support the ES6 specification iterator protocols. In other words, It delays the creation of the collection until the last and evaluates it at the end. This brings performance advantages that other libraries do not have, such as lodashFP or Ramda.

If you actually measure the performance, you can see that FxTs is significantly faster than Ramda and slightly faster than Lodash. This difference becomes larger as the size of the array increases.

Performance test Ramda vs lodash vs FxTs

Why there is such a big difference? To investigate it, let’s inserttap function in the middle to know how FxTs works. The tap function is also implemented in Ramda and it’s used when you want to check how the value changes in the middle of function composition.

As you can see that FxTs outputs an iterator, unlike Ramda which outputs an array. That means, Ramda creates an array whenever map, filter are executed, which is why Ramda’s performance is significantly lower than FxTs.

You might notice that there is toArray in FxTs at the last step of function composition. It helps to convert an iterator into an array.

If you want to retrieve the actual value inside the iterator for debugging purposes, you can use the peek function in the middle of piping in FxTs.

However, there is something to be aware of: lazy evaluation is not always performant. If we remove take of the above example, the results are reversed.

This is because toArray function that converts an iterator into an array takes a lot of time. Lazy evaluation is good when you need to perform an operation by extracting only a part of a lot of array data. Otherwise, it is better to use with native JS Array API.

Try this link: FxTs vs LodashFP vs Ramda performance comparison2

Therefore, it is important to know at which moment lazy evaluation can achieve maximum performance.

If you want to know more about how iterator protocol works with FP, I recommend you to read this article.

2. Easy async control

Try this link: asynchronous test Fxts vs LodashFP vs Ramda

FxTs has a super convenient way to deal with async/sync functions. You can easily implement Promise.all In the form of functional programming, and furthermore, it is possible to control how many functions are executed using the Concurrency feature.

LodashFP and Ramda do not support asynchronous functions in the library itself. This is because it is not designed to receive a Promise object when creating a library in the first place. If the code below is executed with lodashFP or Ramda, the Promise object remains in the array.

Async with Ramda example

However, FxTs internally check whether it is a Promiseobject through typeof when retrieving a value from an array through an iterator. If the value is a Promise, FxTs retrieves the value through then.

Async with FxTs example

For this reason, when using other libraries than FxTs, functions must be implemented by yourself to check Promise as follows.

Async with Ramda example 2

FxTs also has a very cool feature Concurrency. In the case of the above example, 5 asynchronous functions are executed simultaneously like Promise.all but if you use the concurrent function, you can control how many asynchronous functions are executed at the same time.

3. Error handling

Try this link: Error handling test FxTs vs LodashFP vs Ramda

Handling errors well is directly related to the robust program. In FxTs, all errors can be handled using try catch

For example, assuming that there is null in the middle of the data array where the object should exist. When accessing the null property in Ramda and lodashFP, the corresponding error does not go to try catchand the library itself breaks down.

4. Easy to use

If you have the basic knowledge of functional programming and understand the iterator protocol, you can start functional programming with FxTs. Since it is designed to implement only for the data type in JS without implementing Functor or Monad separately, it does not require a deep understanding of the relevant library or functional programming, so it is perfect for an FP beginner.

You just need to understand 2 basic concepts in FxTs: Lazy and Strict.

  • Lazy function uses lazy evaluation, returns an iterator, and is used at the beginning or in the middle of function composition.
  • Strict function uses immediate evaluation, returns a value, and is used at the end of function composition.

Let’s take an example.

Lazy functions such as map, filter are used at the beginning or middle of function composition, and Strict functions are used at the last to make meaningful value. You can see the list of Lazy and Strict functions here.

5. Lightweight

FxTs consists of only functions essential for functional programming, so it is suitable for an introduction to functional programming.

It boldly excludes monads and functors that do not exist in Vanilla JavaScript and aims for lightweight functional programming only with basic JavaScript types and Promise. It supports tree-shaking of course.

FxTs has recently been registered in awesome-javascript and it is getting popular. Of course, it is not yet as popular as LodashFP or Ramda, but it is worthy of being widely used considering the above reasons I have mentioned so far.

FxTs is an open-source library developed by marpple dev team, and its popularity is increasing in Korea thanks to developer Indong Yoo’s functional programming lecture.

For those who are interested in FxTs through this article, I have linked the Slack community as a reference. It’s still a fresh library, so if you’d like to contribute, maybe now is a chance

Leave a Comment