<?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>Forem: Nahuel Scotti</title>
    <description>The latest articles on Forem by Nahuel Scotti (@singuerinc).</description>
    <link>https://forem.com/singuerinc</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%2F36535%2F782f2a25-6a06-4311-bef8-4ed674455a53.jpeg</url>
      <title>Forem: Nahuel Scotti</title>
      <link>https://forem.com/singuerinc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/singuerinc"/>
    <language>en</language>
    <item>
      <title>How to make your Javascript code run faster</title>
      <dc:creator>Nahuel Scotti</dc:creator>
      <pubDate>Thu, 31 May 2018 15:58:00 +0000</pubDate>
      <link>https://forem.com/singuerinc/how-to-make-your-javascript-code-faster-4pn8</link>
      <guid>https://forem.com/singuerinc/how-to-make-your-javascript-code-faster-4pn8</guid>
      <description>&lt;p&gt;A long time ago, in 2011, I wrote my first DNI (Spanish ID) &lt;a href="https://blog.singuerinc.com/javascript/actionscript3/validators/dni/nie/nif/2011/06/17/tip-validar-un-dninienif-de-espana-con-actionscript/" rel="noopener noreferrer"&gt;validation script&lt;/a&gt;. I did it with ActionScript.&lt;/p&gt;

&lt;p&gt;Some years later, after the Flash era, I &lt;a href="https://blog.singuerinc.com/javascript/validation/spain/dni/nie/nif/regex/2014/08/06/code-day-006-spain-dni-validation/" rel="noopener noreferrer"&gt;re-wrote it&lt;/a&gt; for Javascript.&lt;/p&gt;

&lt;p&gt;Recently, I went into a heavily Functional Programming learning process and as a goal, I propose myself to create a new module applying what I learned.&lt;/p&gt;

&lt;p&gt;The result (not following FP paradigm too much I would say) is:&lt;/p&gt;

&lt;h4&gt;
  
  
  Better DNI
&lt;/h4&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/singuerinc" rel="noopener noreferrer"&gt;
        singuerinc
      &lt;/a&gt; / &lt;a href="https://github.com/singuerinc/better-dni" rel="noopener noreferrer"&gt;
        better-dni
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      The fastest Spanish DNI (NIE / NIF) validation out there.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;p&gt;&lt;a href="http://npm.im/better-dni" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/4cf4bb23adf5e4757d8963a1111a80f8289eb48a7c94b2eb15a80c1589f42632/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f6265747465722d646e692e737667" alt="npm"&gt;&lt;/a&gt;
&lt;a href="https://app.codacy.com/app/nahuel.scotti/better-dni" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/f24a35f872662522633cd33b11fb6ae11432e89cd40e72eeca362d99637cbce2/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f67726164652f38336430306661626661343234623064626261363437333566363466663734632e7376673f7374796c653d666c61742d737175617265" alt="Codacy grade"&gt;&lt;/a&gt;
&lt;a href="https://coveralls.io/github/singuerinc/better-dni?branch=master" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/db39d639827dc7a485bbecc883de1f85adaed304dcca77893b3d7928cd9deb9b/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f73696e67756572696e632f6265747465722d646e692e7376673f7374796c653d666c61742d737175617265" alt="Coveralls github"&gt;&lt;/a&gt;
&lt;a href="https://snyk.io/test/github/singuerinc/better-dni" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/78fec85b39eef93e0ece754f72703a6883696027d0faba025ccec9c1d871461b/68747470733a2f2f736e796b2e696f2f746573742f6769746875622f73696e67756572696e632f6265747465722d646e692f62616467652e7376673f7374796c653d666c61742d737175617265" alt="Known Vulnerabilities"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/fa4742f255f12d16aae7a8a85e5c3f7b635f5916f9d8e23a202cb9032f6977cd/68747470733a2f2f696d672e736869656c64732e696f2f62756e646c6570686f6269612f6d696e2f6265747465722d646e692e7376673f7374796c653d666c61742d737175617265"&gt;&lt;img src="https://camo.githubusercontent.com/fa4742f255f12d16aae7a8a85e5c3f7b635f5916f9d8e23a202cb9032f6977cd/68747470733a2f2f696d672e736869656c64732e696f2f62756e646c6570686f6269612f6d696e2f6265747465722d646e692e7376673f7374796c653d666c61742d737175617265" alt="npm bundle size (minified)"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/singuerinc/better-dnilogo.png"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fsinguerinc%2Fbetter-dnilogo.png" alt="Better DNI"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The fastest Spanish DNI (NIE / NIF) validation out there — And lighter: 660 bytes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Installation&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-c"&gt;// npm&lt;/span&gt;
&lt;span class="pl-s1"&gt;npm&lt;/span&gt; &lt;span class="pl-s1"&gt;i&lt;/span&gt; &lt;span class="pl-s1"&gt;better&lt;/span&gt;&lt;span class="pl-c1"&gt;-&lt;/span&gt;&lt;span class="pl-s1"&gt;dni&lt;/span&gt;
&lt;span class="pl-c"&gt;// yarn&lt;/span&gt;
&lt;span class="pl-s1"&gt;yarn&lt;/span&gt; &lt;span class="pl-s1"&gt;add&lt;/span&gt; &lt;span class="pl-s1"&gt;better&lt;/span&gt;&lt;span class="pl-c1"&gt;-&lt;/span&gt;&lt;span class="pl-s1"&gt;dni&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Usage&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;isValid / isNIF / isNIE / ctrlChar&lt;/h3&gt;

&lt;/div&gt;
&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&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;isValid&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;isNIF&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;isNIE&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;ctrlChar&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;"better-dni"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;

&lt;span class="pl-c"&gt;// validates any type of DNI (NIE or NIF)&lt;/span&gt;
&lt;span class="pl-en"&gt;isValid&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"Z7662566Y"&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;//=&amp;gt; true&lt;/span&gt;

&lt;span class="pl-c"&gt;// checks if it is a valid NIF&lt;/span&gt;
&lt;span class="pl-en"&gt;isNIF&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"06672804K"&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;//=&amp;gt; true&lt;/span&gt;

&lt;span class="pl-c"&gt;// checks if it is a valid NIE&lt;/span&gt;
&lt;span class="pl-en"&gt;isNIE&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"X1302311M"&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;//=&amp;gt; true&lt;/span&gt;

&lt;span class="pl-c"&gt;// returns the control letter in upper case&lt;/span&gt;
&lt;span class="pl-en"&gt;ctrlChar&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"X1302311M"&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;//=&amp;gt; 'M'&lt;/span&gt;
&lt;span class="pl-en"&gt;ctrlChar&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"X1302311"&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;//=&amp;gt; 'M'&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Generators&lt;/h3&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h4 class="heading-element"&gt;NIF&lt;/h4&gt;

&lt;/div&gt;
&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&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;randomNIF&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-s1"&gt;randomNIFWith&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;"better-dni"&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;
&lt;span class="pl-en"&gt;randomNIF&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-kos"&gt;;&lt;/span&gt; &lt;span class="pl-c"&gt;//=&amp;gt; '31719111H'&lt;/span&gt;

&lt;span class="pl-c"&gt;// returns a valid random NIF given the control letter&lt;/span&gt;
&lt;span class="pl-en"&gt;randomNIFWith&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;"C"&lt;/span&gt;&lt;span class="pl-kos"&gt;)&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/singuerinc/better-dni" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Quickly I found out that there are many modules doing the same…&lt;/p&gt;

&lt;p&gt;Replicate what others already did is a little boring, so I changed my goal to&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Create the faster module to validate a DNI”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this process, I found many ways to improve the speed of the code, although sometimes it looks a bit uglier, some of them made the code twice faster.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that you would get better results with newer versions of Node. I think &lt;a href="https://jsperf.com" rel="noopener noreferrer"&gt;https://jsperf.com&lt;/a&gt; is quite good in speed since it runs isolated with just one or two lines of code, but when I tried in “normal” computers with the whole module the difference is quite big. I would say that you should test your code in different environments and see where you can optimize.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is what I discovered:&lt;/p&gt;

&lt;h3&gt;
  
  
  str.substr() vs str.slice()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'0123456789'.slice(-9); // =&amp;gt; '123456789'
'0123456789'.substr(-9); // =&amp;gt; '123456789'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Winner: &lt;strong&gt;slice&lt;/strong&gt; / x21 faster! / &lt;a href="https://jsperf.com/better-dni-slice-vs-substr/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  toLowerCase() vs toUpperCase()
&lt;/h3&gt;

&lt;p&gt;I have to admit that I have never thought before why would one method be faster than the other. I was looking into the V8 code and looks like &lt;a href="https://github.com/v8/v8/blob/master/src/string-case.cc#L16" rel="noopener noreferrer"&gt;https://github.com/v8/v8/blob/master/src/string-case.cc#L16&lt;/a&gt; is the answer to that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'A'.toLowerCase(); // =&amp;gt; 'a'

'a'.toUpperCase(); // =&amp;gt; 'A'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially useful if you need to compare two strings.&lt;/p&gt;

&lt;p&gt;Before comparing them, better to convert them to lower case.&lt;/p&gt;

&lt;p&gt;Winner: &lt;strong&gt;toLowerCase&lt;/strong&gt; / slightly faster / &lt;a href="https://jsperf.com/better-dni-lower-case-vs-upper-case/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famm38dcn604mlnv6qavx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famm38dcn604mlnv6qavx.jpeg" width="800" height="533"&gt;&lt;/a&gt;Random image related to speed. You need one in all posts. Photo by &lt;a href="https://unsplash.com/photos/ukTd6UiQbLQ?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;James Traf&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/fast?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  indexOf() vs object prop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const y = 'xyz'.indexOf('y'); // =&amp;gt; 1

const y = {x: 0, y: 1, z: 2}['y'] // =&amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Winner: &lt;strong&gt;indexOf&lt;/strong&gt; / slightly faster / &lt;a href="https://jsperf.com/better-dni-index-of-vs-obj-prop/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  String concatenation vs Template literals
&lt;/h3&gt;

&lt;p&gt;I don’t know why I imagined that template literals were more complex than a simple string concatenation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const x = 'x';

'yyy' + x;

'yyy${x}'; // faster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Winner: &lt;strong&gt;Template literals&lt;/strong&gt; / slightly faster / &lt;a href="https://jsperf.com/better-dni-str-concat-vs-str-template/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Convert to number
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parseInt('1234', 10); // =&amp;gt; 1234

+'1234'; // =&amp;gt; 1234
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Winner: &lt;strong&gt;+sign&lt;/strong&gt; / x6.12 faster! / &lt;a href="https://jsperf.com/better-dni-convert-to-number/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Math.floor() vs Bitwise shift
&lt;/h3&gt;

&lt;p&gt;I took some inspiration from this &lt;a href="http://blog.blakesimpson.co.uk/read/58-fastest-alternative-to-math-floor-in-javascript" rel="noopener noreferrer"&gt;blog post&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.floor(1.123456789); // =&amp;gt; 1

1.123456789 &amp;lt;&amp;lt; 0; // =&amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Winner: &lt;strong&gt;Bitwise shift&lt;/strong&gt; / slightly faster in jsperf but Math.floor() performs better much better in my MacBook Pro. / &lt;a href="https://jsperf.com/better-dni-floor-vs-bitwise/1" rel="noopener noreferrer"&gt;test&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Other tips that could help your code to run faster
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Tip 1
&lt;/h4&gt;

&lt;p&gt;Do easy validations first and return as quickly as possible. Take a look at these snippets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(someComplexValidation(value) &amp;amp;&amp;amp; value.length !== 9) return;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(value.length !== 9 &amp;amp;&amp;amp; someComplexValidation(value)) return;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s the same code right? Do the “easy” validation first so your code does not run and return if the first validation does not pass.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 2
&lt;/h4&gt;

&lt;p&gt;Avoid using costly methods like “toUpperCase()” and try to use smart alternatives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 'XYZ'.indexOf('y'.toUpperCase()); // =&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 'XYZxyz'.indexOf('y') % 3; // =&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Tip 3
&lt;/h4&gt;

&lt;p&gt;If you know that you can achieve the same result with different methods, find the one that works better for each specific case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const x1 = 'xyz'.substr(0, 1); // =&amp;gt; 'x'

const x2 = 'xyz'[0]; // =&amp;gt; 'x'

const x3 = 'xyz'.slice(0, 1); // =&amp;gt; 'x'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this would help you to optimize your code!&lt;/p&gt;

&lt;p&gt;Do you want to contribute, make it faster? Open a pull request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/singuerinc/better-dni/" rel="noopener noreferrer"&gt;singuerinc/better-dni&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>optimization</category>
    </item>
    <item>
      <title>Migrate from Jekyll to Gatsby</title>
      <dc:creator>Nahuel Scotti</dc:creator>
      <pubDate>Wed, 25 Oct 2017 07:04:27 +0000</pubDate>
      <link>https://forem.com/singuerinc/migrate-from-jekyll-to-gatsby-11a</link>
      <guid>https://forem.com/singuerinc/migrate-from-jekyll-to-gatsby-11a</guid>
      <description>&lt;h4&gt;
  
  
  A bit of history
&lt;/h4&gt;

&lt;p&gt;Some years ago I created my &lt;a href="https://blog.singuerinc.com/"&gt;blog&lt;/a&gt; with &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt; and it has been working well for a while.&lt;/p&gt;

&lt;p&gt;I have never been a huge fan of Ruby, but because of requirements or convenience I made some apps with it (Rails / Sinatra / Jekyll / etc).&lt;/p&gt;

&lt;p&gt;I used to like Jekyll because I could write my posts in Markdown and easily to iterate through them. At that time I took a theme called &lt;a href="https://github.com/poole/lanyon"&gt;Lanyon&lt;/a&gt; and I tweaked a bit. That was a enough for me.&lt;/p&gt;

&lt;p&gt;But recently I found that I was installing Ruby and Jekyll in my machine just to manage my blog (I mostly develop with Node). I tried to develop it inside a Docker container, but was extremely slow.&lt;/p&gt;

&lt;p&gt;Some weeks ago one of my colleague at work talked me about &lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt;, and since I was learning &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; and I started looking at &lt;a href="http://graphql.org/"&gt;GraphQL&lt;/a&gt; (after attend a talk at &lt;a href="http://nordicjs.com/"&gt;Nordic.js&lt;/a&gt;) I was very interested in give it a try.&lt;/p&gt;

&lt;h3&gt;
  
  
  Migration from Jekyll to Gatsby
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OYWsgFp8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/219/1%2Axbn0cmHi2jjxy2cYaP_npg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OYWsgFp8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/219/1%2Axbn0cmHi2jjxy2cYaP_npg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Data
&lt;/h4&gt;

&lt;p&gt;One of the biggest concern about migrating from one generator to another is how you move the data. In my case I have around 45 posts, so it is not a big deal to convert from one type to another, but still a manual work that I wanted to avoid.&lt;/p&gt;

&lt;p&gt;Luckily Gatsby works with Markdown and it can take the data from your YAML front matter block, so you don’t have to change anything. You just need to install gatsby-transformer-remark plugin.&lt;/p&gt;

&lt;p&gt;Then, in your gatsby-config.js file add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gatsby-transformer-remark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Theme
&lt;/h4&gt;

&lt;p&gt;My blog is clean and simple, I don’t need too much css, instead of bloating my blog with a theme I added &lt;a href="http://tachyons.io/"&gt;Tachyons&lt;/a&gt;. I managed to replicate 99% of my previous theme with classes from Tachyons. Something that I would like to do better is to remove the part that I do not use from it.&lt;/p&gt;

&lt;p&gt;The current css file has less than 30 lines, the rest of the layout is created with tachyons classes: &lt;a href="https://github.com/singuerinc/blog/blob/master/src/layouts/index.css"&gt;https://github.com/singuerinc/blog/blob/master/src/layouts/index.css&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Slug + Date
&lt;/h3&gt;

&lt;p&gt;This was the only “tricky” one, since I wanted to preserve the same page names in order to make a 1:1 transition it was not possible with the default behavior from Gatsby. I don’t have a date field in my front matter block, so I need to extract the date from the file name.&lt;/p&gt;

&lt;p&gt;The slug in my case is generated with this (default in Jekyll) structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://blog.singuerinc.com/[n-categories]/YYYY/MM/DD/dashed-title/

// Given this data:
categories:
  - app
  - macos
  - vuejs
  - electron

filename: 2017-05-09-introducing-tomeito.md

// I would like to get:
/app/macos/vuejs/electron/2017/05/09/introducing-tomeito/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How I created it? Inside the gastby-node.&lt;/p&gt;

&lt;p&gt;I used the onCreateNode function to tweak the slug:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onCreateNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boundActionCreators&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createNodeField&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;boundActionCreators&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;internal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;`MarkdownRemark`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;frontmatter&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createFilePath&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;basePath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`pages`&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// get the date and title from the file name&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;\/([\d]{4}&lt;/span&gt;&lt;span class="sr"&gt;-&lt;/span&gt;&lt;span class="se"&gt;[\d]{2}&lt;/span&gt;&lt;span class="sr"&gt;-&lt;/span&gt;&lt;span class="se"&gt;[\d]{2})&lt;/span&gt;&lt;span class="sr"&gt;-&lt;/span&gt;&lt;span class="se"&gt;{1}(&lt;/span&gt;&lt;span class="sr"&gt;.+&lt;/span&gt;&lt;span class="se"&gt;)\/&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// create a new slug concatenating everything&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;slugify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/`&lt;/span&gt;

    &lt;span class="nx"&gt;createNodeField&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`slug`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;slug&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// save the date for later use&lt;/span&gt;
    &lt;span class="nx"&gt;createNodeField&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`date`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deploy
&lt;/h3&gt;

&lt;p&gt;I used to deploy to &lt;a href="https://about.gitlab.com/features/pages/"&gt;GitLab Pages&lt;/a&gt;, but I notice that from time to time it had his downtimes, so now I’m testing &lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; with almost the same CI workflow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k9lo2Lh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/719/1%2AiuonuLVRzAzN9E_L0EzAqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k9lo2Lh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/719/1%2AiuonuLVRzAzN9E_L0EzAqw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusions
&lt;/h3&gt;

&lt;p&gt;Some key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node instead of Ruby&lt;/li&gt;
&lt;li&gt;Much faster development workflow, hot reload out of the box.&lt;/li&gt;
&lt;li&gt;I can query what I need and transform the data before using it. (I’m looking into the gatsby-plugin-feed to recreate the Atom Feed)&lt;/li&gt;
&lt;li&gt;React and GraphQL for free with Gatsby.&lt;/li&gt;
&lt;li&gt;Since I am confident with the Node ecosystem I’m able to contribute: First pull request to Gatsby: &lt;a href="https://github.com/gatsbyjs/gatsby/pull/2569"&gt;https://github.com/gatsbyjs/gatsby/pull/2569&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Netlify vs GitLab Pages (hopefully 100% uptime)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Final thoughts:&lt;/p&gt;

&lt;p&gt;Although the blog is the same, in content and look, the way that is created has completely changed. For me it is a whole new experience, easier, and faster.&lt;/p&gt;

&lt;p&gt;I wanted to move the blog to Node long time ago and it is finally there!&lt;/p&gt;

&lt;p&gt;Now it is time for my portfolio (also built with Jekyll): &lt;a href="https://www.singuerinc.com/"&gt;https://www.singuerinc.com/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Show me the code
&lt;/h3&gt;

&lt;p&gt;If you want to take a look at the code you can do it, it is published in GitHub: &lt;a href="https://github.com/singuerinc/blog"&gt;https://github.com/singuerinc/blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at&lt;/em&gt; &lt;a href="https://blog.singuerinc.com/jekyll/gatsby/graphql/2017/11/01/migrate-from-jekyll-to-gatsby/"&gt;https://blog.singuerinc.com/jekyll/gatsby/graphql/2017/11/01/migrate-from-jekyll-to-gatsby/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jekyll</category>
      <category>node</category>
      <category>netlify</category>
      <category>gatsby</category>
    </item>
  </channel>
</rss>
