<?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: Mateusz Roszczyk</title>
    <description>The latest articles on DEV Community by Mateusz Roszczyk (@mateuszroszczyk).</description>
    <link>https://dev.to/mateuszroszczyk</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%2F797540%2F71d9a085-8afb-4cd3-b9d6-bf907a67e656.jpeg</url>
      <title>DEV Community: Mateusz Roszczyk</title>
      <link>https://dev.to/mateuszroszczyk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mateuszroszczyk"/>
    <language>en</language>
    <item>
      <title>Pipe any method in template - quick &amp; easy</title>
      <dc:creator>Mateusz Roszczyk</dc:creator>
      <pubDate>Mon, 31 Jan 2022 18:42:09 +0000</pubDate>
      <link>https://dev.to/mateuszroszczyk/pipe-any-method-in-template-quick-easy-4fcl</link>
      <guid>https://dev.to/mateuszroszczyk/pipe-any-method-in-template-quick-easy-4fcl</guid>
      <description>&lt;p&gt;&lt;a href="https://www.reddit.com/r/MemeTemplatesOfficial/comments/dw6oy1/stonk_meme_template_but_without_the_text/" rel="noopener noreferrer"&gt;Cover template found on r/MemeTemplatesOfficial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hi! This is my first article ever, so let me start with something simple 😅&lt;/p&gt;

&lt;p&gt;I've seen a lot of Angular Devs saying that calling methods inside templates should be avoided because of performance reasons. They're right, obviously, nevertheless using methods directly is quick and easy, so it'll always be tempting. Have you ever been in a situation where you knew that you're gonna use a certain method only once, in a specific component, and a thought of going through the whole pipe creation process was like "ooohhh, omg, such effort for one function call, seriously?!" (actually, it's not that much of an effort but enought to be a disturbance). Behold:&lt;/p&gt;

&lt;h2&gt;
  
  
  MethodPipe 😍
&lt;/h2&gt;

&lt;p&gt;Assuming your transforming method is pure, let's just pass it to a generic pipe, shall we? That way we can reap benefits of both pipe performance and ease of method direct call.&lt;/p&gt;

&lt;p&gt;Pipe's transform method implementation example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transform&amp;lt;T, U&amp;gt;(value: T, method: (arg: T) =&amp;gt; U): U {
  return method(value);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{'test string' | method: toPipeOrNotToPipe}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was trying to think of a better name for the pipe but eventually I came to a conclusion that this one reads quite well: "pipe 'test string' through method toPipeOrNotToPipe"&lt;/p&gt;

&lt;p&gt;Ok, but do we &lt;strong&gt;really&lt;/strong&gt; get the same performance benefits as in case of implementing a specific pipe from ground up?&lt;/p&gt;

&lt;p&gt;Yes, passed method isn't being treated in any different way, so it's memoized as it should be. If that answer satisfies you and you trust me then you can as well stop reading right here, otherwise...&lt;/p&gt;

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

&lt;p&gt;I've created a fresh app using &lt;code&gt;ng new&lt;/code&gt; command, removed default content and filled app.component with test content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private iterations = 50;
private multiplier = 1000000000;

public toPipeOrNotToPipe = (input: string): string =&amp;gt; {
  this.calculatePrimes(this.iterations, this.multiplier);
  return input.toUpperCase();
};

private calculatePrimes(iterations: number, multiplier: number): number[] {
  const primes = [];
  for (let i = 0; i &amp;lt; iterations; i++) {
    const candidate = i * (multiplier * Math.random());
    let isPrime = true;
    for (let c = 2; c &amp;lt;= Math.sqrt(candidate); ++c) {
      if (candidate % c === 0) {
        // not prime
        isPrime = false;
        break;
      }
    }
    if (isPrime) {
      primes.push(candidate);
    }
  }
  return primes;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;calculatePrimes&lt;/code&gt; is a slightly adjusted version of &lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Performance/Scenarios/Intensive_JavaScript" rel="noopener noreferrer"&gt;MDN Intensive Javascript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Html - 3 cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;{{ 'test string' }}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{{ 'test string' | method: toPipeOrNotToPipe }}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{{ toPipeOrNotToPipe('test string') }}&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I've enabled Angular's dev tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// main.ts

platformBrowserDynamic().bootstrapModule(AppModule).then(moduleRef =&amp;gt; {
  const applicationRef = moduleRef.injector.get(ApplicationRef);
  const componentRef = applicationRef.components[0];
  enableDebugTools(componentRef);
}).catch(err =&amp;gt; console.error(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allowed me to use &lt;code&gt;ng.profile.timeChangeDetection()&lt;/code&gt; inside browser's console and, well..., time change detection 😆&lt;/p&gt;

&lt;h3&gt;
  
  
  Results
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rendered content&lt;/th&gt;
&lt;th&gt;CD cycle time [ms]&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ 'test string' }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0.000926&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MethodPipe&lt;/td&gt;
&lt;td&gt;0.000842&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;function call&lt;/td&gt;
&lt;td&gt;291.614000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you can see, rendering a previously memoized result is even a little faster than simple interpolation. Why? I don't wanna guess, we'd have to look into Angular's guts :)&lt;/p&gt;

&lt;h4&gt;
  
  
  Annotations:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;The results don't take the initial render into account.&lt;/li&gt;
&lt;li&gt;The times presented in the table are the average of 10 measurements.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Make yourself comfortable with pipes and use them 😉&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
