<?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 Hakobyan Karapet on Medium]]></title>
        <description><![CDATA[Stories by Hakobyan Karapet on Medium]]></description>
        <link>https://medium.com/@hakobyan.karapet91?source=rss-b6a5c858d2d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1-psgK3yIWa58S6Y</url>
            <title>Stories by Hakobyan Karapet on Medium</title>
            <link>https://medium.com/@hakobyan.karapet91?source=rss-b6a5c858d2d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 04:26:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hakobyan.karapet91/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[Big O notation with JavaScript examples]]></title>
            <link>https://medium.com/@hakobyan.karapet91/big-o-notation-with-javascript-examples-b5521209598b?source=rss-b6a5c858d2d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5521209598b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[big-o-notation]]></category>
            <category><![CDATA[time-complexity]]></category>
            <dc:creator><![CDATA[Hakobyan Karapet]]></dc:creator>
            <pubDate>Mon, 11 Mar 2024 22:38:34 GMT</pubDate>
            <atom:updated>2024-03-12T10:28:22.093Z</atom:updated>
            <content:encoded><![CDATA[<p>Writing scripts with some programming language that’s a very good skill</p><p><strong>BUT..</strong></p><p>When you need to create something special which is requires a lot of functionality, performance and complexity then you need to know about big o notation.</p><p><strong>O </strong>stands for <strong><em>operation</em></strong></p><p><em>Here is the chart of operations size</em></p><figure><img alt="Image alt https://www.bigocheatsheet.com/" src="https://cdn-images-1.medium.com/max/858/1*n221O8zKReuJAUiprJFp2w.png" /><figcaption>Chart image source: <a href="https://www.bigocheatsheet.com/">https://www.bigocheatsheet.com/</a></figcaption></figure><p>Now let’s see examples of complexity below 👇</p><h3><em>Constant runtime</em> <strong>O(1)</strong></h3><pre>const dataObject = {<br> key: &quot;value&quot;,<br> name: &quot;John&quot;,<br> lastName: &quot;Smith&quot;<br>};<br><br>// We always getting current item with one operation<br>dataObject[&quot;key&quot;];</pre><p>This means that no matter what data object changes or becomes larger, our operation will remain the same.</p><p>Another example of <strong>O(1) </strong>with array</p><pre>const dataArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];<br><br>// Here we always getting current index<br>dataArray[4]; // 5</pre><h3><em>Logarithmic time complexity</em> <strong>O(log N)</strong></h3><pre>function binarySearch(arr, val) {<br>  let start = 0;<br>  let end = arr.length - 1;<br><br>  while (start &lt;= end) {<br>    let mid = Math.floor((start + end) / 2);<br><br>    if (arr[mid] === val) {<br>      return mid;<br>    }<br><br>    if (val &lt; arr[mid]) {<br>      end = mid - 1;<br>    } else {<br>      start = mid + 1;<br>    }<br><br>    console.log(start, end);<br>    console.log(&#39;Operations count&#39;);<br>  }<br>  return -1;<br>}<br><br>const data = Array.from(Array(4_000).keys());<br>console.log(binarySearch(data, 498));</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*6KlDqMpu4HThHj7bz8EhEQ.gif" /><figcaption>Image source: <a href="https://upload.wikimedia.org/wikipedia/commons/c/c1/Binary-search-work.gif">https://upload.wikimedia.org/wikipedia/commons/c/c1/Binary-search-work.gif</a></figcaption></figure><p>For the logarithmic runtime, we have a good example of binary search, which essentially splits a <strong>sorted</strong> array into two, then checks whether our search number is equal to more or less than the number found, and repeats this until we get the exact one. In this example, we sorted the index array from <strong>0</strong> to <strong>3999</strong> and got the exact index after <strong>11 operations</strong>.</p><h3><em>Linear time complexity</em> <strong>O(N)</strong></h3><pre>const data = Array.from(Array(100).keys());<br><br>data.forEach((index) =&gt; {<br>  if (index === 50) { // but 50 can be any number this is just for example<br>    // do something<br>  } else {<br>    // do something else<br>  }<br>});</pre><p>Here we have an index array of size 100, so the execution time should always be equal to the length of the array (100) no matter what we do.</p><h3>Log linear time complexity O(N log N)</h3><pre>function merge(left, right) {<br>  let sortedArr = [] // the sorted items will go here<br>  while (left.length &amp;&amp; right.length) {<br>    // Insert the smallest item into sortedArr<br>    if (left[0] &lt; right[0]) {<br>      sortedArr.push(left.shift());<br>    } else {<br>      sortedArr.push(right.shift());<br>    }<br>  }<br>  // Use spread operators to create a new array, combining the three arrays<br>  return [...sortedArr, ...left, ...right];<br>}<br><br>function mergeSort(arr) {<br>  // Base case<br>  if (arr.length &lt;= 1) {<br>   return arr;<br>  }<br>  const mid = Math.floor(arr.length / 2)<br>  // Recursive calls<br>  let left = mergeSort(arr.slice(0, mid));<br>  let right = mergeSort(arr.slice(mid));<br><br>  return merge(left, right)<br>}<br><br>console.log(<br> mergeSort([12, 34, 11, 1, 54, 25, 67, 45])<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*fc28I7HCfzFbECoJoLqR1A.gif" /><figcaption>Source of image: <a href="https://en.wikipedia.org/wiki/Merge_sort">https://en.wikipedia.org/wiki/Merge_sort</a></figcaption></figure><p>Let’s look at an example of merge sort, which divides an unsorted array into fragments until only 1 element remains, then compares them and merges them.</p><h3>Quadratic time complexity O(n²)</h3><pre>function bubbleSort(arr) {<br>  for ( let i = 0, l = arr.length; i &lt; l; i++ ) {<br>    for ( let j = 0; j &lt; l; j++ ) {<br>      const temp = arr[i];<br><br>      if (arr[i] &lt; arr[j]) {<br>        arr[i] = arr[j];<br>        arr[j] = temp;<br>      }<br>    }<br>  }<br><br>  return arr;<br>}<br><br>console.log(<br> bubbleSort([12, 34, 11, 1, 54, 25, 67, 45])<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*1MiLjMYgr2r2fDORCJn89w.gif" /><figcaption>Image source: <a href="https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif">https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif</a></figcaption></figure><p>Let’s imagine that we have an array of numbers and our task is to sort them in ascending order, so one way to do this is to use the bubble sort algorithm. The basic idea is to compare the current number with all the others and swap them if necessary.</p><h3>Cubic time complexity O(n³)</h3><pre>function CubicFunction(arr) {<br>  for ( let i = 0, l = arr.length; i &lt; l; i++ ) {<br>    for ( let j = 0; j &lt; l; j++ ) {<br>      for ( let m = 0; m &lt; l; m++ ) {<br>        console.log( `i:${i}, j:${j}, m:${m}` );<br>      }<br>    }<br>  }<br><br>  return arr;<br>}<br><br>CubicFunction([12, 34, 11, 1, 54, 25, 67, 45]);</pre><p>This runtime example is similar to bubble sort with the difference that it has three levels of iteration depth.</p><h3>Exponential time complexity O(2^n)</h3><pre>const recursiveFibonacci = (n) =&gt; {<br>  if (n &lt; 2) {<br>    return n;<br>  }<br><br>  const result = recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2);<br><br>  return result;<br>};<br><br>console.log(`Result: ${recursiveFibonacci(8)}`); // 21</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*DlyD-7nh6tINGUHwDJx6Ug.png" /><figcaption>Image source: <a href="https://en.wikipedia.org/wiki/Fibonacci_sequence">https://en.wikipedia.org/wiki/Fibonacci_sequence</a></figcaption></figure><p>Let’s see the example of fibonacci recursive function, where the <strong>Fibonacci sequence</strong> is a sequence in which each number is the sum of the two preceding ones.</p><h3>Factorial time complexity O(n!)</h3><pre>function factorial(n){<br>  <br>  if (n === 0) {<br>    return 1;<br>  }<br><br>  return n * factorial(n - 1);<br>}<br><br>console.log( factorial(2) ); // 2 operations<br>console.log( factorial(3) ); // 6 operations<br>console.log( factorial(4) ); // 24 operations<br>console.log( factorial(5) ); // 120 operations<br><br>/* Example of factorial 2<br>12 21<br><br>/* Example of factorial 3<br>132 123<br>231 213<br>321 312<br><br>/* Example of factorial 4<br>1234 2431 3412 4123<br>1243 2413 3421 4132<br>1423 2341 3214 4213<br>1432 2314 3241 4231<br>1342 2143 3142 4321<br>1324 2134 3124 4312<br><br>etc...<br>*/</pre><p>This is the worst case scenario of all the previous ones, so what we do here is we simply count all the available scenarios for the current combinations.</p><blockquote><strong>Conclusion</strong></blockquote><p><strong>Congratulations, you now know all the popular complexity runtimes. I wish you to find interest in what you creating, have fun, and most importantly, enjoy the process!</strong> 😊</p><p><strong>Codepan link: </strong><a href="https://codepen.io/pen?template=LYvNKWK">https://codepen.io/pen?template=LYvNKWK</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5521209598b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimized virtual scroll]]></title>
            <link>https://medium.com/@hakobyan.karapet91/optimized-virtual-scroll-cb1c8cf82e19?source=rss-b6a5c858d2d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb1c8cf82e19</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Hakobyan Karapet]]></dc:creator>
            <pubDate>Sat, 17 Feb 2024 20:01:35 GMT</pubDate>
            <atom:updated>2024-02-17T21:11:08.670Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome !!</p><p>Today I’ll show you how to create optimized virtual scroll using pure HTML+CSS+JS.</p><p>In the modern www (World Wide Web), many problems arise related to DOM optimization, and we will solve one of them here today.</p><p>Let’s imagine that we have very large data coming from the server and we need to display it in a scroll &lt;div&gt;.</p><p>But we don’t want/need to show all of them cause it can affect on performance.</p><p>What to do?</p><p>Let’s create <strong>index.html</strong> file with the following content 👇</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>    &lt;title&gt;Virtual scroll&lt;/title&gt;<br>    &lt;style&gt;<br>       * { box-sizing: border-box; margin: 0; padding: 0; }<br>       body { background-color: #838383; font-family: Helvetica, Arial, sans-serif; }<br>       .scroll { margin: 0 auto; width: 600px; }<br>       .scroll__wrapper { background-color: #FFFFFF; border: 2px solid #999999; max-height: 400px; overflow: auto; }<br>       .scroll__wrapper::-webkit-scrollbar { background-color: #EEEEEE; height: 10px; width: 10px; }<br>       .scroll__wrapper::-webkit-scrollbar-thumb { background: #000000; border-radius: 8px; }<br>       .scroll__item-number { color: #FFFFFF; font-size: 40px; height: 60px; line-height: 60px; margin: 20px 0; text-align: center; }<br>       .scroll__content { border-bottom: 1px solid #999999; margin-bottom: 10px; padding: 10px; }<br>       .scroll__title { font-size: 16px; margin-bottom: 10px; }<br>       .scroll__text { font-size: 14px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }<br>       .scroll__start-padding,<br>       .scroll__end-padding { background: repeating-linear-gradient(#EEEEEE, #EEEEEE 10px, #FFFFFF 20px, #FFFFFF 25px); }<br>   &lt;/style&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>   &lt;div class=&quot;scroll&quot;&gt;<br>     &lt;div class=&quot;scroll__item-number&quot;&gt;0&lt;/div&gt;<br>     &lt;div class=&quot;scroll__wrapper&quot;&gt;<br>       &lt;div class=&quot;scroll__start-padding&quot;&gt;&lt;/div&gt;<br>       &lt;div class=&quot;scroll__list&quot;&gt;&lt;/div&gt;<br>       &lt;div class=&quot;scroll__end-padding&quot;&gt;&lt;/div&gt;<br>     &lt;/div&gt;<br>   &lt;/div&gt;<br>   &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>Let’s not dive into css is just a basic view</p><p>Now create <strong>script.js </strong>file with the following code 👇</p><pre>const initScroll = () =&gt; {<br><br> // Functions<br> const generateData = (arraySize) =&gt; {<br><br>  const data = new Array(arraySize).fill({<br>   name: &quot;John&quot;,<br>   lastName: &quot;Smith&quot;,<br>   text: &quot;Lorem ipsum dolor, sit amet consectetur adipisicing elit. Adipisci eveniet nam laboriosam, quas assumenda tempora fuga ut dignissimos quia consequuntur magnam sit aliquid ex cum quo similique nemo molestiae fugit aliquam repellendus voluptatibus? Delectus itaque sequi cupiditate laboriosam perspiciatis ab.&quot;<br>  });<br><br>  return data;<br> }<br><br> const getHtmlContent = ({ name, lastName, text, index }) =&gt; {<br><br>  const htmlTemplate = (<br>   `&lt;div class=&quot;scroll__content&quot; data-index=&quot;${index}&quot;&gt;<br>    &lt;h2 class=&quot;scroll__title&quot;&gt;${name + &quot; &quot; + lastName + &quot; - &quot; + index}&lt;/h2&gt;<br>    &lt;p class=&quot;scroll__text&quot;&gt;${text}&lt;/p&gt;<br>   &lt;/div&gt;`<br>  );<br><br>  return htmlTemplate;<br> };<br><br> const getScrollItemHeight = () =&gt; {<br><br>  const scrollContent = scroll.querySelector(&quot;.scroll__content&quot;);<br><br>  return scrollContent.offsetHeight + parseInt(getComputedStyle(scrollContent).marginBottom);<br> };<br><br> const renderItems = () =&gt; {<br><br>  let content = &quot;&quot;,<br>   startIndex = start;<br><br>  while ( startIndex &lt; end ) {<br><br>   content += getHtmlContent({...filledArr[startIndex], index: startIndex});<br><br>   startIndex++;<br><br>  }<br><br>  scroll.querySelector(&quot;.scroll__list&quot;).innerHTML = content;<br><br> };<br><br> const setPaddings = () =&gt; {<br><br>  const scrollItemHeight = getScrollItemHeight();<br><br>  scrollStartPadding.style.height = (start * scrollItemHeight) + &quot;px&quot;;<br>  scrollEndPadding.style.height = ((scrollItemsCount - end) * scrollItemHeight) + &quot;px&quot;;<br> };<br><br> const debounce = () =&gt; {<br><br>  let timer = null;<br><br>  return (cb, time = 250) =&gt; {<br>   if (timer !== null) {<br>    clearTimeout(timer);<br>   }<br>   timer = setTimeout(cb, time);<br>  };<br>  <br> };<br><br> const shouldReRender = () =&gt; {<br>  const startItem = scroll.querySelector(`.scroll__content[data-index=&quot;${start}&quot;]`);<br>  const endItem = scroll.querySelector(`.scroll__content[data-index=&quot;${end-1}&quot;]`);<br><br>  return !startItem || !endItem;<br> };<br><br> const onScrollHandler = (e) =&gt; {<br><br>  const scrollTop = e.target.scrollTop;<br>  const getScrolledCurrentIndex = Math.floor(scrollTop / getScrollItemHeight()) || 0;<br><br>  // Showing in the browser current item index<br>  scroll.querySelector(&quot;.scroll__item-number&quot;).innerHTML = getScrolledCurrentIndex;<br><br>  scrollEnd(() =&gt; {<br><br>   // Update start, end items<br>   start = getScrolledCurrentIndex;<br>   end = getScrolledCurrentIndex + limit;<br>   <br>   // Decreasing displayed items by average of limit in order to have current item in the middle of renderer items<br>   start -= rendererItemsAverage;<br>   end -= rendererItemsAverage;<br><br>   // Check not to display unavailable items<br>   if (start &lt;= 0) {<br>    start = 0;<br>    end = limit;<br>   } else if (end &gt;= scrollItemsCount) {<br>    start = scrollItemsCount - limit;<br>    end = scrollItemsCount;<br>   }<br><br>   // Check should re-render<br>   if (shouldReRender()) {<br>    reRender();<br>    e.target.scrollTop = scrollTop;<br>   }<br><br>  });<br>  <br> };<br><br> const reRender = () =&gt; {<br>  renderItems();<br>  setPaddings();<br> };<br><br> // Constants<br> const limit = 10;<br> const scrollItemsCount = 100_000;<br> const filledArr = generateData(scrollItemsCount);<br> const scroll = document.querySelector(&quot;.scroll&quot;);<br> const scrollWrapper = scroll.querySelector(&quot;.scroll__wrapper&quot;);<br> const scrollStartPadding = scroll.querySelector(&quot;.scroll__start-padding&quot;);<br> const scrollEndPadding = scroll.querySelector(&quot;.scroll__end-padding&quot;);<br> const rendererItemsAverage = Math.floor(limit / 2);<br> const scrollEnd = debounce();<br><br> // Variables<br> let start = 0,<br>     end = limit;<br><br> // Init functional<br> reRender();<br> scrollWrapper.onscroll = onScrollHandler;<br><br>};<br><br>// Document ready event<br>document.addEventListener(&quot;DOMContentLoaded&quot;, initScroll);</pre><p>Now refresh your browser.</p><figure><img alt="Virtual scroll" src="https://cdn-images-1.medium.com/max/1000/1*0cMuVvVZgjzmqVNmSBl2dg.gif" /></figure><p>🎉🎉 Huuurah Congrats you have it.</p><p>As we can see we have 100.000 array of objects but showing only limited 10 items.</p><p>To change the limit just edit <strong><em>limit</em></strong> constant on your own.</p><p>Example 👇</p><pre>const limit = 15;</pre><p>Now you will have 15 items per scroll.</p><figure><img alt="Virtual scroll" src="https://cdn-images-1.medium.com/max/1024/1*guDp0nxD2WbouhkSFEV0Hg.png" /></figure><p><strong>Codepan link:</strong> <a href="https://codepen.io/Karapet-Hakobyan/pen/Rwddbrw">https://codepen.io/Karapet-Hakobyan/pen/Rwddbrw</a></p><p><strong>Github link: </strong><a href="https://github.com/karo73/virtual-scroll">https://github.com/karo73/virtual-scroll</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb1c8cf82e19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>