<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juan Carlos Garzon on Medium]]></title>
        <description><![CDATA[Stories by Juan Carlos Garzon on Medium]]></description>
        <link>https://medium.com/@juancarlosgarzon?source=rss-5783c5259189------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XPyPb1pj_7baj46g_jeVmA.jpeg</url>
            <title>Stories by Juan Carlos Garzon on Medium</title>
            <link>https://medium.com/@juancarlosgarzon?source=rss-5783c5259189------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 07:27:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@juancarlosgarzon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Every Codebase Is a Story (Most Are Just Bad Ones)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@juancarlosgarzon/every-codebase-is-a-story-most-are-just-bad-ones-72f64b11760b?source=rss-5783c5259189------2"><img src="https://cdn-images-1.medium.com/max/1480/0*by3DuOoikaweTGeq.png" width="1480"></a></p><p class="medium-feed-snippet">I&#x2019;ve been developing software for over 15 years. If you&#x2019;ve done this long enough, you get a kind of intuitive feel for code that has a&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@juancarlosgarzon/every-codebase-is-a-story-most-are-just-bad-ones-72f64b11760b?source=rss-5783c5259189------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@juancarlosgarzon/every-codebase-is-a-story-most-are-just-bad-ones-72f64b11760b?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/72f64b11760b</guid>
            <category><![CDATA[story-driven-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Thu, 08 Oct 2026 15:36:53 GMT</pubDate>
            <atom:updated>2026-10-08T15:36:53.948Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[KMS and Envelope Encryption Explained]]></title>
            <link>https://medium.com/icapital-network-technology-group/kms-and-envelope-encryption-explained-e8753e817e4a?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8753e817e4a</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[key-management-system]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Wed, 17 Jun 2020 20:21:41 GMT</pubDate>
            <atom:updated>2020-06-17T20:21:41.297Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is a KMS (Key Management System)?</strong></p><p>Behind every bit of encrypted information, there is a <strong>key</strong> that unlocks that information. Due to the complexity of modern encryption, it becomes necessary to create and maintain a system that allows you to create, manage, assign, decommission keys, as well as perform the base encryption and decryption functions. This kind of system is known as a <strong>KMS</strong> or a Key Management System. Having a <strong>KMS</strong> is so crucial that even Amazon offers its own KMS system to the public.</p><p><strong>Limitations of Amazon KMS</strong></p><p>While Amazon KMS is a highly considered and useful solution. Its flaw lies in how your keys are stored by regions. So if you were to create encryption keys in the US-East region, it would mean that if there were a disaster that caused an outage to Amazon services on the East coast of the US, these keys would be unavailable to you, and prevent you from accessing your data during a disaster. This is a worst-case scenario, and if your don’t have a need for a robust disaster recovery plan, then you could use Amazon KMS without much worry.</p><p>However, let&#39;s practice extreme caution and work around Amazon’s KMS region problem. We’ll do this by creating our own KMS Service and manage our own data security strategy.</p><p><strong>Basic Encryption</strong></p><p>The base of any KMS, is the ability to encrypt and decrypt data. Node has a built-in crypto library. This gives us access to an encryption API, allowing us to choose and implement from many different cryptographic algorithms from OpenSSL. If you want to see a full list of supported cryptographic algorithms, run the following command in your terminalopenssl list -cipher-algorithms.</p><p>Choosing a cryptographic algorithm is a topic worthy of discussion on its own. However, for our purposes, let’s keep it simple and just use AES-128-CBC in our examples.</p><pre>const crypto = require(&quot;crypto&quot;);<br>const ALGORITHM = &quot;aes-128-cbc&quot;;</pre><pre>const key = &quot;plainTextMasterKey&quot;;</pre><pre>const encrypt = function(str) {<br>  let cipher = crypto.createCipher(ALGORITHM, key);<br>  let ciphertext = cipher.update(str, &quot;utf8&quot;, &quot;hex&quot;);</pre><pre>  return ciphertext + cipher.final(&quot;hex&quot;);<br>};</pre><pre>const decrypt = function(encrypted_str) {<br>  let cipher = crypto.createDecipher(ALGORITHM, key);<br>  let text = cipher.update(encrypted_str, &quot;hex&quot;, &quot;utf8&quot;);</pre><pre>  return text + cipher.final(&quot;utf8&quot;);<br>};</pre><pre>module.exports = {<br>  basicEncrypt: encrypt,<br>  basicDecrypt: decrypt<br>};</pre><p>The above uses Encryptor/Decryptor, the basis of which is pretty simple, we need a key and we use that get to set up the ciphers for the createCipher and createDecipher methods. Then we simply encode and encrypt the string, and do it reverse when we want to decode and decrypt the encryption result.</p><p>You can take some time and play in the sandbox, and see how this works in action.<br>Source Code: <a href="https://codesandbox.io/s/basic-encryption-3sccu?file=/src/basicCryptor.js">https://codesandbox.io/s/basic-encryption-3sccu?file=/src/basicCryptor.js</a></p><p><strong>Envelope Encryption</strong></p><p>Now we have a basic understanding of how to encrypt and decrypt data. For a functional KMS system, we need to add the ability to support multiple keys. But this presents a new problem, how does our KMS know which key was used to encrypt which file?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aGfJF7Stjmlr3IJxNCJa1A.png" /><figcaption>Standard Encryption Workflow</figcaption></figure><p>This where <strong>envelope encryption </strong>comes in. In our previous example, we used a <strong>key </strong>to lock the data, like in the diagram above. However, in envelope encryption, we take it one step further, we use a separate key and append it to the encrypted file. In essence, we have 2 separate keys, and to avoid confusion, we’ll call the encryption key the <strong>crypt key</strong>, and the other the <strong>data key</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ca6ympz8WhDRZwtPapAE4Q.png" /><figcaption>Envelope Encryption combines the Data Key with the encrypted file</figcaption></figure><p>Keep in mind that the <strong>data key </strong>is <strong><em>not</em></strong> the same as the <strong>crypt key,</strong> you will not be able to use it to unlock encrypted information no matter who has it. It&#39;s simply an identifier that lets you know <strong><em>which</em></strong> key was used to lock the data. So let’s take a look at an example.</p><pre>const crypto = require(&quot;crypto&quot;);<br>const ALGORITHM = &quot;aes-128-cbc&quot;;</pre><pre>const DATAKEYS = {<br>  dataKey1: { cryptKey: &quot;plainTextMasterKey1&quot; },<br>  dataKey2: { cryptKey: &quot;plainTextMasterKey2&quot; },<br>  dataKey3: { cryptKey: &quot;plainTextMasterKey3&quot; }<br>};</pre><pre>const DATAKEY_LENGTH = 8;</pre><pre>const getRandomKey = function() {<br>  const ranNum = Math.floor((Math.random() * 10) % 3);<br>  return Object.keys(DATAKEYS)[ranNum];<br>};</pre><pre>const encrypt = function(str) {<br>  const dataKey = getRandomKey();<br>  const cryptKey = DATAKEYS[dataKey].cryptKey;<br>  const cipher = crypto.createCipher(ALGORITHM, cryptKey);</pre><pre>  const ciphertext = cipher.update(str, &quot;utf8&quot;, &quot;hex&quot;);</pre><pre>  return dataKey + ciphertext + cipher.final(&quot;hex&quot;);<br>};</pre><pre>const decrypt = function(enveloped_str) {<br>  const dataKey = enveloped_str.slice(0, DATAKEY_LENGTH);<br>  const cryptKey = DATAKEYS[dataKey].cryptKey;<br>  const cryptedText = enveloped_str.substring(DATAKEY_LENGTH);</pre><pre>  const cipher = crypto.createDecipher(ALGORITHM, cryptKey);<br>  const text = cipher.update(cryptedText, &quot;hex&quot;, &quot;utf8&quot;);</pre><pre>  return text + cipher.final(&quot;utf8&quot;);</pre><pre>};</pre><pre>module.exports = {<br>  encrypt: encrypt,<br>  decrypt: decrypt<br>};</pre><p>In this implementation of envelope encryption, a hash map is used to map the <strong>crypt keys</strong>, to the <strong>data keys</strong>. Then I randomly choose one <strong>data key</strong> and its associated <strong>crypt key</strong>, to encrypt the data and then append the <strong>data key </strong>with the resulting encryption.</p><p>Notice that no matter which <strong>data key </strong>is used, the decrypt method here will always be able to decode the information. We ensure this by keeping our <strong>data keys </strong>the same length. This way the decrypt method knows which part of the data is the <strong>data key</strong> and which part is the encrypted data. Also, keep in mind you don’t want to use anything readable like dataKey1 as the <strong>data key</strong>. You should randomly generate your <strong>data key</strong>, so it appears as if it&#39;s part of the encrypted data, and then you can append it to the start or end of the file.</p><p>Take some time and play in the sandbox, and see how this works in action.</p><p>Source Code: <a href="https://codesandbox.io/s/envelope-encryption-6bezb?file=/src/cryptor.js">https://codesandbox.io/s/envelope-encryption-6bezb?file=/src/cryptor.js</a></p><p><strong>Putting it all together KMS and the Keybox</strong></p><p>Envelope encryption alone does not make a key management system. So far our examples have flaws such as our <strong>crypt keys</strong> being stored as plaintext, and the lack of flexibility as to which encryption key we want to apply.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*V5TB0qfdng6vr7rtEAjG8g.png" /><figcaption>Real-World Keybox</figcaption></figure><p>To add these new features let’s introduce the <strong>key box. </strong>It is our centralized location for managing keys.</p><p>Our previous examples had our keys stored in a hash, where our <strong>crypt keys</strong> were stored as plain text. This is a bad practice because it leaves our keys vulnerable to anyone who has access to the source code. Ideally, you want to keep the circle of people who have access to these keys small.</p><p>We can do this by keeping our <strong>data keys </strong>stored in a database<strong>, </strong>along with our encrypted <strong>crypt keys</strong>. Our <strong>crypt keys </strong>should never be plaintext in our database. In keeping with the principle of keeping our <strong>crypt keys</strong> as secret as possible, we also want to protect them from anyone who has database access.</p><p>We can leverage the Amazon KMS to just encrypt and decrypt our <strong>crypt keys</strong>. Since we only need to rely on it for the initialization of our keys, and not the active encrypting of our data, the region problem doesn&#39;t hamper us. This is because our <strong>crypt keys </strong>will already have been decrypted and loaded into memory once our server starts up.</p><pre>const guids = require(&quot;./guids&quot;);<br>const Sequelize = require(&quot;sequelize&quot;);</pre><pre>// Always password protect your databases, this is just an example<br>var sequelize = new Sequelize(&quot;database&quot;, &quot;&quot;, &quot;&quot;, {<br>  host: &quot;0.0.0.0&quot;,<br>  dialect: &quot;sqlite&quot;,<br>  pool: {<br>    max: 5,<br>    min: 0,<br>    idle: 10000<br>},<br>  storage: &quot;src/db/database.sqlite&quot;<br>});</pre><pre>const KEYS = {};<br>var KMS_DATABASE;</pre><pre>function initKeys() {<br>  console.log(&quot;++++ Initializing Keys ++++&quot;);</pre><pre>  let promise = new Promise((resolve, reject) =&gt; {<br>    sequelize.authenticate().then(function(err) {<br>      console.log(&quot;Connection established.&quot;);<br>      KMS_DATABASE = sequelize.define(&quot;kms_dev&quot;,{<br>       /* Define your table columns here */<br>      });</pre><pre>      KMS_DATABASE.findAll().then(function(kms) {<br>        <em>// finds all entries in the kms_devs table<br>        </em>kms.forEach(itm =&gt; {<br>          <em>// Here I&#39;m using the plain text for this example<br>          // But here is where you would decrypt encrypted_crypt_key<br>          // By using Amazon KMS, or any other decryption strategy.<br>          </em>const decryptedKey = itm.encrypted_crypt_key;<br>          KEYS[itm.client_id] = {<br>           [itm.data_key]: { <br>             binary: decryptedKey,<br>             disabled: itm.disabled<br>            }<br>          };<br>        });<br>        // KEYS have been initalized and the app can now start<br>        resolve(); <br>      });<br>     }).catch(function(err) {<br>       console.log(&quot;Unable to connect to database: &quot;, err);<br>     });<br>   });<br>  return promise;<br>}</pre><pre>function getClientDataKeys(client_id) {<br>  return KEYS[client_id];<br>}</pre><pre>function validClient(client_id) {<br>  return getClientDataKeys(client_id) !== undefined;<br>}</pre><pre>function validKey(client_id, key_id) {<br>  const client = getClientDataKeys(client_id);<br>  return client[key_id] !== undefined;<br>}</pre><pre>function getKey(client_id) {<br>  const clientKeys = getClientDataKeys(client_id);<br>  const dataKeys = Object.keys(clientKeys);<br>  const enabledKey = dataKeys.find(key =&gt; {<br>    return !clientKeys[key].disabled;<br>  });<br>  return enabledKey;<br>}</pre><pre>function getBinary(client, key_id) {<br>  return KEYS[client][key_id].binary;<br>}</pre><pre>module.exports = {<br>  initKeys: initKeys,<br>  validClient: validClient,<br>  validKey: validKey,<br>  getKey: getKey,<br>  keyLength: guids.guidLength,<br>  getBinary: getBinary<br>};</pre><p>Source: <a href="https://codesandbox.io/s/simple-kms-579ts?file=/src/keybox.js">https://codesandbox.io/s/simple-kms-579ts?file=/src/keybox.js</a></p><p>In our example, we use the initKeys function to load our keys into memory. We added the ability to apply specific keys to clients by adding aclient_id. Although this code still stores the <strong>crypt key</strong> as plain text in the database, I placed comments where you can take can add the extra step to decrypt an encrypted <strong>crypt key</strong>, and store it as thebinary attribute. Lastly, in order to allow us to rotate keys in and out of commission, we have the disabled attribute, notice the getKey function where we only return enabled keys for encryption.</p><blockquote>Note: Rotating keys should be done on a regular intervals. It reduces the overall impact a potential breach can have. A compromised crypt key will only expose the data that was encrypted with it during your interval.</blockquote><p>Data security is a constant arms race. We have to try to keep ahead of those who are looking for new and inventive ways or gaining access to our sensitive data, and KMS is just 1 tool, in the larger InfoSec toolbox but ultimately a core component of it.</p><p>Relevant Links:<br>Amazon KMS: <a href="https://aws.amazon.com/kms/">AWS Key Management System</a>.<br>Node.js Crypto API: <a href="https://nodejs.org/api/crypto.html">https://nodejs.org/api/crypto.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8753e817e4a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/kms-and-envelope-encryption-explained-e8753e817e4a">KMS and Envelope Encryption Explained</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Audit Performance Metrics w/ DevTools]]></title>
            <link>https://medium.com/icapital-network-technology-group/audit-performance-metrics-w-devtools-cbb8d8c053b9?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbb8d8c053b9</guid>
            <category><![CDATA[front-end-developer]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[chrome]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Tue, 19 Nov 2019 15:54:38 GMT</pubDate>
            <atom:updated>2019-11-26T20:04:10.905Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding Audit Performance Metrics w/ DevTools</h3><p>Just as the adage goes “measure twice, cut once,” so must we “measure twice and optimize once.” Chrome DevTools is our toolbox, and before we start optimizing, we must first learn how to measure our site’s performance. So let’s start our focus with DevTools’ version of measuring tape, the <strong>Audits</strong> tab.</p><h3>Audits</h3><p>Like measuring tape, the <strong>Audits</strong> tab is a pretty straightforward tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*R4lkaCmtYdaga8yNMSinxw.png" /></figure><p>At a glance, you’ll have a good idea of how to use it. The <strong>Audits </strong>tool<strong> </strong>is a versatile tool. Other than measuring performance, you can also use this tool to measure your site’s best practices, accessibility, and SEO. Furthermore, you can tweak your audit to simulate a mobile environment. For our purposes, we will <strong>only </strong>be focusing on the <strong>Performance </strong>audit. Someday in the future, I will dive into the other audit types in a different post.</p><p><strong>Before Running an Audit</strong></p><p>Before running the audit, there are a couple of things you should know.</p><ol><li>Make sure you are running the audit on the staging or production URL. The goal is to get as close to a real-world environment. Doing so will give you a more accurate measurement.</li><li>Make sure to run the audit from an incognito window to prevent Chrome extensions from interfering with accurate scoring.</li></ol><p><strong>After Running an Audit</strong></p><p>On measuring tape, we measure in inches or centimeters, and in <strong>Audits,</strong> we get a grade between 0 and 100. The higher the rating, the better the performance. It’s also color-coded red, yellow, or green, which gives you a visual representation of how proud or disappointed your parents would be if you had brought home this grade. Like a parent with high standards, if you score anything less than a 90, then you have room for improvement and less than 50, and you will be grounded until your grade improves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*Q0BqwqymoEvZlx8macY0ow.png" /><figcaption>Not Red but still terrible</figcaption></figure><p>Below the score is where things can start to get a little confusing. It’s broken down into four sections, <strong>Metrics</strong>, <strong>Opportunities</strong>, <strong>Diagnostics</strong>, and <strong>Passed Audits</strong>. <strong>Metrics</strong> breaks down the six measurements used to grade you. The <strong>Opportunities </strong>section<strong> </strong>gives you suggestions to improve. <strong>Diagnostics</strong> provides you with additional useful information, but these suggestions don’t necessarily directly impact the performance of your website. Lastly, <strong>Passed Audits</strong>, gives you peace of mind by letting you know what you’re doing right.</p><p><strong>Metrics</strong></p><p>The score you got above does not tell the full story. The six measurements found in the <strong>Metrics </strong>section are used to calculate the score. At first glance, it’s not immediately clear what these mean, so let’s dive in and see what kind of impact they’ll have on your site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*wndE0wUXGFv9M-Z9GxlW2A.png" /><figcaption>Six Performance Measurements</figcaption></figure><p><strong>First Contentful Paint (FCP)</strong></p><p>FCP metric measures how quickly something gets rendered onto the screen. This parameter measures the initial time from the navigation to the site until the first bits of content appear on the screen. Ultimately this is the time it takes before users could start seeing content on the page.</p><p>Content in this context refers to any text including those with pending web fonts, images (including background images), non-white canvas or SVG, that the browser has to render initially. However, this excludes any content in iframes.</p><p><strong>How to Improve FCP</strong></p><p>To speed up FCP, we need to speed up the download time of the resources or reduce the amount of work done in the javascript, or CSS that could block the browser from rendering content.</p><ul><li>Minimize the number of render-blocking external stylesheets and scripts upon which the page depends. See <a href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css">Render-Blocking CSS</a> and <a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/loading-third-party-javascript/">Loading Third-Party JavaScript</a>.</li><li>Use <a href="https://developers.google.com/web/fundamentals/performance/get-started/httpcaching-6">HTTP Caching</a> to speed up repeat visits.</li><li>Minify and compress text-based assets to speed up their download time. See <a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer">Optimizing Encoding and Transfer Size of Text-Based Assets</a>.</li><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/javascript-startup-optimization/">Optimize JavaScript bootup</a> and reduce JavaScript payloads with <a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking/">tree shaking</a> or <a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/code-splitting/">code splitting</a>. The goal is to do less JavaScript work on page load.</li></ul><p><em>Source: </em><a href="http://developers.google.com/"><em>Google</em></a></p><p><strong>First Meaningful Paint (FMP)</strong></p><p>First Meaningful Paint is the paint that takes place after the most significant visible on-screen layout (aka., above the fold) change has happened, and web fonts have loaded. It measures the time it takes before the most visible parts of the page are onscreen. To lower your FMP score, you’ll want to speed up the time it takes to get most of your content on-screen.</p><p><strong>How to Improve FMP</strong></p><p>To optimize, you should learn the process the browser uses to get around to rendering things on the screen, known as the <strong>Critical Rendering Path</strong>. The browser uses this process to convert HMTL, CSS, and Javascript into the pixels we see on screen. It’s a dense topic, so I’ll invite you to learn more. There is a great FREE course on <a href="https://www.udacity.com/course/website-performance-optimization--ud884">Udacity</a> that goes into this topic into detail. The visualization below should give you a rough idea of an optimized <strong>Critical</strong> <strong>Rendering Path</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/0*-p64SsvqbgWXE7Pv.png" /><figcaption>Optimized Critical Rendering Path vs. Unoptimizing Rendering</figcaption></figure><p>Below the <strong>Metrics</strong> table, you will see a rough rendering timeline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*ZmoKvCkJLnhKeuQJVc6sfQ.png" /><figcaption>Rendering Timeline</figcaption></figure><p><strong>Quick Tips</strong></p><ul><li>Improving FCP should also help benefit this metric.</li><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/javascript-startup-optimization/">Optimize JavaScript bootup</a> and reduce JavaScript payloads with <a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking/">tree shaking</a> or <a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/code-splitting/">code splitting</a>. The goal is to do less JavaScript work on page load.</li><li>Minimize the number of render-blocking external stylesheets. See <a href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css">Render-Blocking CSS</a></li><li>Reduce the load on the DOM by Incrementally Loading HTML.</li></ul><p><strong>Speed Index</strong></p><p>One of the most straightforward metrics to understand. Speed Index is a measurement of how quickly the contents of a page are visibly populated. From the start to end, it will tell you how fast did your page load.</p><p><strong>How to Improve the Speed Index</strong></p><ul><li>Improving FCP and FMP</li><li>Optimize or defer Backend API calls that you make on page load.</li><li>Pay attention to all your file sizes. Try to reduce the file sizes of your HTML, CSS, Javascript, Images, or any other file you might be loading.</li></ul><p><strong>First CPU Idle (FCI)</strong></p><p>This metric measures how fast it takes before your page is quiet enough to interact. You may have noticed that on some websites, you can start scrolling and clicking around before all the elements have finished loading. Vice versa, you can also have a page that appears to have loaded, but you’re frustratingly clicking around trying to make something happen.</p><p><strong>How to Improve (FCI)</strong></p><p>Like all the other measurements, this metic benefits from the same optimizations provided above. Use this metric to inform you of how busy your site is before its <em>minimally </em>interactive, and pay special attention to your javascript. The more your CPU is doing, the more likely you are to score poorly here.</p><p><strong>Time to Interactive (TTI)</strong></p><p>Like the First CPU Idle measurement, this metric measurement goes a bit further by measuring how long takes before a page is fully interactive. Specifically, this means.</p><ul><li>The page has displayed useful content, as measured by the <strong>First Contentful Paint</strong> metric.</li><li>Event handlers are registered for most visible page elements.</li><li>The page is responsive to user interactions within 50 milliseconds.</li></ul><p>It’s possible to optimize content visibility at the expense of interactivity. Use this metric to give you insight into whether the site appears to be ready vs. being useable.</p><p><strong>How to Improve TTI</strong></p><p>To improve your TTI score, defer or remove unnecessary JavaScript work that occurs during page load. Google has some great resources</p><ul><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/javascript-startup-optimization/">Optimize JavaScript Bootup</a></li><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking/">Reduce JavaScript Payloads with Tree Shaking</a></li><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-javascript/code-splitting/">Reduce JavaScript Payloads with Code Splitting</a>.</li></ul><p>As an added help to debug trouble spots in your javascript, I invite you to check out the <a href="https://www.html5rocks.com/en/tutorials/webperformance/usertiming/">User Timing API</a>, which can help you measure your app’s Javascript performance and display the result in your Audit Report.</p><p><strong>Max Potential Input Delay.</strong></p><p>The FCI and TTI measure how long before your site is minimally or fully interactive. However, this metric will measure how long the delay(aka Input Lag) your user might experience when it attempts to interact with it for the first time. It’s a representation of the potential frustration and pains for your users when interacting with the page. For that reason, this metric is more valuable by measuring what actual users are experiencing. Using real-world data for this metric will be infinitely more useful to you.</p><p><strong>How to Improve Max Potential Input Delay.</strong></p><p>As with the other interaction metrics, this also benefits from those suggestions. Keep in mind, that Server-side rendered (SSR) JavaScript apps and iframes can cause this metric to perform poorly.</p><p><strong>View Trace</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*8Ia3SXaHH2EYEgocVVOdpw.png" /><figcaption>Performance Tab</figcaption></figure><p>As a bonus to all the measurements above, you’ll get a convenient button at the bottom labeled <strong>View Trace</strong>. Clicking this magical button takes you to the <strong>Performance</strong> tab. This tab is a tool all on its own, and it contains a plethora of extra data, it displays a raw timeline of events, such as what code was running, which events fired, API calls made, and the site’s frame rate. It even shows you when on the timeline, the <strong>FCP</strong> and <strong>FMP</strong> happened. The real power of the <strong>Audits </strong>will come from understanding this <strong>Performance</strong> tab, which I will cover in my next post.</p><p><strong>Additional Information</strong></p><p>If you&#39;re using Ruby on Rails or Angular 1.x, the links below will offer you quick solutions, to common optimization problems that will directly impact your <strong>Audits Score</strong>.</p><ul><li><a href="https://medium.com/icapital-network-technology-group/improving-rails-performance-part-3-n-1-e752ef6e0c66?source=your_stories_page---------------------------">Ruby on Rails — Resolving N+1</a></li><li><a href="https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-ng-repeat-track-by-f2d7cf3bf9b9?source=your_stories_page---------------------------">Angular 1.x — ng-repeat and track by</a></li><li><a href="https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-one-time-binding-b9a0200a8f5a?source=your_stories_page---------------------------">Angular 1.x — One Time</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbb8d8c053b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/audit-performance-metrics-w-devtools-cbb8d8c053b9">Audit Performance Metrics w/ DevTools</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 1.x Performance Quick Tip: One-Time Binding]]></title>
            <link>https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-one-time-binding-b9a0200a8f5a?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9a0200a8f5a</guid>
            <category><![CDATA[front-end-developer]]></category>
            <category><![CDATA[front-end-web-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Wed, 05 Dec 2018 18:33:28 GMT</pubDate>
            <atom:updated>2018-12-05T18:33:28.655Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rSuzLZ7TXDBJeUMo" /><figcaption>Photo by <a href="https://unsplash.com/@chrisliverani?utm_source=medium&amp;utm_medium=referral">Chris Liverani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As software developers, performance is often something you don’t worry about until you have no choice. This rings true particularly with Front End performance. Full Stack developers like me tend to pay much more attention to optimizing API’s. That’s all fine and dandy if your API takes 2ms… until you realize that your UI is lagging.</p><p>I’d like to use this space to shine a spotlight on a quick and easy method toward maintaining and increasing the performance of your Angular 1.x legacy applications.</p><blockquote>Note: Angular 1.x is on its way out in favor of React and the later versions of Angular. Migrating large codebases often happens in phases and these tips are meant to help optimize and maintain your Angular 1.x. legacy applications.</blockquote><h3>Background</h3><p>At its core, Angular’s bind data system is easy to use. This system (usually) automatically synchronizes the data between the model and the view. Angular essentially treats the model as the single source of truth.</p><p>Data Binding is typically the first skill we are introduced to when learning Angular. You can recognize it anywhere you see double brackets syntax (i.e. {{}}) surrounding your model or variable in the view. Data binding is also used as HTML attributes such as the ng-model. No matter which method is used, the outcome is always the same. Data binding <strong>dynamically</strong> updates the view and synchronizes user inputs, changes or interactions with the model and controller respectively.</p><p>This data binding is <em>usually </em>not a problem. However what Angular’s own tutorial leaves out, is how this data binding can become problematic with larger and more complex the application becomes.</p><p>You can happily work with Angular and never spare a single thought how it performs. I do recommend you spend some time learning how Angular works, particularly the <strong>digest cycle</strong>. I won’t delve into details in this article, but in essence, the <strong>digest cycle</strong> is the means by which Angular synchronizes data and ensures that everything is updated.</p><p>The <strong>digest cycle</strong> is a loop that loops over a collection of <strong>watchers</strong>. A <strong>watcher</strong> is a listener that triggers the update of the model or view. The <strong>digest cycle </strong>loops over <strong>watchers</strong> repeatedly until it determines that no additional changes are required. A <strong>watcher</strong> is created each time we bind something. There is a lot more to this process than I’ve described, but this is the fundamental concept you should grasp.</p><p>The more we increase the complexity of our applications, the more <strong>watchers</strong> we create. This, in turn, increases the duration of our <strong>digest cycles</strong>. This is where performance issues will arise; these issues are exacerbated once you learn that the <strong>digest cycle</strong> must run at least twice.</p><p>The <strong>digest cycle</strong> must run at least twice because it must take into account that data may be interdependent. If one element is changed, this can potentially trigger a change in another element and set off a chain reaction of changes. For this reason, Angular must continue the cycle until it detects that no additional changes have to be made.</p><blockquote>Note: if the loop runs ten or more times, Angular will throw an exception and kill the app. It does this to prevent and infinite loop scenario.</blockquote><h3>Visualizing the Problem</h3><pre>// Angular Controller<br>$scope.chores = [<br>{<br>assigned_to: ‘Bobby’,<br>name: ‘Homework’,<br>done: true,<br>priority: ‘High’<br>}, <br>{<br>assigned_to: ‘Chris’,<br>name: ‘Wash the Dishes’,<br>done: false,<br>priority: ‘Low’<br>}, <br>{<br>assigned_to: ‘John’,<br>name: ‘Walk the Dog’,<br>done: true,<br>priority: ‘Medium’<br>}</pre><pre>//HTML<br>&lt;table&gt;<br>  &lt;thead&gt;<br>    &lt;tr&gt;<br>      &lt;td&gt;Assigned To&lt;/td&gt;<br>      &lt;td&gt;Chore&lt;/td&gt;<br>      &lt;td&gt;Status&lt;/td&gt;<br>      &lt;td&gt;Priority&lt;/td&gt;<br>    &lt;/tr&gt;<br>  &lt;/thead&gt;<br>  &lt;tbody&gt;<br>    &lt;tr ng-repeat=”chore in chores”&gt;<br>      &lt;td&gt;{{chore.assigned_to}}&lt;/td&gt;<br>      &lt;td&gt;{{chore.name}}&lt;/td&gt;<br>      &lt;td&gt;{{chore.done ? ‘Done’ : ‘To Do’}}&lt;/td&gt;<br>      &lt;td&gt;{{chore.priority}}&lt;/td&gt;<br>    &lt;/tr&gt;<br>   &lt;/tbody&gt;<br>&lt;/table&gt;</pre><p>Let’s look at an example. How many watchers are created above?</p><p>You should promptly notice that there are 4 bound items in a table. We can see chore.assigned_to, chore.name, chore.done, and chore.priority. Did you notice that there is an ng-repeat?</p><p>This means Angular is binding those 4 properties for each chore in the array. With that being the case, we have 4 properties x 3 items, for a total of 12 <strong>watchers</strong>. If we stopped counting there, we’d be close… but we’d be wrong. We are missing the chores array in the ng-repeat. In actuality have a total of 13 <strong>watchers</strong>, for a relativity simple table view.</p><p>Observe how, even in this relatively simple example, we create more <strong>watchers</strong> than we initially think. You can safely assume that just about everything you do with Angular creates a <strong>watcher</strong>. Think about all the Angular attributes you use, directives you create, or even the third-party components you take advantage of. Now, think about all the arrays you are using. Each of those will create a <strong>watcher. </strong>It should become apparent that your performance could, as a result, take a big hit.</p><h3>Quick Solution</h3><p>A lot of the data on a page is usually static. It need not change; it just needs to be displayed. In other words, once the data is displayed, a <strong>watcher </strong>is no longer necessary.</p><p>Here is where Angular’s <strong>One-Time Binding</strong> saves our application from holding on to<strong> watchers</strong> it doesn’t need. You could easily tell Angular, <em>“Once this is loaded, don’t worry about it anymore.”</em> The best part is that it doesn’t require a massive change to your existing views. You just need to add :: before whatever you’re binding. For example: instead of inputting {{variable}}, you would input {{::variable}}.</p><pre>// Angular Controller<br>$scope.chores = [<br>{<br>assigned_to: ‘Bobby’,<br>name: ‘Homework’,<br>done: true,<br>priority: ‘High’<br>}, <br>{<br>assigned_to: ‘Chris’,<br>name: ‘Wash the Dishes’,<br>done: false,<br>priority: ‘Low’<br>}, <br>{<br>assigned_to: ‘John’,<br>name: ‘Walk the Dog’,<br>done: true,<br>priority: ‘Medium’<br>}</pre><pre>//HTML<br>&lt;table&gt;<br>  &lt;thead&gt;<br>    &lt;tr&gt;<br>      &lt;td&gt;Assigned To&lt;/td&gt;<br>      &lt;td&gt;Chore&lt;/td&gt;<br>      &lt;td&gt;Status&lt;/td&gt;<br>      &lt;td&gt;Priority&lt;/td&gt;<br>    &lt;/tr&gt;<br>  &lt;/thead&gt;<br>  &lt;tbody&gt;<br>    &lt;tr ng-repeat=”chore in ::chores”&gt;<br>      &lt;td&gt;{{::chore.assigned_to}}&lt;/td&gt;<br>      &lt;td&gt;{{::chore.name}}&lt;/td&gt;<br>      &lt;td&gt;{{::(chore.done ? ‘Done’ : ‘To Do’)}}&lt;/td&gt;<br>      &lt;td&gt;{{::chore.priority}}&lt;/td&gt;<br>    &lt;/tr&gt;<br>   &lt;/tbody&gt;<br>&lt;/table&gt;</pre><p>It truly is that simple. Notice you can use :: within the ng-repeat. You are not limited to using :: to only double brackets. Additionally, remember that if you have some inline logic you can just encase it in parentheses, i.e. (::(chore.done ? ‘Done’ : ‘To Do’)).</p><h3>When not to use a One-Time Binding?</h3><p>One-time binding is <strong>not</strong> something to use without careful thought. If you have an element, model, or variable that changes on-screen, then <strong>one-time binding </strong>is not an ideal choice. Think of <strong>one-time binding</strong> as a freeze ray. Once it’s rendered on-screen and bound variable contains a value. The view will not display the updated result… no matter how much you change it. This makes <strong>one-time binding</strong> exceptional for static page elements, but inappropriate for <strong>interactive</strong> or <strong>dynamic</strong> elements of a page.</p><h3>Who Watches the Watchers?</h3><p>Since we don’t exist in a comic book world where people are constantly worried about the unchecked power held by costumed vigilantes, this actually has an answer. There are extensions you can download for Google Chrome that will give you a count of the watchers. These extensions will also show you first-hand how much of an impact these.</p><p><a href="https://chrome.google.com/webstore/detail/angularjs-inspect-watcher/gdfcinoagafkodbnkjemaajfahnmfkhg?hl=en">https://chrome.google.com/webstore/detail/angularjs-inspect-watcher/gdfcinoagafkodbnkjemaajfahnmfkhg?hl=en</a></p><p><a href="https://chrome.google.com/webstore/detail/angular-watchers/nlmjblobloedpmkmmckeehnbfalnjnjk?hl=en">https://chrome.google.com/webstore/detail/angular-watchers/nlmjblobloedpmkmmckeehnbfalnjnjk?hl=en</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9a0200a8f5a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-one-time-binding-b9a0200a8f5a">Angular 1.x Performance Quick Tip: One-Time Binding</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 1.x Performance Quick Tip: ng-repeat + track by]]></title>
            <link>https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-ng-repeat-track-by-f2d7cf3bf9b9?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2d7cf3bf9b9</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[quick-tips]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Mon, 23 Jul 2018 13:14:11 GMT</pubDate>
            <atom:updated>2018-07-23T21:26:27.196Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*5ENqCWdwVOUoerGn.png" /></figure><p>As software developers, performance often becomes one of those things you don’t worry about until you have too, and it’s especially true when it comes to Front End performance. We often spend more time focusing on making our API’s snappy that we overlook how well the browser is responding to all that data you’re sending back.</p><p>To shine more light on the front end, I wanted to provide a quick tip for improving the performance of your Angular 1.x Application.</p><p>Even as I write this, I’m well aware that Angular 1.x is on its way out in favor of React and the later versions of Angular. However, the tradeoff here is that you can implement these performance improvements faster than you could if you choose to rebuild your UI React of Angular 2+.</p><p>Most of your performance hits are likely to be located within an ng-repeat segment. It’s one of the first things you’re probably learned how to do in Angular and then probably haven’t thought about it much since.</p><p>Here is an example.</p><pre>&lt;div class=&quot;chores-container&quot;&gt;<br>&lt;table&gt;<br> &lt;tbody&gt;<br> &lt;tr ng-repeat=&quot;chore in chores&quot;&gt;<br> &lt;td&gt;{{chore.assigned_to}}&lt;/td&gt;<br> &lt;td&gt;{{chore.name}}&lt;/td&gt;<br> &lt;td&gt;{{chore.done ? &#39;Done&#39; | &#39;To Do&#39;}}&lt;/td&gt;<br> &lt;td ng-show=&quot;chore.priority&quot;&gt;{{chore.priority}}&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;/tbody&gt;<br>&lt;/table&gt;<br>&lt;/div&gt;</pre><h3>The Problem</h3><p>So what kind of problem can arise from such a simple example? What would happen if you had 10 chores or 100 chores or maybe you’re a parent and have 2000+ chores?</p><p>Well, consider what would happen if you had to reload the data from the server, in each of those scenarios. Also, consider how you’re loading that data.</p><p>If you’re like most people, you’re probably using something like this $scope.chores = newChores; in your controller after you’ve received your response from the server.</p><p>It’s a simple way of loading your data, and the most common, but it can have drastically different outcomes depending on how much data you’re trying to load or how complicated your template is.</p><p>Without any changes, reloading data in this way would cause angular to take all the items in the ng-repeat, by removing all existing &lt;tr&gt; and associated &lt;td&gt; elements from the DOM and recreating them again by rerunning every associated function call, resynchronize all bindings, and make a ton of expensive DOM operations.</p><p>Rebuilding like this might not be a concern if you’re only working with 10 or maybe even 100 items, but keep increasing your dataset, and you’ll start to notice, and it only gets worse.</p><h3>The Solution</h3><p>The solution for this is straightforward; you add a track by to your ng-repeat so that it looks like this ng-repeat=&quot;chore in chores track by chore.id&quot;.</p><pre>&lt;div class=“chores-container”&gt;<br>&lt;table&gt;<br> &lt;tbody&gt;<br> &lt;tr ng-repeat=&quot;chore in chores track by chore.id&quot;&gt;<br> &lt;td&gt;{{chore.assigned_to}}&lt;/td&gt;<br> &lt;td&gt;{{chore.name}}&lt;/td&gt;<br> &lt;td&gt;{{chore.done ? ‘Done’ | ‘To Do’}}&lt;/td&gt;<br> &lt;td ng-show=“chore.priority”&gt;{{chore.priority}}&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;/tbody&gt;<br>&lt;/ul&gt;<br>&lt;/div&gt;</pre><p>Simple, right? Can we call it a day and go home?</p><h3>Why this works</h3><p>No, we can’t. Any software developer worth his salt needs to KNOW why something works, not just that it does.</p><p>When we don’t use the track by, the ng-repeat by default keeps track of items by appending a $$hashKey to each item. Angular goes through the process of removing and adding back everything, when $$hasKey is changed, or a new one is added.</p><p>Adding a new array from your server call which won’t have any $$hashKey ‘s in it, causes Angular to trigger ng-repeat to create a whole a new batch $$hashKey’s which triggers the full removal of existing items, and recreation of mostly duplicate items.</p><p>When we use the track by, we are telling angular to use a key that we specify. This limits the number of changes to only the new or changed items in the array, which is going to be very helpful when you’re trying to avoid reloading 1000+ items.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2d7cf3bf9b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/angular-1-x-performance-quick-tip-ng-repeat-track-by-f2d7cf3bf9b9">Angular 1.x Performance Quick Tip: ng-repeat + track by</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving Rails Performance | Part 3 | N+1]]></title>
            <link>https://medium.com/icapital-network-technology-group/improving-rails-performance-part-3-n-1-e752ef6e0c66?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/e752ef6e0c66</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby-on-rails-development]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Fri, 29 Jun 2018 20:29:50 GMT</pubDate>
            <atom:updated>2018-07-03T21:23:51.309Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://medium.com/@juancarlosgarzon/improving-rails-performance-part-1-execution-time-benchmarking-781c9c0410b3">Part 1</a> and <a href="http://Improving Rails Performance | Part 2 | Memory Benchmarking">Part 2</a> we learned how to measure performance. With that knowledge in hand, we can now tackle one of the most common performance plagues found in a rails app.</p><p>For those who aren’t familiar with the N +1 Problem. It is a common anti-pattern. In which unoptimized code results in excessive, duplicate or unnecessary SQL Queries that negatively impact performance.</p><p>In rails its simplest form looks something like this:</p><pre>(1..100).each do |id|<br> User.find(id)<br> //…<br>end</pre><p>Did you see it? The code looks simple enough, what could go wrong?</p><p>Let’s break it down and assume that User is an active model record. If we were to run through this loop once, it would run User.find(1), which would result in the following SQL is generated and executed.</p><pre>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 1</pre><p>If we follow the example above and then do this for each number between 1 and 100. Then a similar piece of SQL is run for each id. (See below)</p><pre>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 1<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 2<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 3<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 4<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 5<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 6<br>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE AND &quot;users&quot;.&quot;id&quot; = 7<br>//… until id = 100</pre><p>The problem with this approach is that each query comes with overhead. This overhead affects performance and escalates with the amount of data we are trying to load. Plainly put running a query once that returns 100 results is faster than running 100 queries with 1 result.</p><h3>Batching</h3><p>The first way to fix this problem is easy. We can batch our queries to load all the data we want up front. This involves using where() method instead of find().</p><p>With that strategy, we can rewrite:</p><pre>(1..100).each do |id|<br> User.find(id)<br> //…<br>end</pre><p>Like this:</p><pre>User.where(id: [1..100]).each do |user|<br> //…<br>end</pre><p>The generated query is much more straightforward.</p><pre>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE (&quot;users&quot;.&quot;id&quot; BETWEEN 1 AND 100)</pre><p>Keep in mind that the query generated can vary depending on the version of rails, and the flavor of SQL your using.</p><p>The query below is a variation of the same optimized query.</p><pre>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE &quot;users&quot;.id&quot; IN (1,2,3,4,5,6,7,8, …. 98, 99, 100)</pre><p>Don’t worry about which variation of the query is generated. The outcome is still the same, regardless of the length of query the outcome is still the same 1 Query for 100 Results.</p><p>With this small change, we eliminated the “N” from the N+1 Problem.</p><p>The where() method is where you should start your thinking when considering optimization. To avoid N+1 issues, you generally want to think about all the data you need up front and start determining the most effective queries.</p><h3>Eager Loading</h3><p>Batching is a simple fix but it’s far from a catch-all. You can still run into N+1 issues especially once you start working with models that have relationships with other models. For example, let’s say each User has 1 Firm. What would happen if we tried to access the Firm associated with a User model? Reusing the same batching code, we used above, and add a line where we access each user’s Firm.</p><p>Like this:</p><pre>User.where(id: [1..100]).each do |user|<br> user.firm<br> //…<br>end</pre><p>Will result in this SQL Output:</p><pre>User Load (6.7ms) SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE (&quot;users&quot;.&quot;id&quot; BETWEEN 1 AND 100)<br>Firm Load (1.9ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 1]]<br>Firm Load (2.2ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 1]]<br>Firm Load (3.9ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 1]]<br>Firm Load (4.6ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 2]]<br>Firm Load (1.8ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 2]]<br>Firm Load (5.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 2]]<br>Firm Load (0.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 2]]<br>Firm Load (0.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 2]]<br>Firm Load (0.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 3]]<br>Firm Load (0.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 3]]<br>Firm Load (0.3ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; = $1 LIMIT 1 [[&quot;id&quot;, 3]]<br>//… until end of loop</pre><p>Our first line is what we expect, from batching our users.</p><pre>SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE (&quot;users&quot;.&quot;id&quot; BETWEEN 1 AND 100)</pre><p>However, since we are now calling each Firm individually for each User, we introduce the N+1 problem again. In this case, the problem is worse than you might think. Not only are we loading each firm one at a time, but the same Firm can belong to different users. Which results in repeated loaded and reloading of a Firm into memory unnecessarily. Thus, taking up precious resources and impacting our performance.</p><p>So how do we fix this?</p><p>Luckily every ActiveRecord comes with a handy includes() method. This method tells Rails to preload the association of an ActiveRecord.</p><p>It’s called eager loading, and it allows us to access a Firm associated with a User without needing to reload the same Firm.</p><p>Let’s write our previous query and add the includes(). Keep in mind that the includes() method needs an argument passed to it. The arguments you would pass to it would be the same symbols you use to describe an association in a Models. The same ones you use via belongs_to, has_many, has_one, and so on.</p><p>In this example, we are assuming the User model has a belongs_to :firm in the User model.</p><pre>User.where(id: [1..100]).includes(:firm).each do |user|<br> user.firm<br>end</pre><p>Output</p><pre>User Load (6.7ms) SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE (&quot;users&quot;.&quot;id&quot; BETWEEN 1 AND 100)<br>Firm Load (2.2ms) SELECT &quot;firms&quot;.* FROM &quot;firms&quot; WHERE &quot;firms&quot;.&quot;id&quot; IN (1, 2, 3, 4, 5)</pre><p>You can see once we run it this time, N+1 is one and is replaced by a similar query to that of User. Rails is smart and depending on the need sometimes you’ll see a separate call, and other times Rails might use a JOIN, resulting in just 1 query, instead of two. Either way, our code is optimized.</p><p>Our example above looks pretty simplistic, but the includes() method is surprisingly versatile. You can nest associations, and much like the movie Inception, you can go many levels deep.</p><p>You could eagerly load an association a few levels deep. For example, if you wanted to load a model that is only associated with a Firm. You could still use the same includes() method and eager load nested associations and avoid the N+1. This topic gets complex fast, so for more information about eager loading, I recommend using the “<a href="http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations">Ruby on Rails Guide</a>” it’s an excellent introduction into the using the includes() method.</p><p>However, the last thing you might consider when it comes to using eager loading is that there is memory cost in preloading hundreds or thousands of records. Most of the time you’ll be fine, but any site that generates or stores ton of data has to worry about its memory consumption.</p><p>In those cases, you’ll want to try a more selective approach by using select() method. This method allows us to load just the data you want selectively, but I’ll save that for a different article. You can read more about it <a href="http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields">here</a></p><h3>Detecting the Problem</h3><p>Learning to detect the performance problems in your apps is an essential skill for any developer. The most important thing for me is to pay attention to my logs while I’m testing. As you saw in our examples above, a telltale sign of the N+1 is the repeating or duplicate or similar SQL.</p><p>They are easy to spot because you’ll find them bundled together, or nested as a set of repeated queries. Pay particular attention to your logs when writing and testing a new endpoint and use benchmarks to ensure you’re getting a performance increase.</p><p>You can also use the “Bullet” gem. It does an excellent job of pointing out the N+1’s your app. It will also give you recommendations on where to use includes, and where to its best to remove them. It’s a valuable gem but it is not a catch-all, and you should always work on building up your ability.</p><p>For more about the Bullet Gem go to <a href="https://github.com/flyerhzm/bullet">https://github.com/flyerhzm/bullet</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e752ef6e0c66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/improving-rails-performance-part-3-n-1-e752ef6e0c66">Improving Rails Performance | Part 3 | N+1</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving Rails Performance | Part 2 | Memory Benchmarking]]></title>
            <link>https://medium.com/icapital-network-technology-group/improving-rails-performance-part-2-memory-benchmarking-2c490a2bc789?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c490a2bc789</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[profiling]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Fri, 16 Feb 2018 16:25:43 GMT</pubDate>
            <atom:updated>2018-02-16T16:25:43.545Z</atom:updated>
            <content:encoded><![CDATA[<p>There comes a time in the course of development, when we realize how poorly optimized our code is. Ideally, we aim to do this as we code, but as we know, that’s not always the case. The pressure to get things done can and does often win over getting things done optimally.</p><p>Tasked with that mission, I decided to document my efforts, and just as importantly MEASURE performance, and share it with the developer community.</p><p>All my examples and tools are coming from Rails 4.2.7.1 + Angular 1.x perspective. But most of the general ideas are platform agnostic, and useful regardless of tech stack.</p><h3>Benchmarking</h3><p>So what is Benchmarking? Put simply benchmarking is the process gathering and comparing performance metrics.</p><p>In software development, there are two metrics in particular that we pay attention too. Those are execution time and memory. These are so important that there is a mathematical notation that is used in computer science to classify and analyze algorithms. If you don’t know it, I suggest you brush up on Big O notation, understanding these concepts should be your first line of defense toward writing code that performs faster and consumes less memory. I won’t go into here, but I may touch on this here or there.</p><h3>Simple guide to Memory Profiling</h3><h4>Install the profiler</h4><p>In Part 1, we performed execution time benchmarking. The other factor in benchmarking is memory consumption. However, unlike execution time, in Ruby memory consumption requires a gem to be installed. You can get that by adding gem ‘memory_profiler’ to your gemfile, or installing it globally with gem install memory_profiler’ in your terminal.</p><p>Then just require the profiler in the class your testing.<br>require ‘memory_profiler’</p><h4>Set your $stdout</h4><p>To make your life easier, set your $stdout. Doing this will log your results to a log file of your choosing, and will make it that much easier for you see your results.</p><pre>$stdout = File.new(‘log/mem_perf.log’, ‘a+’)<br>$stdout.sync = true</pre><p>Keep in mind any `puts`, or `print` command will be logged in this file.</p><h4>Now Benchmark your code</h4><p>For memory profiling, you can call this block multiple times, but you won’t want too; you’ll see why when we view the results.</p><pre>report = MemoryProfiler.report do<br> users = User.all<br> # Insert code here<br>end</pre><h4>View the results</h4><p>Unlike the Benchmark module, which outputs the result automagically. The Memory Profiler needs to be explicitly told to write the result. Luckily Memory Profiler provides a helper method for that.</p><pre>report.pretty_print</pre><p>With the code in place, and $stout set. The only thing left to do is run your code.</p><p>From there all need to do is open log/mem_perf.log (if you used my example) and see your results. What you will see will be very intimidating, its long and throws a lot of information at you. Here is where you get the most benefit out of setting the $stout. Don’t get bogged down by all the info, for basic memory benchmarking purposes, just pay attention to the first two lines.</p><pre>Total allocated: 6856 bytes (127 objects)<br>Total retained: 2512 bytes (31 objects)</pre><pre>allocated memory by gem<br> — — — — — — — — — — — — — — — — — -<br> 6496 activerecord-4.2.7.1<br> 160 awesome_print-1.7.0<br> 120 arel-6.0.4<br> 80 acts_as_paranoid-0.5.0</pre><pre>allocated memory by file<br> — — — — — — — — — — — — — — — — — -<br> 1800 /Users/juangarzon/.rvm/gems/ruby-2.3.5/gems/activerecord-4.2.7.1/lib/active_record/relation.rb<br> 1512 /Users/juangarzon/.rvm/gems/ruby-2.3.5/gems/activerecord-4.2.7.1/lib/active_record/scoping.rb<br> 1200 /Users/juangarzon/.rvm/gems/ruby-2.3.5/gems/activerecord-4.2.7.1/lib/active_record/relation/query_methods.rb<br> 512 /Users/juangarzon/.rvm/gems/ruby-2.3.5/gems/activerecord-4.2.7.1/lib/active_record/relation/spawn_methods.rb<br> 480 /Users/juangarzon/.rvm/gems/ruby-2.3.5/gems/activerecord-4.2.7.1/lib/active_record/relation/merger.rb<br>etc… etc…</pre><p>The report breaks down 2 key concepts. Total allocated, and Total retained.</p><p><strong>Retained</strong>: Is long-lived memory use and object count retained due to the execution of the code block.<br><strong>Allocated</strong>: Is all object allocation and memory allocation during code block.</p><p>Here is where you have to use your own judgment, and experience to determine how much is too much memory usage. I like to see how much memory is used when I’m eager loading heavy models in Rails.</p><p>For more information about these tools. Visit the memory profiler <a href="https://github.com/SamSaffron/memory_profiler">github page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c490a2bc789" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/improving-rails-performance-part-2-memory-benchmarking-2c490a2bc789">Improving Rails Performance | Part 2 | Memory Benchmarking</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving Rails Performance | Part 1 | Execution Time Benchmarking]]></title>
            <link>https://medium.com/icapital-network-technology-group/improving-rails-performance-part-1-execution-time-benchmarking-781c9c0410b3?source=rss-5783c5259189------2</link>
            <guid isPermaLink="false">https://medium.com/p/781c9c0410b3</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Juan Carlos Garzon]]></dc:creator>
            <pubDate>Thu, 08 Feb 2018 16:43:50 GMT</pubDate>
            <atom:updated>2018-03-23T17:02:31.076Z</atom:updated>
            <content:encoded><![CDATA[<p>There comes a time in the course of development, when we realize how poorly optimized our code is. Ideally, we aim to do this as we code, but as we know, that’s not always the case. The pressure to get things done can and does often win over getting things done optimally.</p><p>Tasked with that mission, I decided to document my efforts, and just as importantly MEASURE performance, and share it with the developer community.</p><p>All my examples and tools are coming from Rails 4.2.7.1 + Angular 1.x perspective. But most of the general ideas are platform agnostic, and useful regardless of tech stack.</p><h3>Benchmarking</h3><p>So what is Benchmarking? Put simply benchmarking is the process gathering and comparing performance metrics.</p><p>In software development, there are two metrics in particular that we pay attention too. Those are execution time and memory. These are so important that there is a mathematical notation that is used in computer science to classify and analyze algorithms. If you don’t know it, I suggest you brush up on Big O notation, understanding these concepts should be your first line of defense toward writing code that performs faster and consumes less memory. I won’t go into here, but I may touch on this here or there.</p><p>To perform a benchmark, we will need to measure. Like any good carpenter who has their own set of tools, so must a developer. The good news is that Ruby comes with a Benchmark module baked in, which will help us measure execution time.</p><h3>Simple guide on Execution Time Benchmarking</h3><ul><li><strong>Require and Include the module</strong></li></ul><pre>require ‘benchmark’<br>include Benchmark</pre><ul><li><strong>Set your $stdout</strong><br>To make your life easier, set your $stdout. Doing this will log your results to a log file of your choosing, and will make it that much easier for you see your results.</li></ul><pre>$stdout = File.new(‘log/perf.log’, ‘a+’)<br>$stdout.sync = true</pre><p>Keep in mind any puts, or printcommands will be logged in this file.</p><ul><li><strong>Performing the Benchmark test</strong></li></ul><p>For Execution Time benchmarking, you have the advantage of being able to test more than one block of code at a time.</p><p>To benchmark you’ll need to start by calling the Benchmark class, we’re going use the bm method. It provides a simple interface to generate sequential reports with labels. You can see the documentation for the module <a href="https://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html#method-c-benchmark">here</a>.</p><p>This is what it looks like:</p><pre>Benchmark.bm(7, “&gt;total:”) do |x|<br> le = x.report(“line:”) { # Insert you code here }<br> cs = x.report(“custom:”) do # Insert your code here<br> end<br> # This will add up each report, and display it as a total</pre><pre> [le + cs] <br>end</pre><ul><li><strong>View the results</strong></li></ul><p>So let’s run these benchmark testing using real code. Let’s measure the difference between counting from 1 to 5000000, between using three different kinds of counting loops.</p><pre>Benchmark.bm(7, “&gt;total:”) do |x|<br> forx = x.report(“for:”) { for i in 1..5000000; a = “1”; end }<br> times = x.report(“times:”) { 5000000.times do ; a = “1”; end }<br> upto = x.report(“upto:”) { 1.upto(5000000) do ; a = “1”; end }</pre><pre> [forx + times + upto]<br>end</pre><p>With the code in place, and $stdout set. The only thing left to do is run your code.</p><p>From there all need to do is open log/perf.log (if you used my example) and see your results. The report should look something like this</p><pre>user system total real<br>for: 4.180000 0.050000 4.230000 ( 4.303220)<br>times: 4.530000 0.040000 4.570000 ( 4.717092)<br>upto: 4.140000 0.030000 4.170000 ( 4.215828)<br>&gt;total: 12.850000 0.120000 12.970000 ( 13.236140)</pre><p>This report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. They measure time in seconds.</p><p>With that done, you can now wash rinse repeat with your code and start performance testing the code execution time.</p><p>For more information about these tools. <br>Visit the Benchmark <a href="https://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html">documentation page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=781c9c0410b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/icapital-network-technology-group/improving-rails-performance-part-1-execution-time-benchmarking-781c9c0410b3">Improving Rails Performance | Part 1 | Execution Time Benchmarking</a> was originally published in <a href="https://medium.com/icapital-network-technology-group">iCapital Network Technology Group</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>