<?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 Valeriy Baranov on Medium]]></title>
        <description><![CDATA[Stories by Valeriy Baranov on Medium]]></description>
        <link>https://medium.com/@baranovxyz?source=rss-9bef77b73736------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*GcZ9tJYU6lOVhRjrlb7zUA.jpeg</url>
            <title>Stories by Valeriy Baranov on Medium</title>
            <link>https://medium.com/@baranovxyz?source=rss-9bef77b73736------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:37:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baranovxyz/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[How to Make a Simple Notes Viewer App with Vanilla JavaScript]]></title>
            <link>https://javascript.plainenglish.io/how-to-make-a-simple-notes-viewer-app-with-vanilla-javascript-25129b146ec3?source=rss-9bef77b73736------2</link>
            <guid isPermaLink="false">https://medium.com/p/25129b146ec3</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[learn-to-code]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Valeriy Baranov]]></dc:creator>
            <pubDate>Sat, 06 Jun 2020 07:35:22 GMT</pubDate>
            <atom:updated>2020-06-06T07:35:22.688Z</atom:updated>
            <content:encoded><![CDATA[<h4>LEARN TO CODE</h4><h4>For those who are just starting out with JavaScript</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NsZiT03H--K7FMQr81-_pw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@reinhartjulian?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Reinhart Julian</a> on <a href="https://unsplash.com/s/photos/studying?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>A colleague asked my advice today. He wanted to display notes from a json file. He also wanted to filter and sort notes. He told me that he probably wants to use handlebars for templating, and that he is already using jQuery.</p><blockquote>- Maybe I should use something like lodash? Could you recommend some simple solution? — he asked.</blockquote><p>My colleague is not a JavaScript developer. He wants to build something useful for himself. JavaScript is often overwhelming, especially for those who are just starting out.</p><p>This article will allow you to make a simple useful application with vanilla JavaScript. No handlebars, no jquery, no lodash. I argue that you don’t need those libraries if you are just starting out — learn vanilla JavaScript first.</p><p>You can play with the resulting notes app in the sandbox (link in the end). Or you can checkout <a href="https://github.com/baranovxyz/simple-notes">this</a> GitHub repo.</p><h3>HTML file</h3><p>First, let&#39;s make the simplest possible html file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4128baa005357cc7f95f62743dae0953/href">https://medium.com/media/4128baa005357cc7f95f62743dae0953/href</a></iframe><p>When browser loads this file, it requests stylesheet from src/styles.css and requests and executes JavaScript from src/script.js file.</p><h3>Script file</h3><p>Our script file is placed just before the end of the body tag in HTML. When it runs body already has input element and div with id notes.</p><p>We will render notes in a div with id notes. To do this, we need to get notes element with const notesEl = document.<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector">querySelector</a>(&#39;#notes&#39;);. No jquery needed. Now we have a reference to notes element. We can replace what is inside of it with builtin method notesEl.<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML">innerHtml</a>.</p><p>To render notes we use render function. It is very simple. We map over notes, for each we return html string and join them together. No lodash needed to map over an array, we use builtin <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">map</a> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0284aa55570b13d5cea56b5474ba365c/href">https://medium.com/media/0284aa55570b13d5cea56b5474ba365c/href</a></iframe><p>We used function Note in the code above. It gets a note and returns an html string. It is the very simple template function. No handlebars needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5df9aebf57b6b6c2c687aff5b7428936/href">https://medium.com/media/5df9aebf57b6b6c2c687aff5b7428936/href</a></iframe><p>To fetch notes, we use native <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">fetch</a> api. It is a simple app, so we only fetch notes once — when script is loaded. fetch api returns a promise, and we get json from it and assign to notes variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9d869162f346ca4997fff9382caa435/href">https://medium.com/media/b9d869162f346ca4997fff9382caa435/href</a></iframe><p>To add filtering, we get reference to filter input with the const filterEl = document.querySelector(&#39;#filter&#39;);. And we listen for keyup events on this element. For each event, we execute render function filterEl.addEventListener(&#39;keyup&#39;, render);.</p><p>Now we modify render function to use text from filter input. If filter input is empty, we show all notes. If it has some text, we show only notes where title or text <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes">includes</a> filter text.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0163149ae5ccbd64b16c7ebb6f4bfd3e/href">https://medium.com/media/0163149ae5ccbd64b16c7ebb6f4bfd3e/href</a></iframe><p>Now when you type in filter input field, you will see real-time updates.</p><h3>Styles file</h3><p>To style our app, we use css. As you can see from the template function Note, each note title has class Note-Title, and each note text element has class Note-Text. We can use class selectors to style them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e1a3194157b878053afe255e8e23eef/href">https://medium.com/media/8e1a3194157b878053afe255e8e23eef/href</a></iframe><p><strong><em>Thats it! Now we have a working application with basic styling.</em></strong></p><p>We didn’t implement sorting in this app. Try to do it yourself in the sandbox below.</p><blockquote><strong>If you are stuck with some issue:</strong> type <em>mdn your question</em> into any search engine. Start with MDN and not with StackOverflow. After reading the MDN documentation, you will get up-to-date solutions. After reading on StackOverflow, you may get outdated solutions to your problems.</blockquote><p>You can play with the code in the sandbox below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fn5ddt&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fn5ddt&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fn5ddt%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/419b0e42a07a5f433abb27d58b2ee7b9/href">https://medium.com/media/419b0e42a07a5f433abb27d58b2ee7b9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25129b146ec3" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-make-a-simple-notes-viewer-app-with-vanilla-javascript-25129b146ec3">How to Make a Simple Notes Viewer App with Vanilla JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Calculate MongoDB Document Size in Node.js]]></title>
            <link>https://medium.com/better-programming/how-to-calculate-mongodb-document-size-in-node-js-f463b8457f27?source=rss-9bef77b73736------2</link>
            <guid isPermaLink="false">https://medium.com/p/f463b8457f27</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[mongo]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Valeriy Baranov]]></dc:creator>
            <pubDate>Wed, 27 May 2020 11:18:48 GMT</pubDate>
            <atom:updated>2020-05-27T12:49:04.878Z</atom:updated>
            <content:encoded><![CDATA[<h4>Without the bson module</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9JoNmvYzhiZvzof0rZBgtA.png" /></figure><p>In my last article, <a href="https://medium.com/better-programming/how-to-store-documents-larger-than-16-mb-in-mongodb-aecc957bbe6c">How to Store Documents Larger Than 16Mb in MongoDB</a>, I suggested an approach to store a large document in linked documents. To do this, we need to calculate the size of the document in runtime to know when to split it and save it to MongoDB. I used a very naive approach to calculate size on the fly.</p><p>In this article, I will make a function that calculates the exact number of bytes a document is. There’s one caveat — I will do it for the column type from the last article. For simplicity, I changed the column type a bit below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/642ca4e9b7274fb70660560e8ca06ba8/href">https://medium.com/media/642ca4e9b7274fb70660560e8ca06ba8/href</a></iframe><p>Documents are stored in MongoDB in BSON format. You can guess, that BSON is a Binary JavaScript Object Notation or Binary JSON for short. It originated at MongoDB — here’s its <a href="http://bsonspec.org/spec.html">spec</a>.</p><p>We will use a bson module to explore the BSON format. Let’s install it first with npm install bson. Then we require ìt with const BSON = require(&#39;bson&#39;);. We’re good to go.</p><p>Let’s start exploring how documents are encoded with BSON.</p><h3><strong>Empty Object Encoding</strong></h3><p>Let&#39;s start with the simplest document you have — an empty object. Serialize it and log to a console:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bdbe2d056c91c673dbe6144b20c642d/href">https://medium.com/media/3bdbe2d056c91c673dbe6144b20c642d/href</a></iframe><p>We can see that there is a buffer with 5 bytes in it. This is the smallest valid BSON document.</p><p>Let’s look at how the empty object is encoded: 05 00 00 00 00</p><ol><li>05 00 00 00: The first four bytes is actually an int32 number. This is the size (5 bytes) of a BSON document, including the document itself and a document terminator.</li><li>00: The last byte stands for a document terminator.</li></ol><p><strong><em>Implementation detail:</em></strong><em> bytes are written in little-endian notation, where the least significant byte goes first.</em></p><h3>Number Encoding</h3><p>Next, let’s look at a valid MongoDB document, that can be saved into a database. It only has one key-value pair in it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f41b5706fa5b380d69374a13d2439e3/href">https://medium.com/media/5f41b5706fa5b380d69374a13d2439e3/href</a></iframe><h4><strong>How the number key-value pair is encoded</strong></h4><p>0e 00 00 00 10 5f 69 64 00 01 00 00 00 00</p><ol><li>0e 00 00 00 document size is 14 bytes.</li><li>10 5f 69 64 00 01 00 00 00 key-value pair.</li><li>00 last byte stands for a document terminator.</li></ol><p>All BSON documents are stored in binary format, where the first four bytes is the document size and the last byte is a document terminator. In between are the key-value pairs.</p><h4><strong>Let’s analyze the key-value pair from above</strong></h4><p>10 5f 69 64 00 01 00 00 00</p><ol><li>10 The first byte is a value type — int32 in this case (you can look at the specification link above).</li><li>5f 69 64 any number of the following bytes is a key.</li><li>00 The key terminator.</li><li>01 00 00 00 The last four bytes are a value — we know that it takes four bytes because it is int32.</li></ol><p>We have a value type, but where is the key type?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xpbg9kPSIOv-flCUChv5xw.jpeg" /></figure><p><strong><em>Implementation detail: </em></strong><em>the key is always a string. As a result, we don’t need to add to BSON a type for a key — we know it’s a string.</em></p><h3><strong>String Type</strong></h3><p>Let’s look at how key-value pairs with strings as values are encoded.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52b3871f979bd5647b56bb9d209b5c64/href">https://medium.com/media/52b3871f979bd5647b56bb9d209b5c64/href</a></iframe><h4><strong>How the string key-value part is encoded</strong></h4><p>02 74 79 70 65 00 07 00 00 00 73 74 72 69 6e 67 00</p><ol><li>02 The first byte is a value type, string in this case.</li><li>74 79 70 65 key type.</li><li>00 The key terminator.</li><li>07 00 00 00 For a string type length of a string value is written in int32. The length includes one byte for the string value terminator.</li><li>73 74 72 69 6e 67 our encoded string.</li><li>00 a string value terminator.</li></ol><p><strong>Implementation detail: </strong>string is encoded in UTF-8, which means every character takes between one to four bytes to encode. Latin alphabet, numbers, and some other things take one byte to encode. Some non-English language characters take two bytes to encode. Chinese hieroglyphs take three bytes. Emojis take four bytes. To calculate the size of the encoded string, we can use the nodejs buffer method: Buffer.from(str).size.</p><h3><strong>Array Type</strong></h3><p>Let’s go further and examine how arrays are encoded.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa8c936f7c34e89d6b352ba511ee1dcd/href">https://medium.com/media/fa8c936f7c34e89d6b352ba511ee1dcd/href</a></iframe><h4><strong>How the array key-value part is encoded</strong></h4><p>04 76 61 6c 75 65 73 00 12 00 00 00 02 30 00 06 00 00 00 66 69 72 73 74 00 00</p><ol><li>04: The array document type.</li><li>76 61 6c 75 65 73 00: The key values with a terminator 00.</li><li>12 00 00 00 02 30 00 06 00 00 00 66 69 72 73 74 00 00: The encoded array. It’s a full-blown BSON document.</li></ol><h4><strong>Let&#39;s look at how array </strong><strong>[&#39;first&#39;] is encoded</strong></h4><p>12 00 00 00 02 30 00 06 00 00 00 66 69 72 73 74 00 00</p><ol><li>12 00 00 00: The document length is 12 bytes.</li><li>02 30 00 06 00 00 00 66 69 72 73 74 00: The encoded key-value`.</li><li>00 a document terminator.</li></ol><p>But wait, why the key-value pair if we only had one element in the [&#39;first’] array?</p><p><strong><em>Implementation details:</em></strong><em> Arrays are stored as objects in BSON, so </em><em>[&#39;first&#39;] is actually saved as </em><em>{ &#39;0&#39;: &#39;first&#39; }.</em></p><h3><strong>Null Type</strong></h3><p>In a Column.values array I can have strings and nulls, so I need to know how nulls are stored as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db6b2ee9641cf20564be8ca94f29ad46/href">https://medium.com/media/db6b2ee9641cf20564be8ca94f29ad46/href</a></iframe><p><strong>How the null key-value part is encoded:<br></strong>0a 30 00</p><ol><li>0a null type code.</li><li>30 string key 0 followed by</li><li>00 a key terminator.</li></ol><p>As you see, it is very efficient to store null&#39;s in BSON format. We do not need to provide a value, it’s obvious from its type.</p><p>Now we have enough information to make a function that calculates BSON size for theColumn interface. MongoDB limits the size of the document to 16Mb, which is 16777216 bytes.</p><h3><strong>Calculate BSON Size Function</strong></h3><p>In the snippet below is a function I wrote to calculate the exact size of the column. It’s for demonstration purposes, just to show you how it’s possible to write a function when you know the specification.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cd0dcd1170968a1f0ddb8e7db46062f/href">https://medium.com/media/0cd0dcd1170968a1f0ddb8e7db46062f/href</a></iframe><p>The function we use in production is actually using the bson module to calculate object size with an empty array. Then, while adding new elements, we keep track of the document size.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15fbce42167873245f7121d4c7d39ad0/href">https://medium.com/media/15fbce42167873245f7121d4c7d39ad0/href</a></iframe><h3>Conclusion</h3><p>With the calculation of theBSON size in runtime, we can split a column into chunks efficiently.</p><p>I will be writing about other options to reduce the size of MongoDB documents in future articles, so stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f463b8457f27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-calculate-mongodb-document-size-in-node-js-f463b8457f27">How to Calculate MongoDB Document Size in Node.js</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Store Documents Larger Than 16 MB in MongoDB]]></title>
            <link>https://medium.com/better-programming/how-to-store-documents-larger-than-16-mb-in-mongodb-aecc957bbe6c?source=rss-9bef77b73736------2</link>
            <guid isPermaLink="false">https://medium.com/p/aecc957bbe6c</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Valeriy Baranov]]></dc:creator>
            <pubDate>Mon, 18 May 2020 13:52:03 GMT</pubDate>
            <atom:updated>2020-05-18T13:52:03.997Z</atom:updated>
            <content:encoded><![CDATA[<h4>A simple approach without using GridFS</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZd0V3kX1TEKu1jAW_52pg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@ruchindra?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Ruchindra Gunasekara</a> on <a href="https://unsplash.com/s/photos/storage?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>.</figcaption></figure><p>I used to develop an app for viewing huge tables of data (250K rows x 120 columns). The main requirement was that all the data was available in the client’s app. The data was immutable — it was saved once and then used in table views in the client app.</p><p>As you know, MongoDB stores data in a document. The limit for one document is 16Mb. You can also use GridFS to store large files that can exceed 16Mb. It will store them in multiple chunks.</p><p>I decided to store each column as a single document in MongoDB where _id was a hash of that column data. In this article, I will share an approach I used to store documents in MongoDB columns larger than 16Mb.</p><p>There are four types of data in our app — strings, numbers, dates (represented as Unix ms), and booleans. With its 16Mb limit, a MongoDB document can easily store around 2 million values of 64-bit numbers (also dates and booleans). But strings are a special case. Each UTF-8 character takes one byte.</p><p>The interface for the column with strings is below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e17bfaee43ed1771513b14470bf2f498/href">https://medium.com/media/e17bfaee43ed1771513b14470bf2f498/href</a></iframe><p>At some point, our users started to upload tables with 250K values with columns of 40-character hashes. Simple math shows that 250,000 x 40 characters x 2 bytes = 20Mb. At that point, our app was unable to save such columns to MongoDB.</p><p>I had to find a solution.</p><h3>Ways to Decrease the Size of the Column With Strings</h3><h4>1. Stringify and zip string of values</h4><p>The most naive approach still gets the job done for some repeated texts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f2b81dd50a7cce04b90a4d888f5609c/href">https://medium.com/media/1f2b81dd50a7cce04b90a4d888f5609c/href</a></iframe><h4>2. Use the Excel approach with a dictionary</h4><p>Create a dictionary, put unique strings into a dictionary, and replace the strings with indexes. This method is useful if there are many repeated strings in a column. It does not help if someone wants to save a column of hashes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/984acf126f0a68068e2981d49b2fcbf8/href">https://medium.com/media/984acf126f0a68068e2981d49b2fcbf8/href</a></iframe><p>Unfortunately, if someone wants to save a column of hashes, the two previous methods will not help. So, this is the third approach: Do what GridFS does for files.</p><h4>3. Create multiple documents for a single column</h4><p>We always serve a full column to the user so that we create a linked list of documents. If our column fits 16Mb, we store it as is. Otherwise, we split the column into chunks and save the chunks in other documents linked to the main document.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44ca494b47a2abbcb831532f8e3fb143/href">https://medium.com/media/44ca494b47a2abbcb831532f8e3fb143/href</a></iframe><p>Our get function for the column will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47634a7d5823780af2e6216a5f01bdac/href">https://medium.com/media/47634a7d5823780af2e6216a5f01bdac/href</a></iframe><p>Now our users can save large amounts of data without crashing our app. We will store a column in multiple documents.</p><h3>Conclusion</h3><p>I used a naive approach to calculate the MongoDB document size in the code above. In the next article, I will write about how to better calculate document size.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aecc957bbe6c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-store-documents-larger-than-16-mb-in-mongodb-aecc957bbe6c">How to Store Documents Larger Than 16 MB in MongoDB</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Safe Recursion with Trampoline in JavaScript]]></title>
            <link>https://levelup.gitconnected.com/safe-recursion-with-trampoline-in-javascript-dbec2b903022?source=rss-9bef77b73736------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbec2b903022</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Valeriy Baranov]]></dc:creator>
            <pubDate>Mon, 11 May 2020 16:10:30 GMT</pubDate>
            <atom:updated>2020-05-11T16:10:30.517Z</atom:updated>
            <content:encoded><![CDATA[<p>Using recursion in JavaScript is not safe — consider trampoline. This converts recursion to a while loop to get around JavaScript’s limitations and prevent an overflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9Sxt2ppwVpNELxC0" /><figcaption>Photo by <a href="https://unsplash.com/@charlesc7?utm_source=medium&amp;utm_medium=referral">Charles Cheng</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>An idiomatic example of a recursive function is a factorial calculation. We call factorial function n times to get a result. Every call will add one more factorial function to the call stack.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37c3c99edf343f961c7c5dcdd13b4598/href">https://medium.com/media/37c3c99edf343f961c7c5dcdd13b4598/href</a></iframe><p>Recursion in JavaScript is mostly a sign of code smell and generally should be avoided. The reason is that JavaScript is a stack-based language, there is no tail call optimization yet. If you execute the following snippet, you will get a RangeError: Maximum call stack size exceeded.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da922bbffe26b6057602ad2c278eb0f8/href">https://medium.com/media/da922bbffe26b6057602ad2c278eb0f8/href</a></iframe><p>Still, there are situations when you want to use a recursive function. A realistic one would be to traverse a DOM tree or any other tree-like structure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87119e996336202ddc9a7868d1b064e8/href">https://medium.com/media/87119e996336202ddc9a7868d1b064e8/href</a></iframe><h4>Trampoline — a safe way to use recursion in JavaScript.</h4><p>With trampoline, your transform recursive function into a while loop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f31e2c8216afe90e6f2938355fad35e2/href">https://medium.com/media/f31e2c8216afe90e6f2938355fad35e2/href</a></iframe><p>Let&#39;s rewrite the factorial function to use trampoline. We need to:</p><p>1. return a value in a base case.</p><p>2. return a function to be called in other cases.</p><p>We also add accumulator argument for our internal implementation of _factorial.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1af54630adf2e47c5069864668c63c9/href">https://medium.com/media/c1af54630adf2e47c5069864668c63c9/href</a></iframe><p>Our recursiveFn can be rewritten similar to factorial, but we do not need an accumulator to track state. This implementation can be safely run even for millions of iterations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50f4f80487d7c506e140f6cdd51c135e/href">https://medium.com/media/50f4f80487d7c506e140f6cdd51c135e/href</a></iframe><p>Next time you want to use recursion, try trampoline!</p><p>You can try trampoline in CodePen below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fbaranovxyz%2Fembed%2Fpreview%2FzYvjKGN%3Fheight%3D600%26slug-hash%3DzYvjKGN%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fbaranovxyz%2Fpen%2FzYvjKGN%3Feditors%3D0012&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1198465.zYvjKGN.small.9208008b-ddf1-4fb1-9b96-3fafe1f9966f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a71b010e1669712ef7f77b0dcdafc77d/href">https://medium.com/media/a71b010e1669712ef7f77b0dcdafc77d/href</a></iframe><p>Trampoline is not a popular technique. Even in ramda and lodash, there are no trampoline helpers.</p><p>Thanks to Kyle Simpson for the inspiration about trampolines.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbec2b903022" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/safe-recursion-with-trampoline-in-javascript-dbec2b903022">Safe Recursion with Trampoline in JavaScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Create Simple Modal in React]]></title>
            <link>https://medium.com/swlh/how-to-create-simple-modal-in-react-991e1612559?source=rss-9bef77b73736------2</link>
            <guid isPermaLink="false">https://medium.com/p/991e1612559</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Valeriy Baranov]]></dc:creator>
            <pubDate>Thu, 07 May 2020 19:40:44 GMT</pubDate>
            <atom:updated>2020-05-09T00:38:27.695Z</atom:updated>
            <content:encoded><![CDATA[<p>Creating modal window in React is easier than you might think.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ptpYCJfSOSELz5rg" /><figcaption>Photo by <a href="https://unsplash.com/@coriel?utm_source=medium&amp;utm_medium=referral">Caitlin Oriel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>You probably use some popular library to create a modal window in your React app. From this article you will learn how to create a simple modal in React yourself.</p><p>Our modal will be able to:</p><ul><li>Show its content in the middle of the screen.</li><li>Close if you click it or press Esc.</li></ul><p>We create an overlay for our modal to show it fullscreen, and then add styles to centre it’s content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29aad4e620c8b85fc2469687757c7f2f/href">https://medium.com/media/29aad4e620c8b85fc2469687757c7f2f/href</a></iframe><p>We can make code for our Modal very simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aafa532b8fead503d7911ccb0ef09597/href">https://medium.com/media/aafa532b8fead503d7911ccb0ef09597/href</a></iframe><p>Then we add it to our App and add event listeners for Esc key:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b4c9da857a4a574cb2ab96ea1d265f0/href">https://medium.com/media/8b4c9da857a4a574cb2ab96ea1d265f0/href</a></iframe><p>You can try this simple modal in the codepen below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fbaranovxyz%2Fembed%2Fpreview%2FpojLjjB%3Fheight%3D600%26slug-hash%3DpojLjjB%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fbaranovxyz%2Fpen%2FpojLjjB&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1198465.pojLjjB.small.ee8a4093-b3e7-4b7e-b703-2a0ff2308979.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e54dea7c8103789c820617b49bd2b57/href">https://medium.com/media/3e54dea7c8103789c820617b49bd2b57/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=991e1612559" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-create-simple-modal-in-react-991e1612559">How to Create Simple Modal in React</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>