<?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 Ajay Poshak on Medium]]></title>
        <description><![CDATA[Stories by Ajay Poshak on Medium]]></description>
        <link>https://medium.com/@poshakajay?source=rss-4864fb3a68e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YNpyuJVwPTE5c04I.</url>
            <title>Stories by Ajay Poshak on Medium</title>
            <link>https://medium.com/@poshakajay?source=rss-4864fb3a68e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 13:23:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@poshakajay/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[Here’s how I reduced my bundle size by 90%]]></title>
            <link>https://medium.com/@poshakajay/heres-how-i-reduced-my-bundle-size-by-90-2e14c8a11c11?source=rss-4864fb3a68e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e14c8a11c11</guid>
            <category><![CDATA[web-performance]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webpack4]]></category>
            <dc:creator><![CDATA[Ajay Poshak]]></dc:creator>
            <pubDate>Sat, 23 Jun 2018 13:46:25 GMT</pubDate>
            <atom:updated>2018-09-06T15:36:03.193Z</atom:updated>
            <content:encoded><![CDATA[<p>While working on a side project, I realized that I was shipping the way more code to client than I should be. Bundle size was huge. So I looked for some optimizations.</p><p>On the front of performance also, it was taking 3.25 secs for first meaningful paint even on Wifi connection. So my big bundle was hitting harder to my web performance also.</p><p>And when it finished with easy to do optimizations, I was looking at bundle size 90% less as compared to size when I started.</p><p>Here is the initial webpack config</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adbd55d02e340a0134621109e9a33005/href">https://medium.com/media/adbd55d02e340a0134621109e9a33005/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6W9IyJvgpdxdRLCz8Fg_Pw.png" /></figure><p><strong>Change mode to production</strong></p><p>By changing mode to production, it tells webpack to use its built-in optimizations. Such using uglifyjs for uglification and minification. So passing mode as production , reduced my bundle size by almost 50%.</p><blockquote>Production mode sets process.env.NODE_ENV to production for build process. So that any dependency which relies on mode detection for optimization, can do it during build.</blockquote><p>webpack --mode production --config ./webpack.client.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6W9IyJvgpdxdRLCz8Fg_Pw.png" /><figcaption>main bundle size reduced, thanks to Production mode !!!</figcaption></figure><p>Additional Readings:</p><p><a href="https://webpack.js.org/concepts/mode/">Production Mode explained</a></p><p><strong>Split chunks</strong></p><p>A lot of my code contained node_modules, so creating a vendor chunk with all the node_modules in it, would help further reduce the main bundle size.</p><blockquote>cacheGroups in SplitChunks lets you define various caching groups. For creating a group of vendor chunks, we set test to contain all modules whose path contains node_modules in it. When a chunk name is matched, all modules in that chunk are selected. chunks option tells from where the chunks can be shared, it can be initial if it has to be shared in sync chunks, or async for async chunks, or all for both sync and async chunks.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eAGpM_1sBl7w_zIEf0k6SA.png" /><figcaption>Separating vendors chunk</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/576eb22fda357c0cf4e36ce1d3de0a01/href">https://medium.com/media/576eb22fda357c0cf4e36ce1d3de0a01/href</a></iframe><p>Further Reading:</p><p><a href="https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366">Split Chunks Plugin</a></p><p><a href="https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693">RIP CommonsChunkPlugin</a></p><p><strong>Remove dev tools mapping</strong></p><p>We write ES6, and transpile it to browser compatible code(ES 5) using Babel. Then webpack concatenates all my files into a single chunk. Now to debug my original JS files in browser, I had enabled mapping of modules. Now this is very helpful during development, but for production these mappings create a lot of overhead for bundles. So in order to further optimize the bundle size, I removed dev tools mappings altogether from production mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SSwJivImxweoCEJfUXKV1A.png" /><figcaption>After removing dev-tools mappings</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f675c268938e8821006a8c4e3c4446f8/href">https://medium.com/media/f675c268938e8821006a8c4e3c4446f8/href</a></iframe><p>Further Reading:</p><p><a href="https://webpack.js.org/configuration/devtool/">Devtool Mappings</a></p><p><strong>Compression</strong></p><p>Now, I wanted to compress it to further reduce the bundle size. Although, gzip compression is more popular, I tried brottli , a compression algorithm developed by Google. It is more efficient as compared to gzip. I used brotli-webpack-plugin to achieve it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*saUZlCHahHIS5tVQIEgR9g.png" /><figcaption>.br extension indicates file compressed by brotli</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55b738432e57339733963f29f232c730/href">https://medium.com/media/55b738432e57339733963f29f232c730/href</a></iframe><p>Further Reading:</p><p><a href="https://opensource.googleblog.com/2015/09/introducing-brotli-new-compression.html">Introduction to Brottli</a></p><p><a href="https://medium.com/@samparkinson_/brotli-compression-34e436efca0">Brotli Compression</a></p><p><strong>End Note</strong></p><p>I started with bundle size 2.97 MiB , and reduced it to 64 KiB (vendor and main bundle combined). Reduced bundle size helps a lot in performance tuning. It results in faster page load. Initially it was taking 3.25 secs for First Meaningful paint, which now reduced to 1.02 secs . That’s a huge win in performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1cOKTujQJZhzwRYAl_DdUA.png" /><figcaption>No CPU throttling, Wifi connection</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nN5LPabp4A6n2VQPqVsMpQ.png" /><figcaption>No CPU throttling; Fast 3G</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjBfaxazJeK4c-1wwZSkDQ.png" /><figcaption>4X CPU slowdown; Fast 3G (First Meaningful Paint — 1.2 seconds)</figcaption></figure><p>Further optimizations can be done by extracting runtime chunk, and inlining it in html response generated on server. See complete webpack configuration <a href="https://gist.github.com/AjayPoshak/e41ec36d28437494d10294256e248bc6">here</a>.</p><p>If you liked this story, please give it a clap, and follow me on medium or <a href="https://twitter.com/poshakajay">twitter</a> for more such articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e14c8a11c11" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prototype Pattern in JS]]></title>
            <link>https://medium.com/we-are-bookmyshow/prototype-pattern-in-js-5c1f44440081?source=rss-4864fb3a68e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c1f44440081</guid>
            <category><![CDATA[prototype-chain]]></category>
            <category><![CDATA[bms-engineering-mobile]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ajay Poshak]]></dc:creator>
            <pubDate>Sat, 20 Jan 2018 16:35:18 GMT</pubDate>
            <atom:updated>2018-06-07T08:53:53.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Prototypes in javascript have been an integral part of the language for a long time. They stay in object chain and can be looked up for properties.</p><p><strong>How Prototype works?</strong></p><blockquote>Whenever an object is created, by default it points to some prototypes. Thus, every object has a prototype linked to it.</blockquote><p>Let us say we create an array, let arr = []; the array prototype object gets attached to it so it has access to Array functions such as find, foreach, includes etc. So when we iterate over arr using map function, then it searches on arr for map function, when it is not found here, the search is continued to next prototype in a chain, and as Array prototype is attached to reference chain which has map function, the search stops there and map function is invoked.</p><p>The same thing happens when we create an object, the object prototype gets attached to it and we can access object properties as toString, valueOf, hasOwnProperty.</p><p><strong>Usage of Prototypes</strong></p><p>JS uses these prototypes to achieve inheritance. When an object gets attached to prototype chain of another object then its properties and methods are available in that object. This way to achieve inheritance is also known as <strong>delegation</strong>.</p><p><strong>The Prototype Pattern</strong></p><p>The ability of prototypes to be accessed in other objects can be used to create object templates thus effectively serving as a blueprint to create objects. This is broadly known as Prototype pattern. It relies on prototype’s ability to be available for reference lookups.</p><blockquote>Attach a method or property to a prototype and it will be available to all instances of that prototype.</blockquote><p><strong>Prototype.Example()</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a990574da091491ae308e9677ae8054d/href">https://medium.com/media/a990574da091491ae308e9677ae8054d/href</a></iframe><p>This function Notification also serves as a constructor to initialize properties. Prototype declaration is done using <em>prototype </em>keyword. All the methods attached to prototype can access the properties using <em>this </em>keyword.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bfbbce2a6e3c34046b8079473a8e9f7/href">https://medium.com/media/4bfbbce2a6e3c34046b8079473a8e9f7/href</a></iframe><p>In order to use it, we need to instantiate it using the new keyword as we do in case of other objects. So our Notification function now serves as a template to instantiate objects. In other words, this prototype function serves as a parent and all the objects instantiated by it are its children. It shares its properties and methods among them.</p><p>The new ES6 classes are also a syntactic sugar on prototype pattern. They utilize the underlying prototypes to achieve inheritance.</p><p><strong>Benefits of Prototype Pattern</strong></p><p>One obvious benefit is that it makes the code more structured, readable and organized. Having a single template to instantiate objects can lead to fewer spaghetti code.</p><p>And it comes with a performance boost, as all the methods defined via prototype are created by reference instead of having their own separate copies. Instead of keeping common variables global which can pollute the global namespace, these can be referred by using <em>this </em>inside prototype methods.</p><p><strong>Anti-Pattern</strong></p><p>Extending native prototypes such as Array, Object etc is considered anti-pattern, as it doesn’t compose nicely with other libraries and third-party code.</p><p>Although several polyfills do that, still avoid doing it until you’re sure about what you’re doing.</p><p>The reason is simple, suppose you want to create a cool method which finds if an array contains ‘foo’ string. So you attach this method to Array.prototype so that it can be used across all arrays. Still, your method is non-standard.</p><p>So if somehow TC-39 gets this epiphany to create a ‘foo’ method on Array, and browser ships this upgrade, then either your application will break or you’ll be stuck with a non-standard implementation.</p><p>This case led to an interesting case #<em>SmooshGate</em>. In case, you didn’t follow, read <a href="https://developers.google.com/web/updates/2018/03/smooshgate">here</a> in details.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c1f44440081" width="1" height="1" alt=""><hr><p><a href="https://medium.com/we-are-bookmyshow/prototype-pattern-in-js-5c1f44440081">Prototype Pattern in JS</a> was originally published in <a href="https://medium.com/we-are-bookmyshow">We Are BookMyShow</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>