<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rasmusvhansen</title>
    <description>The latest articles on DEV Community by rasmusvhansen (@rasmusvhansen).</description>
    <link>https://dev.to/rasmusvhansen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33609%2F03c5fbbb-6e83-4ac5-b9db-8d84c9504669.jpeg</url>
      <title>DEV Community: rasmusvhansen</title>
      <link>https://dev.to/rasmusvhansen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rasmusvhansen"/>
    <language>en</language>
    <item>
      <title>RxJS Transducer - Harness the Power of RxJS Operators</title>
      <dc:creator>rasmusvhansen</dc:creator>
      <pubDate>Mon, 28 Jan 2019 20:19:17 +0000</pubDate>
      <link>https://dev.to/rasmusvhansen/rxjs-transducer---harness-the-power-of-rxjs-operators-1ai8</link>
      <guid>https://dev.to/rasmusvhansen/rxjs-transducer---harness-the-power-of-rxjs-operators-1ai8</guid>
      <description>&lt;p&gt;&lt;em&gt;Use the well known RxJS operators to manipulate arrays or iterables in a blazing fast way using the new tiny library rxjs-transducer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most JavaScript developers by now have learned to use Array’s builtin methods like &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;some&lt;/code&gt;, and &lt;code&gt;every&lt;/code&gt; to manipulate arrays of data in a functional programming style. This style of programming has the advantage of being easier to reason about than imperative loop style programming. It does have a number of drawbacks, however:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The operations only work for arrays, not iterables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is a fairly limited set of operations. Notable omissions are operations like &lt;code&gt;take&lt;/code&gt;, &lt;code&gt;skip&lt;/code&gt;, &lt;code&gt;first&lt;/code&gt;, &lt;code&gt;last&lt;/code&gt;, &lt;code&gt;single&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bad performance: When chaining multiple operations, they will each create an intermediate array and thus iterate the array as many times as there are operators. E.g:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Will create 3 intermediate arrays and then iterate the last array to reduce it to a string, a total of 4 array iterations. Not exactly efficient. Of course this is not an issue when the source contains 10 or 100 elements. But if there are millions of elements, it could be a problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using RxJS operators instead
&lt;/h2&gt;

&lt;p&gt;When thinking of RxJS, you usually think &lt;em&gt;asynchronous stream processing&lt;/em&gt;, but RxJS will in fact process streams synchronously when possible. This means that we can use RxJS to create a synchronous stream of values from an array or other iterable using the from function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This snippet will only iterate the array once, transforming and filtering values as it goes along. It is however a bit clunky to have to use the from, pipe, and subscribe keywords, so I have written a &lt;a href="https://www.npmjs.com/package/rxjs-transducer"&gt;small transducer library&lt;/a&gt; that reduces the snippet above to:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The cool thing about this transducer is that it supports iterables such as infinite sequences so you can do stuff like:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Furthermore it is written in TypeScript, so it will give you full TypeScript support in your IDE:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6lp-J5_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A_I02gNN4T1VE3k0KllHfXw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6lp-J5_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A_I02gNN4T1VE3k0KllHfXw.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;So, how does it perform you say?&lt;/p&gt;

&lt;p&gt;Let us make a test using console.time where we map, filter and reduce an array of 10,000,000 random numbers:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;310ms vs 47ms! So in this case the rxjs-transducer is more than 6 times as fast as standard array chaining. Your mileage may vary, but in almost all cases it will be quite a lot faster than array chaining.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to get it
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm install rxjs-transducer&lt;/code&gt; (&amp;lt; 1KB GZipped)&lt;/p&gt;

&lt;p&gt;Check out the code on GitHub: &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/rasmusvhansen"&gt;
        rasmusvhansen
      &lt;/a&gt; / &lt;a href="https://github.com/rasmusvhansen/rxjs-transducer"&gt;
        rxjs-transducer
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A transducer implementation using the excellent operators from RxJs
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
rxjs-transducer&lt;/h1&gt;
&lt;p&gt;A transducer implementation using the excellent and well known operators from RxJS
The benefits are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Performance: Doing a array.map().filter().reduce() causes the array to be iterated 3 times. Using rxjs-transducers, the array is only iterated once. Doing a &lt;code&gt;filter().map().Math.max()&lt;/code&gt; on an array with 1,000,000 items is roughly three times as fast with the transducer as with normal array operations.&lt;/li&gt;
&lt;li&gt;Ability to work with lazy and infinite collections (generators)&lt;/li&gt;
&lt;li&gt;Access to a huge library of well tested operators from RxJS such as &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;skip&lt;/code&gt;, &lt;code&gt;take&lt;/code&gt;, &lt;code&gt;takeWhile&lt;/code&gt;, and many others&lt;/li&gt;
&lt;li&gt;Full TypeScript support&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
Installation&lt;/h2&gt;
&lt;div class="highlight highlight-source-shell"&gt;&lt;pre&gt;npm install rxjs-transducer --save&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
Usage&lt;/h2&gt;
&lt;h3&gt;
TypeScript / ES6&lt;/h3&gt;
&lt;div class="highlight highlight-source-ts"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-s1"&gt;transducer&lt;/span&gt; &lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;'rxjs-transducer'&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-s1"&gt;map&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;filter&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;reduce&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;skip&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;take&lt;/span&gt; &lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;'rxjs/operators'&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-k"&gt;const&lt;/span&gt; &lt;span class="pl-s1"&gt;source&lt;/span&gt; &lt;span class="pl-c1"&gt;=&lt;/span&gt; &lt;span class="pl-kos"&gt;[&lt;/span&gt;&lt;span class="pl-s"&gt;'a'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s"&gt;'ab'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s"&gt;'abc'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s"&gt;'abcd'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s"&gt;'abcde'&lt;/span&gt;&lt;span class="pl-kos"&gt;]&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-c"&gt;// Works as standard array&lt;/span&gt;&lt;/pre&gt;…&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/rasmusvhansen/rxjs-transducer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Playground
&lt;/h2&gt;

&lt;p&gt;I have created a StackBlitz playground for you to try it out in your browser:&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/typescript-kgtpyq?embed=1&amp;amp;&amp;amp;" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Let me know what you think.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/rasmusvhansen"&gt;https://twitter.com/rasmusvhansen&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>transducer</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
