<?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: Kyle Agronick</title>
    <description>The latest articles on DEV Community by Kyle Agronick (@agronick).</description>
    <link>https://dev.to/agronick</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%2F171569%2F9de1dd9b-6fb8-418b-af1c-e46a33d17a72.jpeg</url>
      <title>DEV Community: Kyle Agronick</title>
      <link>https://dev.to/agronick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/agronick"/>
    <language>en</language>
    <item>
      <title>Optimizing Your JavaScript for Speed</title>
      <dc:creator>Kyle Agronick</dc:creator>
      <pubDate>Tue, 18 Jun 2019 03:42:10 +0000</pubDate>
      <link>https://dev.to/agronick/benchmarking-common-javascript-tasks-5cmp</link>
      <guid>https://dev.to/agronick/benchmarking-common-javascript-tasks-5cmp</guid>
      <description>&lt;p&gt;There are often different ways of doing the same thing in JavaScript. You shouldn't waste your time worrying about premature optimizations but there are plenty of times performance does matter. When you are dealing with with huge datasets and lots of DOM elements you can push the browser to it's limits.&lt;/p&gt;

&lt;p&gt;There are several questions I've had and I decided to explore them.&lt;/p&gt;

&lt;p&gt;Its important to note that these benchmarks are not scientific. They were put together out of curiosity and there are many circumstances that could affect these results. The results I discuss below took place on Chrome's V8 JavaScript engine.&lt;/p&gt;

&lt;p&gt;Lets look at some common ways of doing things and see how the browser reacts.&lt;/p&gt;

&lt;h1&gt;
  
  
  Concat vs. Spread
&lt;/h1&gt;

&lt;p&gt;When you want to combine arrays you have a few choices. You can use the old style of &lt;code&gt;arr.concat(arr2)&lt;/code&gt; or you can use the spread operator &lt;code&gt;[...arr, ...arr2]&lt;/code&gt;. Both of them return a new array.&lt;/p&gt;

&lt;p&gt;You might be surprised to find out that for large arrays spread can be about &lt;a href="https://jsperf.com/concat-vs-spreader"&gt;50% slower&lt;/a&gt;. I found that concat was consistently faster unless the array was less than 10 items.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bind vs. Arrow Function
&lt;/h1&gt;

&lt;p&gt;When you pass a callback there are many times you want to preserve your reference to &lt;code&gt;this&lt;/code&gt;. You have two choices for how to do this. You can do &lt;code&gt;this.myFunction.bind(this)&lt;/code&gt; or &lt;code&gt;() =&amp;gt; this.myFunction()&lt;/code&gt;. It turns out that using an arrow function is about &lt;a href="https://jsperf.com/bind-vs-array"&gt;83% slower than bind()&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;That is, unless if you are calling the function multiple times. It is more expensive to set up the function with &lt;code&gt;bind()&lt;/code&gt; than it is with an arrow function. So if you are only calling the function once &lt;a href="https://jsperf.com/bind-vs-array-single"&gt;bind is about 90% slower&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Filtering for True
&lt;/h1&gt;

&lt;p&gt;It is a common task to get the truthy values out of an array. There are a few ways to do this. The common ones involve passing something to filter. For some reason filter requires a value. &lt;/p&gt;

&lt;p&gt;So should you cast it to a boolean?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.filter(Boolean)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just return the value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.filter((i)=&amp;gt; i);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or cast it to a boolean using a double negative?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.filter((i)=&amp;gt; !!i);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Its a minor difference but using &lt;code&gt;arr.filter(Boolean)&lt;/code&gt; is about &lt;a href="https://jsperf.com/filter-true"&gt;9% slower&lt;/a&gt; and the no-op arrow function is the fastest. The double negation is about 2% slower and in effect does nothing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sets vs Arrays
&lt;/h1&gt;

&lt;p&gt;Sets and Arrays can be used for a lot of the same things. The main difference is that sets are unique and arrays aren't. The other big differnce is that it is way faster to check if an object exists in a set rather than an array. The difference is off the charts. With 10,000 items JSPref reports &lt;code&gt;Array.includes&lt;/code&gt; is &lt;a href="https://jsperf.com/set-vs-array-contains"&gt;100% slower&lt;/a&gt; than &lt;code&gt;Set.has&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But what if you have an Array and you want to check if things exist in it. Should you convert it to a set? Well it turns out creating a set is somewhat expensive. If you needed to check if a 10,000 item Array contained something 200 times &lt;a href="https://jsperf.com/convert-array-or-check-includes"&gt;you would still be better off&lt;/a&gt; keeping it as an Array and using &lt;code&gt;Array.includes&lt;/code&gt; rather than converting it to a set and using &lt;code&gt;Set.has&lt;/code&gt;. There is an inflection point though. It seems to be right around 400 checks on an array of 10,000 items.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed these benchmarks. As always, you should benchmark your usecase and unless your code needs to be highly optimized the most important thing is how the code looks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
    <item>
      <title>What does your build pipeline look like?</title>
      <dc:creator>Kyle Agronick</dc:creator>
      <pubDate>Tue, 18 Jun 2019 01:36:04 +0000</pubDate>
      <link>https://dev.to/agronick/what-does-your-build-pipeline-look-like-1k7c</link>
      <guid>https://dev.to/agronick/what-does-your-build-pipeline-look-like-1k7c</guid>
      <description>&lt;p&gt;How does your code go from the repo to the users? How do you deal with connection draining? Do you make sure that requests using the old code aren't interrupted? Do you spin servers up and down based on load? Do you have websocket connections that can last for hours?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>devops</category>
      <category>architecture</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
