<?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 Sergey Dubovyk on Medium]]></title>
        <description><![CDATA[Stories by Sergey Dubovyk on Medium]]></description>
        <link>https://medium.com/@knidarkness?source=rss-11c7292a812b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*or6QN2E-dP39qWeE3mgJqw.jpeg</url>
            <title>Stories by Sergey Dubovyk on Medium</title>
            <link>https://medium.com/@knidarkness?source=rss-11c7292a812b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:31:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@knidarkness/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[Can we use local LLM to enhance traditional chatbot output on a budget?]]></title>
            <link>https://medium.com/@knidarkness/can-we-use-local-llm-to-enhance-traditional-chatbot-output-on-a-budget-beba6cfc48d6?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/beba6cfc48d6</guid>
            <category><![CDATA[chat-bot-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Sat, 09 Mar 2024 23:33:37 GMT</pubDate>
            <atom:updated>2024-03-09T23:33:37.270Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or a journey on using HuggingFace to quickly prototype free and open large-language models to enhance deterministic applications</h4><h3>Introduction</h3><p>We all know that large LLMs can be great for many applications, but they also bring unique risks of being very “confident liars”. Additionally, it takes an incredible amount of computing power to operate and the costs do add up. On the other end of the spectrum, there is a whole world of open-source LLMs that can be run locally (or on-prem) often on a fraction of resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cqPGdgjjJS1bDn1j3lwydQ.jpeg" /><figcaption>I had to use AI to create an image for an article about an AI, had I not?</figcaption></figure><p>Even though these smaller LLMs often lack the performance and capabilities of their well-known counterparts like ChatGPT, Gemini, and others, I wanted to test how we can use them locally to enhance user experience with a more “alive” presentation of some factual information.</p><p>As the simplest example, I am going to use the weather forecast because it is the most straightforward case of a “question → factual answer” scenario.</p><p>The goal for today’s exercise is to create a Python wrapper around one of the open-source LLMs. This package will take weather data as its input, do some “magic”, and return a text message with the weather information and some “personalized” flavor.</p><h3>Baseline</h3><p>As a first baseline, I have created a super minimalistic setup that prints the weather provided. Nothing fancy, just the “parameter = value” type of output. For most cases, it’s good enough and can be used either directly or with some processing as an output for our bot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0703dfe8dd9073b56113042dcd68d28/href">https://medium.com/media/e0703dfe8dd9073b56113042dcd68d28/href</a></iframe><p>As one would expect, the output is nothing fancy but it does its job:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rd5dg1v4dbX9R8l4xoukCQ.png" /></figure><p>But we (maybe) can do better, can’t we?</p><h3>Choosing the model</h3><p>There are a lot of free and open models nowadays. As of recently the <a href="https://huggingface.co/teknium/OpenHermes-2.5-Mistral-7B#quantized-models">OpenHermes Mistral 7B</a> was quite decent in my experiments, so I have decided to start with it.</p><h3>First attempts</h3><p>I am using the public Inference API by HuggingFace. Mainly, because it is simple and does the trick for this one-evening endeavor. Of course, for the “production” usage one would want to run the model either on-prem or in a private cloud, but for the initial testing, it seems to be good enough.</p><p>For now, we extend our “message generating” class with an API call to HuggingFace like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41d9498519741917d84c9cca498eb272/href">https://medium.com/media/41d9498519741917d84c9cca498eb272/href</a></iframe><p>In this iteration, I am not too concerned about tinkering with the prompt and only want to get a reasonable enough message output. That we have achieved. For weather conditions of 45 degrees Celsius, 50% humidity, no chance of rain, and 5 kph wind, we get the following outputs from the Mistral 0.2 model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iiynYzU_uNIKQ4myFCklQQ.png" /></figure><p>Not perfect, but I’d say a good enough first result without any major tuning, or even time investment.</p><h3>Can we prevent it from lying?</h3><p>One thing that modern language models are infamous for is either outright lying or complying with prompts just partially. When toying around with the prompts I had several occasions when the model would not mention all of the parameters for seemingly random reasons.</p><p>The simplest option is to always verify that the model’s output contains all the “numbers” (i.e. temperature, wind, and so on) from our weather object. Then we will simply regenerate the output until the model includes all the data points we need. This approach seems quite similar to unit testing and I think it’s quite useful, especially in cases when we have a set of “hard facts” and need to only generate additional “flavoring” around them.</p><p>For this exercise I have implemented a very dumb approach which nonetheless is sufficient to sieve out the most obvious failed attempts:</p><pre>def __validate_weather_message(self, weather: WeatherInfo, message: str):<br>    return ((str(weather.temperature) in message)<br>           and (str(weather.wind_speed) in message)<br>           and (str(weather.wind_speed) in message))<br><br>def __prepare_weather_message(self, weather: WeatherInfo, location: str, retries: int = 5):<br>    message = self.__get_llm_output(weather, location)<br>    if not self.__validate_weather_message(weather, message) and retries &gt; 0:<br>        print(&#39;Will retry weather generation&#39;)<br>        return self.__prepare_weather_message(weather, location, retries - 1)<br>    return message<br><br>def get_user_message(self, weather: WeatherInfo) -&gt; str:<br>    return self.__prepare_weather_message(weather, &quot;Austin, Texas&quot;)p</pre><p>Crude and overly simplistic, but hopefully will be sufficient to get us going. Indeed, we are getting quite good outputs here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RRDrJKIxkMX6cKbmr7J7hw.png" /></figure><h3>Wrapping it all up</h3><p>So, after a couple of hours playing with free text generation models, I would say that even without paying the steep price for OpenAI or competitors’ API access one can get quite some benefits of modern LLMs. What we’ve created today is not the most sophisticated or powerful solution, but even at this point adds enough value to at least consider using it.</p><p>All in all, it looks like we can confidently use smaller and/or free and open large language models to enhance our otherwise deterministic applications. On top of that, it is now possible and feasible to use CPU-only environments for these “occasional” computations. More likely though, you’d still need a dedicated node/cluster to run the “LLM-ed” part of the application.</p><p>With that being said, get out there and have fun!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=beba6cfc48d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lessons learned from a failure: how not to start an outsource company:]]></title>
            <link>https://medium.com/@knidarkness/lessons-learned-from-a-failure-how-not-to-start-an-outsource-company-7dba02301abc?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dba02301abc</guid>
            <category><![CDATA[enterpreneurship]]></category>
            <category><![CDATA[outsourcing]]></category>
            <category><![CDATA[hr]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Sat, 09 Mar 2024 18:56:18 GMT</pubDate>
            <atom:updated>2024-03-09T23:51:51.464Z</atom:updated>
            <content:encoded><![CDATA[<h4>A story of ambitions or why you might not want to run a company in your early 20s</h4><blockquote>I was sifting through my drafts and this one has been sitting unfinished for almost 4 years now. The story I am telling here is a bit hard to swallow, but also really important for me &amp; my experience. So, after a bit of deliberation I have decided to finally finish it and put to public eye.</blockquote><p>I will tell you a story of another small tech company which failed. I was 20 and tired of “all that stupid management” in my current team. I got bored with meetings, bureaucracy, and boring projects. But most of all I was tired of working in the outsource without any real impact on what I was creating. You know, that feeling, you sometimes have when you do believe that you can do something useful?</p><p>The solution I found was pretty simple: to start a business by myself and “do it right” (and of course, it had to be another outsource agency, because I was 21 and didn’t know any better. Go figure).</p><p>Now it’s been a year (no, <strong>now</strong> it’s been much longer, but I kept it as is to remind myself how long-ish ago that all happened) after it failed and I am going to share some insights on decisions which lead to such end.</p><p>First of all, a piece of background information about my “start-up”. We started as two passionate guys who wanted to build software with as little control and management as possible. The end was much more realistic. We were barely keeping up with requirements from the few customers we had, our quality (both of life and deliveries) was slowly but steadily declining, I was trying to bite off a log bigger chunk than I possibly could swallow, and ultimately and most importantly — I was so enamored with the idea of running a company that I’d forgot that I wanted to do something not related to out-source.</p><h3>Lessons learned and what not to repeat</h3><p>Now with the introduction done, I want to go over a few of the outtakes in a bit more detail. None of them are particularly ground-breaking news, but for me, they were “new things” I had discovered almost five years ago.</p><h4>Do not run a business if you want to do engineering</h4><p>One of my major misunderstandings was that I believed I would be able to work more as an architect, not as an entrepreneur.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*bbdTew5vahzB-CwnrXgnrA.jpeg" /></figure><p>That is just a wrong opinion. Even if you are running a start-up of 3 people, your main task will be to make connections, find customers, investors, and employees. If you are going to add engineering tasks to that pack… well, one day you will wake up so overloaded you won’t be able to do anything. It is better to admit that once you step on the entrepreneur&#39;s path you are no longer an engineer. That’s fine.</p><h4>If you run a business do not work as an engineer</h4><p>This one is quite similar to the previous point. The idea is that even if you are not intending to keep doing engineering tasks… life happens. You might be tempted to jump into a difficult task and implement something your team got stuck with. Believe me, it’s better to avoid it. You can and actually should share your knowledge. Give ideas on how to solve some problems. But remember, each hour you spend writing code is an hour you are not spending doing tasks bringing business value. For me, that was one of the hardest parts. How can I not help with this feature if it will only take 3–4 hours? Just stop and re-read the previous paragraph.</p><h4>Firing is hard. You will need to do it.</h4><p>Probably one of the least pleasant moments of the whole experience was that one day when I had to let go of one of my friends. We’d hired him as a Junior Developer working on one of our projects. He was a great guy. But he was a weak engineer. And I’ve failed him. Ultimately, when you join a company as a Junior first and foremost you are expected to learn and to grow. We didn’t have neither resources to spare, nor the experience to provide such guidance. It was an environment of “let’s get going and learn along the way”. Quite predictably it didn’t work out well. After complaints from our customer and a painful code review session, it was clear — the guy at the moment did not have the skills we needed. What followed was a very awkward conversation between two similarly-aged young adults. I have put myself in a place, where I have gained someone’s trust, brought them into the team, and then ultimately could not help them grow.</p><p>Lesson: when hiring, always remember that it’s not just you who trusts a new person with some responsibility, but also your new employee who trusts you with an important part of their career and life.</p><h4>Be ready to be responsible for what you do not control</h4><p>One of the biggest changes for me at the time was the idea of being responsible for something I do not have a direct influence over.</p><p>How could I commit to something for our customers when I have zero clue if “Bob” who will work on the project will have any roadblocks or not?</p><p>It took a better part of 5 years for me to learn how to trust those who work with me on one hand, and on the other hand, to have enough experience to “wing” the complexity of a problem. And of course, always multiply estimates by a factor of 2. Minimum!</p><h3>Closing thoughts</h3><p>With all that being said I am still incredibly grateful to all those with whom I embarked on this journey. For me, the most important lessons were not only those I have mentioned before but also the ability to talk with customers and those whose problems we ultimately try to solve.</p><p>I strongly believe that a good software engineer knows, understands, and cares about the underlying pain that the software they are building can eliminate. And for me it was an incredible opportunity to see the “full circle” of the software lifecycle.</p><p>Would I advise everyone to band together with their colleagues, college friends, and a few strangers to create a small company? Hells no! But definitely try to get an experience with the end-to-end customer journey in our industry which can be isolated from living and breathing users by quite a long chain of “processes”.</p><p>I am going to conclude this small retrospective with a quote that I find quite inspirational to always keep going:</p><blockquote>“Success is not final; failure is not fatal: It is the courage to continue that counts.” — Winston S. Churchill</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dba02301abc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[It’s 2023 and WebAssembly is Still Not Ready. Or is It?]]></title>
            <link>https://medium.com/better-programming/its-2023-and-webassembly-is-still-not-ready-or-is-it-993699685811?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/993699685811</guid>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 11:33:37 GMT</pubDate>
            <atom:updated>2023-01-24T07:47:45.466Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>A tale about how it was never about performance in the first place.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E1SUs4EWSXG7Qwtp" /><figcaption>Photo by <a href="https://unsplash.com/de/@mokngr?utm_source=medium&amp;utm_medium=referral">Moritz Knöringer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A couple of weeks back I was discussing the concept and “mental model” of WebAssembly with a colleague and friend of mine at <a href="http://anyline.com/">Anyline GmbH,</a> where we create a lot of impressive stuff in the domain of mobile scanning and machine learning. (Or, and by the way, we are <a href="https://anyline.com/jobs">hiring</a>).</p><p>This conversation threw me to 2018, even before the pandemic, when I presented a talk at a local JS meetup about WebAssembly and how it can help us with creating new projects. At that time, I believed it to be a great tool, especially because it brought a lot of performance improvements. Somewhere in the 5–10x ballpark for a naive C/JS comparison.</p><p>During the last few years, however, a lot of things happened. Some of those things were good for web development: numerous improvements to the browsers’ engine performance, JS/TS getting into a “decent language” territory, and the ever-growing open-source community around the web stack.</p><p>A lot of people now ask:</p><blockquote>It’s almost 2023 and WebAssembly has been around for over 5 years after its initial release in 2017. And yet, is it ready to be used in production environments and deliver the performance of C into JavaScript world?</blockquote><p>Let’s see for our own.</p><p>For starters, let’s establish a benchmark. We want to measure the “performance in a vacuum”. This disqualifies a lot of UI interactions because the transition between WebAssembly and JavaScript adds some overhead which we, for now, want to avoid.</p><p>So, to give WebAssembly the best chances, let’s use Fourier transformations as it is a somewhat non-trivial, yet at the same time “pure” computational task. If you are unfamiliar with what it is, below is a fairly good explanation of the concept. But either way, for us it is a “somewhat computationally intensive task which is not a benign sorting”.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FspUNpyF58BY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DspUNpyF58BY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FspUNpyF58BY%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9997c0d0e1f921d8c3f2a0e2cca4fcb4/href">https://medium.com/media/9997c0d0e1f921d8c3f2a0e2cca4fcb4/href</a></iframe><h4>Test cases</h4><p>Now, let’s get to the “fun part” — coding.</p><p>First of all, we’ll need a baseline with pure JavaScript:</p><pre>export async function dft(x: number[]): [number[], number[]] {<br>    const Xr = [];<br>    const Xi = [];<br><br>    let i, k, n = 0;<br>    <br>    for(k = 0; k &lt; x.length; k++) {<br>        Xr[k] = 0;<br>        Xi[k] = 0;<br>        <br>        for (n = 0; n &lt; x.length; n++) {<br>            Xr[k] = (Xr[k] + x[n] * Math.cos(2 * Math.PI * k * n / x.length));<br>            Xi[k] = (Xi[k] - x[n] * Math.sin(2 * Math.PI * k * n / x.length));<br>        }<br>    }<br>    <br>    return [Xr, Xi];<br>}</pre><p>The code is not a focus of this article, but let’s go over it quickly as we’ll be using adaptations of it for other languages as well. Here we implement the Discrete Fourier Transform, which is defined by the formula below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*Vpq54F6hE99NK5c1RVUyww.png" /></figure><p>I specifically avoid using any language specific libraries which would provide implementation of complex numbers. This would add both complexity to the test code, as well as introduce performance variations based on each math library we’d use. Instead, I have used two separate arrays for real and imaginary parts of our X.</p><h4>Reproducibility</h4><p>In the following excerpts, I will only include minimal code samples to show the idea. As for the full repository with all the examined functions and benchmark methods, you can find it <a href="https://github.com/knidarkness/wasm-dft">here</a>.</p><h4>Performance</h4><p>Let’s get a performance baseline with our pure JavaScript approach:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dBRvol2QsQ3PYpBjWOqQLw.png" /></figure><p>Here we see that our naive implementation of the Fourier transform indeed shows the properties of an O(n^2) algorithm.</p><p>Now, let’s see how better “fast” languages will fare against “slow”. Let me introduce our contenders:</p><ul><li>Go, because what can be wrong with a “new C”</li><li>C/C++ itself, because it will never go away with the amount of legacy it has</li></ul><p>I thought about adding Rust and Python to this comparison, but essentially the results were almost identical, so we’ll stick to the smallest subset for now.</p><h4>Go</h4><p>Golang’s implementation of DFT is almost identical to the JS one. The only important difference being that we need to somehow copy the array from the JavaScript’s memory into the heap space accessible to the Go code.</p><p>We are doing this by firstly calling CopyBytesToGo() which will move the input from “JS-space” into the slice of type []byte . Then, in the js_buffer_to_float64_slice function we transform this byte slice into a bunch of floats, which are the actual input. The function is not important for us here, but you can take a look in the repo: .</p><pre>func DFT_naive(this js.Value, args []js.Value) interface{} {<br> input_array := make([]byte, args[0].Get(&quot;byteLength&quot;).Int())<br> js.CopyBytesToGo(input_array, args[0])<br><br> dft_points := js_buffer_to_float64_slice(input_array)<br><br> real := make([]float64, len(dft_points))<br> imag := make([]float64, len(dft_points))<br> <br>arg := -2.0 * math.Pi / float64(len(dft_points))<br><br> for k := 0; k &lt; len(dft_points); k++ {<br>  r, i := 0.0, 0.0<br>  for n := 0; n &lt; len(dft_points); n++ {<br>   r += dft_points[n] * math.Cos(arg*float64(n)*float64(k))<br>   i += dft_points[n] * math.Sin(arg*float64(n)*float64(k))<br>  }<br><br>  real[k], imag[k] = r, i<br> }<br> return 0<br>}</pre><h4>C</h4><p>As for the C implementation, we don’t need the machinery we used in Go to copy the data onto our functions heap written into it. All we should do is just accept a pointer to the first element of the array, and then iterate over it.</p><pre>int calculateDFT(int len, int *points)<br>{<br>    float Xr[len];<br>    float Xi[len];<br>    int i, k, n = 0;<br><br>    for (k = 0; k &lt; len; k++)<br>    {<br>        Xr[k] = 0;<br>        Xi[k] = 0;<br>        for (n = 0; n &lt; len; n++)<br>        {<br>            Xr[k] = (Xr[k] + points[n] * cos(2 * M_PI * k * n / len));<br>            Xi[k] = (Xi[k] - points[n] * sin(2 * M_PI * k * n / len));<br>        }<br><br>        printf(&quot;(%f) + j(%f)\n&quot;, Xr[k], Xi[k]);<br>    }<br>    return 0;<br>}</pre><p>Now, how do these two compare to JavaScript?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*InOwWXiurHbCefEqp5WSFA.png" /></figure><p>We can see some speed-up in the C implementation, but definitely not too dramatic. Moreover, for the Golang, the performance is actually way worse. That is probably because of the overhead added by the []byte-&gt;[]float64 conversion is done in quite a terrible way. But even for C code, the performance difference hovers around 50% between C and JavaScript:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*SFd7PG-6RCUBi5PYHHwGXQ.png" /></figure><p>Is getting your application twice as fast worth re-writing all the “heavy pieces” in the new language, introducing it into your tech stack, spending time and money on hiring, and so on?</p><p>Sometimes, the answer actually will be “yes”, but almost certainly, this is not the most widespread use case. Indeed, spending thousands and thousands of dollars and hours, during which you could be building the product itself is rarely argued for. And if you know that it is your case, chances are, you already have good reasons and statistics to back them up.</p><blockquote>This is exactly the argument a lot of people throw at somebody who dares to say: “WebAssembly is a nice piece of technology, we could benefit from using it”.</blockquote><p>What they miss though is a huge amount of already existing code in C/C++, Go, Rust, and even Python. The best code is the code that does not exist. And here the best way to “write” it — is to reuse tooling from another ecosystem. Especially, if you already have some parts of your system written in it.</p><p>And when you can not only save half a year of development time but also get a 30–40% performance boost — I believe it to be “the best of both worlds” scenario.</p><p>As for the example we had today, I challenge you to implement a Fast-Fourier Transform (or use an existing one, like <a href="https://github.com/dntj/jsfft">this</a>) to see that language doesn’t win performance races, algorithms on the other hand — do!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FE8HeD-MUrjY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DE8HeD-MUrjY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FE8HeD-MUrjY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bdf0fc24cd29996585958cc9fa35a9b/href">https://medium.com/media/2bdf0fc24cd29996585958cc9fa35a9b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=993699685811" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/its-2023-and-webassembly-is-still-not-ready-or-is-it-993699685811">It’s 2023 and WebAssembly is Still Not Ready. Or is It?</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[Choosing monorepo tooling: nx.dev vs Turborepo for a green field projects in 2022]]></title>
            <link>https://medium.com/@knidarkness/nx-dev-vs-turborepo-for-a-green-field-projects-in-2022-c73dd858b687?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c73dd858b687</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Thu, 04 Aug 2022 12:56:17 GMT</pubDate>
            <atom:updated>2022-08-04T12:58:05.543Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Analyzing what is a right tool for the job before committing to years of regrets</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xfT478jF1Psa2Y1_.png" /><figcaption>Git is so simple, yet it can be so messy if done wrong</figcaption></figure><h3>In a galaxy far, far away</h3><p>Nobody likes to skim through countless repositories and updating a single dependency. Copying typings, helper functions, and similar “stuff” is even more boring.</p><p>Now we are starting a new project at <a href="https://anyline.com/">Anyline GmbH</a>. Previously we had a lot of relatively small repositories. Some of them were for the micro-services which are still alive and well, others remained stagnant for over a year. After a tedious refactoring by my colleagues, we were able to reduce this number to just under 100 repositories. But the problem remained: we had a lot of code duplication between packages, complicated deployment of breaking changes, and fairly many places to look at for pieces of documentation applicable for several entities.</p><p>The decision we came up with was to move to a monorepo for a new large project and possibly migrate the existing code base to it over time. Today there are quite a lot of existing solutions for managing monorepos for all stacks imaginable, but we focused on:</p><ul><li>Pure yarn workspaces, for their minimalism</li><li>Nx.dev, as one of the most mature projects in the JS/TS ecosystem</li><li>Turborepo, as a relatively new (released late 2021) but promising solution backed by Vercel</li></ul><p>As for the requirements, we came up with this list:</p><ul><li>📚 Top-level dependency management for JavaScript / TypeScript</li><li>🔍 Uniform linting configuration</li><li>🏗 Incremental builds</li><li>📦 Caching of the build steps, ideally between development environments</li><li>⏳ Working out-of-the-box hot module reload for Next.js application for imported packages</li><li>😎 Overall “developer experience”</li></ul><p>Unfortunately, with the default workspaces, we would need to implement most of the items above ourselves, so the selection narrowed down to <a href="https://nx.dev/"><strong>nx.dev</strong></a> vs <a href="https://turborepo.org/"><strong>turborepo</strong></a>.</p><h3>So, here it goes…</h3><h4>Dependency management</h4><p>A lot of us had to deal with cases when you have 10 services, 5 of which are using a recent-ish version of some important package and the other 5 are a mix of everything between several minor patches old and a few years and three major releases outdated. This gets tedious fast. Thus we were looking for a tool that would take care of centralized dependency management for all the packages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z8xuBwSAbzk2ab9kczLdow.png" /><figcaption>What does nx.dev has to say</figcaption></figure><p>In the case of <strong>nx</strong>, it is a part of their design philosophy to pay the upfront cost of synchronizing the dependencies’ versions for the benefit of a more maintainable and reliable codebase.</p><p>Unfortunately, it is not the case with the <strong>Turborepo</strong>. By default, it organizes dependencies on the “per-package” level. This, in my opinion, is too granular for the codebase which we aim to keep cohesive.</p><p><strong>Nx 1— Turborepo 0</strong></p><h4>Uniform linting</h4><p>Nx allows configuring default linting solutions when initializing a monorepo and then customizing it for each package. Also, it uses the tags system to provide a more granular way to control linting rules when working with more complex dependency trees.</p><p>Turborepo also by default creates a package with an ESLint config from which other packages can inherit.</p><p><strong>Nx 2 — Turborepo 1</strong></p><h4>Caching &amp; Incremental Builds</h4><p>Nx uses two main types of caching:</p><ul><li>Local, which is a fairly self-explanatory concept: you run a build, artifacts are cached as a whole and partial build artifacts, and on the next build you have a chance to reuse your work.</li><li>Distributed, which builds upon the previous model, but also synchronizes the caches between compatible development environments which makes even the first “local” build as fast as the ones with existing caches.</li></ul><p>Unfortunately, distributed caching is a <strong>SaaS </strong>option provided by nx.dev.</p><p>As for the turborepo, it also supports local and remote caching. And again, as is the case with nx, remote caching is available as a part of the SaaS solution, this time powered by Vercel.</p><p>Overall both products are on par with each other regarding caching.</p><p>As for the incremental builds, both tools can detect the updated dependencies and optimize the build path. No clear winner here for me.</p><p><strong>Nx 3 — Turborepo 2</strong></p><h4>Hot reload</h4><p>Both nx and turborepo worked fine with Next.js hot module reload for me. Though there might be some <a href="https://github.com/vercel/turborepo/issues/839">workarounds</a> that one will need to implement with turborepo, the overall experience is fairly good with both tools.</p><h4>Developer experience</h4><p>This is mostly a subjective topic, so everything here one should take with a grain of salt. <strong>Nx </strong>comes with a fairly mature CLI tooling as well as a VS Code and JetBrains plugins. On <strong>Turborepo</strong>’s side, there are no IDE plugins as of summer 2022 and it seems to be the official developers’ position for the time being.</p><p>Also, the <strong>nx </strong>provides a much more developed ecosystem due to it being on the market for almost 5 years now. Probably, <strong>Turborepo</strong> will get to this level eventually, but now it is still in a fairly early stage of its life.</p><p>Finally, my experience was that nx builds take less time both due to incremental builds, and distributed (and local) caching. Also, I felt it was more straightforward and “native” to add packages in languages other than JavaScript or TypeScript as well.</p><p><strong>Nx 4 — Turborepo 2</strong></p><h3>Summary</h3><p><strong>I feel that nx provides better performance and developer experience.</strong> Turborepo is just a bit too young and its ecosystem is only beginning to mature. Probably, in a couple of years, it will be a strong competitor to other solutions, as the community already shows the new tool great love. But for now, I believe it’s better to stick to what already works and is “battle-proof”.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c73dd858b687" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The most exciting journey of 2020]]></title>
            <link>https://medium.com/@knidarkness/the-most-exciting-journey-of-2020-6262d6c6f03?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/6262d6c6f03</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 08:33:22 GMT</pubDate>
            <atom:updated>2020-08-17T08:33:22.731Z</atom:updated>
            <content:encoded><![CDATA[<h4>JavaScript developer’s journey for Rust. And a couple of tips from it</h4><p>Usually, I spend quite a bit of my time traveling or playing D&amp;D. All was good and sound up until the year 2020 kicked in, and… well, you know what happened next. Travel got banned, tabletop players self-quarantined and all of a sudden I got lots of free time. Even though it wasn’t voluntary, it was something to deal with. I remembered that I tried Rust during 2019 Hacktoberfest and quite liked it. “That’s it!” — I thought — “I can learn a new tool I like and what’s even more important to spend some time.”</p><blockquote>“That’s it!” — I thought, — “I can learn a new tool I like and what’s even more important spend some time”.</blockquote><h3>The Journey begins</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*X-IM0WcaHvI-7yWYo7733g.jpeg" /></figure><p>Although I tried Rust for a few days and even created several PR’s to Open Source projects using it, I never actually had an opportunity to come back and use this language in production. And about half of a year is quite a long term. Long enough for me to almost completely forget something, I started learning and left. I believe it’s quite good actually because I didn’t have all that messy knowledge, which was barely enough to understand that I like a thing, but not worth building the whole experience on it.</p><p>I have started from the beginning by visiting the <a href="https://www.rust-lang.org/learn/get-started">Get Started</a> section of the Rust website and then following <a href="https://doc.rust-lang.org/book/">The Rust Programming Language</a> book, which is used as “official” learning material.</p><h4>Maybe The Real Treasure Was the Friends We Made Along the Way</h4><p>For most of my career, I have used Python, JavaScript, or TypeScript. And though the latter is a statically typed, it’s still optional and often mixes with dynamically typed JS. I have started with C and Java, but that experience was brief before moving on.</p><p>With Rust, it was a refreshing experience in statically strictly typed languages. However, I would appreciate if Rust had a bit more introspection features like the ones Andrei Alexandrescu tells about in his <a href="https://www.youtube.com/watch?v=9zpHdh70Xdk">The Most Average Function There Is</a> talk (actually a great talk about the D lang).</p><p>Now, I will share some of the main points which stuck in my mind during this journey:</p><ul><li><strong>Love your types<br></strong>There are benefits of latent dynamical typing, but when switching to the strictly statically typed language, which still allows us to ignore explicit typings (yes, I’m talking about TypeScript), don’t omit them. It’s almost unbelievable how one can mess up with types and null/undefined values. Rust handles it for us with Option enum and usually able to catch on the compile phase. But in TypeScript, when we use any type, nothing protects us.</li><li><strong>Be lazy. Write tests. Don’t spend ages debugging</strong><br>We are lazy. Let’s admit that we don’t like doing something we think isn’t critical. Sometimes we think: “Nah, it’s an elementary function, what can go wrong here?” Usually, quite a lot 😉. Rust, with its rich compile-time errors, shows in how many ways we can forget some real minor things and later shoot ourselves in the leg.</li><li><strong>Performance is born within algorithms, much less within compilers and processors<br></strong>This is sometimes forgotten when we start learning. C is faster than JavaScript? Intel Xeon E7 will do the job much quicker than Intel Core i3, right? Well… You already get where I’m going, didn’t you?</li><li><strong>Platforms don’t limit language choices that much (wasm, lambda)<br></strong>I was quite surprised when I found projects like <a href="https://github.com/yewstack/yew">yew</a> (WebAssembly based client framework) or AWS Lambda environment for Rust. Today you can use almost every language for every platform if you try hard enough.</li></ul><h4>Learning materials to consider</h4><p>To start with, go to the Rust site and lurk around.</p><p>Listed under the “Get Started with Rust” section “<a href="https://doc.rust-lang.org/book/">the book</a>”, <a href="https://github.com/rust-lang/rustlings/">Rustlings</a> course and <a href="https://doc.rust-lang.org/stable/rust-by-example/">Rust by Example</a> are all excellent resources which one should check on the way to calling themselves a Rustacean.</p><p>However, just like with any other tool, you learn best when using it for solving real tasks. It can be a wide variety of things:</p><ul><li>Contribute to Open Source projects you like or even use<br>Search GitHub’s ‘trending’ repositories for Rust projects and</li><li>Write your scripts using Rust instead of Python or Bash</li><li>Sell your engineering manager idea of using Rust for one of your next non-core modules or R&amp;D projects</li><li>Create a pet project using it</li></ul><p>All these ways will give you an understanding of real-world problems and limitations of the language, as well as the ability to compare the ways you solve similar tasks with your current language and a new one.</p><h3>Conclusion</h3><p>Today is almost two months since I re-started learning Rust. I must admit that there wasn’t an opportunity to use it extensively in my team, but I believe it is an advantageous experience. Mostly because of the parallels I drew between it and TypeScript environment I mostly live in.</p><p>Besides, the main goal, to entertain me, was met for 100%.</p><p>I hope that you found something useful here and wish you to stay safe and interested in learning new during these challenging times.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6262d6c6f03" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[You don’t need Software Architecture for your startup]]></title>
            <link>https://medium.com/@knidarkness/you-dont-need-software-architecture-for-your-startup-a72051ea9fb4?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a72051ea9fb4</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[agile]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Fri, 06 Dec 2019 10:12:38 GMT</pubDate>
            <atom:updated>2019-12-06T10:12:38.100Z</atom:updated>
            <content:encoded><![CDATA[<h4>…unless your product has it already</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RtIUUGYT5ifzfUQE" /></figure><p>Currently, I am working for a rather small start-up. Once, I was thinking late at the night about our new product and how to design it properly. And below I will summarize a couple of the outcomes from that evening.</p><p>This article aims for those who participate in fairly early-stage start-ups either as technical leaders or as product owners. If you are working on a project which has defined scope which is unlikely to pivot for 180 degree you probably won’t be able to apply any of these ideas.</p><p>First of all, let’s ask ourselves a question:</p><blockquote>Do we need to spend hours and hours planning in detail all of interactions in our system and each of its components?</blockquote><p>Before answering this question I will share couple of my thoughts regarding it.</p><h4>Software Architecture not needed?</h4><p>For quite a long time I have believed that before going coding we have to plan. Even with agile-ish approaches, we should have a design of a whole system.</p><p>And truly, having a solid plan makes development easy. You and the developers’ team will appreciate having well done QML diagrams and API definitions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R0ucrYm1G30mdzZg.jpg" /><figcaption>You don’t want to give up flexibility on early stages of your product</figcaption></figure><p>But. You do give up the very basic thing for your business instead. <strong>Flexibility</strong>. Indeed, software architecture is all about setting borders.</p><p>When you have chosen between cloud and the on-premise solution you put quite vague limitations in available tools and what’s important on this stage pricing and scalability. It limits ways in which you can flex your product, but still allows plenty of options.</p><p>However, when you narrow boundaries further down by defining single language or framework, way to run your components (e.g. AWS Lambda or AWS EC Containers), and even internal structure (for instance, entities relations in form of QML diagrams) several problems arise.</p><p>Not only you spend a good amount of time not to bring value to your product. You also give yourself a bias about what is possible in your system. And can measure it not only by business requirements but also based on the cloud castle of your design approach. Sometimes it’s quite easy to forget that all the technical aspects should serve the product, not vice versa.</p><h4>Software Design</h4><p>While it is quite taxing to design your system as one entity what I find very helpful and beneficial in the long term is designing each more or less self-contained module as “well” as possible.</p><p>This applies not only to microservices or serverless functions but also to separate modules of your monolithic application as well.</p><p>By “well” here I generally mean maintainability, readability, test coverage. Probably, you won’t spend days and days optimizing for performance, as the more important result in the early stages is to get some feedback from real customers. However, I will tell about the opposite case in one of my next writings, so stay tuned.</p><p>On one hand, such prioritization doesn’t take much time for planning ahead of time on something you might not even need. On the other hand, it helps with building robust independent modules.</p><h4>Eventually consistent architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*FKtUpYnAjNJ66kwJ" /><figcaption>Funny, although it shows how you acquire all bits of knowledge to design robust architecture and possible “race conditions” if one tries to use it while on early stages</figcaption></figure><p>When your product comes to a point when the business model is quite stable as well as a set of core features you are going to provide it is time to go further. By this moment you should have pretty good knowledge of the domain you are in as well as the main performance bottlenecks of your system.</p><p>You collected knowledge you need to refine your chaotic architecture while spending zero-to-very-small amount of time directly.</p><p>Now you should be able to afford that time to be spent on architectural decisions and documentation. Also, you will likely grow quite fast from now (if the business model is proven and the backlog is quite long), so a set of boundaries will be really helpful to get a communication framework between developers as well as between applications.</p><h4>Conclusion</h4><p>Congratulations, you have come this far.</p><p>So, finally, do we need that Software Architecture thing about which guys at Google, Facebook, and other cool names will ask us during interviews?</p><p>I must admit, I have lied to you in the title. We do need it. But not in a way one might think about as something steel-hard which defines what we can and what we can’t do to make our customers happy.</p><p>Through clean and maintainable individual components, we will be able to reorganize them when the business we work for has a stable business model and <strong>needs</strong> which can be solved by complex architecture.</p><p>Before that point, we should concentrate on lower levels of abstractions as I maintainable and readable modules (not that great different will it be separate parts of your good old monolith, production-proven microservices or new shiny serverless functions). It will allow fast pivoting without throwing us back in time to redesign the whole system.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a72051ea9fb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Rust at Hacktoberfest in Eight Hours]]></title>
            <link>https://medium.com/better-programming/learning-rust-at-hacktoberfest-in-8-hours-7b788883c665?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b788883c665</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[hacktoberfest]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Mon, 14 Oct 2019 14:18:51 GMT</pubDate>
            <atom:updated>2019-10-15T22:53:27.981Z</atom:updated>
            <content:encoded><![CDATA[<h4>Tricks for mastering this relatively new technology</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KQNM4KTQlVGN0qT9-QU2gA.jpeg" /></figure><p>This year’s #hacktoberfest became a great opportunity for me to start learning <a href="https://www.rust-lang.org/">Rust</a>. In this article, I will share a couple of tricks on how to start mastering this relatively new technology.</p><h3>Motivation</h3><p>You rarely buy a car when you are happy with your current one. It’s the same with technology. I mostly use JavaScript (or TypeScript) with Node.js or Python at the moment.</p><p>These are great languages for their purposes, but as with everything, they have some limitations. Specifically, CPU-heavy tasks or things that demand the processing of large amounts of data. There is a movement towards solving those problems, however, it’s not that significant.</p><p>That’s where Rust comes in. I heard it is a strong static-type language which inherits the best of C/C++ and provides amazing performance, as well as guaranteeing memory and thread-safe execution.</p><p>What an amazing thing it must be! I thought so for months but never had time to test it.</p><p>And then I heard about Hacktoberfest from my team members at Redoc.ly who were organizing the event. “That’s it! My chance to switch and learn something new,” I thought. Also, I wanted to give myself the challenge of learning new technology fast.</p><p>So, I waited until the local Hacktoberfest event, went there, and started learning.</p><h3>What Is Hacktoberfest?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TIhCehpIzTeZonc94xvc8w.png" /><figcaption>Hacktoberfest 2019 logo</figcaption></figure><p>Long story short. <a href="https://hacktoberfest.digitalocean.com/">Hacktoberfest</a> is a great way to contribute to open source and get a cool T-shirt for it.</p><p>A bit more info: It is a month-long event by <a href="https://www.digitalocean.com/">Digital Ocean</a> and <a href="https://dev.to/">DEV</a>. During the event, everybody who creates four or more pull requests for open-source projects hosted on GitHub gets a chance to win a cool T-shirt.</p><p>The event gets more popular each year and in 2019, the are more than 500 on-site events where you can join a bunch of other programmers and solve some issues during the day.</p><p>For my experiment, I decided to go to the offline event as I hoped to get some help in case I would need it and also offer some of my experience with JavaScript or Python to other participants.</p><h3>Getting Rust</h3><p>To start, I navigated to the official <a href="https://www.rust-lang.org/">Rust website</a>. The first good sign was that it is really clear and intuitive. Just go to the <a href="https://www.rust-lang.org/learn/get-started">installation</a> page and copy-paste one single command into your terminal.</p><p>And that’s it. You only have a Rust environment manager (which is called rustup and manages all the toolchains you need), a package manager (cargo), and a bunch of other useful tools.</p><p>When you have the basic installation ready, go to the terminal and type rustup --version and cargo --version. That should print versions of the compiler and package manager installed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q7OnDPbdYy4fUhrQDEJ4qQ.png" /><figcaption>Checking we have installed the Rust toolset</figcaption></figure><p>Another awesome thing is that Rust <a href="https://www.rust-lang.org/tools">suggests</a> a bunch of editors/IDE’s plugins that make the development process painless in most of the existing editors (<a href="https://code.visualstudio.com/">VS Code</a>, <a href="https://atom.io/">Atom</a>, <a href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a>, and others).</p><p>For me, I chose to use VS Code which I use for my daily tasks.</p><p>Now, we are ready to start hacking.</p><h3>First Steps</h3><p>First of all, let’s create a simple Hello World application to familiarize ourselves with the basic syntax of the Rust.</p><p>The official documentation is a great place to start with that. It gives a simple example of a <a href="https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html">guessing game</a> that I have used to get some understanding of the language.</p><p>I won’t even provide it here, as the official tutorial is just awesome and iterates from literally a Hello World to a functional game.</p><p>The final result is the following code, which shows a basic example of flow control, I/O, and Rust’s very special approach called <a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html">ownership</a>. It might be somewhat mind-blowing at first glance, but after some practice, I saw the elegance of this solution.</p><p>The guessing game source code is in the official <a href="https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html">Rust tutorial</a>.</p><h3><strong>Checking the Toolbox</strong></h3><p>Sometime after the Hello World I was wondering, how rich is Rust with tooling for testing, linting, and so on?</p><p>As I have already mentioned, it has a bunch of editor plugins, but it also <a href="https://www.rust-lang.org/tools">turned out</a> that Rust has already developed an ecosystem for most use cases we might need during day-to-day tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TfLP9T18zr5tt_qUlj-ZrQ.png" /><figcaption>Screenshot from the Rust’s tools page</figcaption></figure><h3>Getting Your Hands Dirty</h3><p>That’s all fine, but it might become boring pretty fast to learn from such synthetical examples. So, I decided to go wild and use Rust for some real projects.</p><p>Generally, it’s a brilliant idea for any Hacktoberfest participant to find issues to work on before the event.</p><p>In our location, that was a major problem, as lots of people came with no ideas on what to do, so they spent a lot of time crawling over GitHub looking for any viable issues. It’s much easier to spend an hour or two a couple of days before.</p><p>I didn’t have any ideas for open-source and neither do we use Rust for main projects in my job (at least, for now). So, what can a programmer do, when they want to write some code but they’re not sure which code?</p><p><a href="https://github.com/search?l=&amp;o=asc&amp;q=stars%3A%3E500+language%3ARust&amp;s=stars&amp;type=Repositories">Browse open source</a>, of course. A couple of days before the event, I opened my browser to find some issues to work with. There are a whole lot of projects written in Rust on GitHub.</p><p>Quite a large part of them are popular and have some active issues. I used the <em>Advanced search</em> to find Rust repositories with more than 500 stars.</p><p>For myself, I found two projects to contribute to:</p><ul><li><a href="https://github.com/sharkdp/hyperfine/pull">Hyperfine</a> (A console benchmarking tool that I am using day-to-day.)</li><li><a href="https://github.com/Y2Z/monolith">Monolith</a> (A tool to download webpages and bundle them into a single .html file.)</li></ul><p>And that is another good point here. Try to find a project you already use. For some main-stream languages (such as JavaScript or Python), it’s easier as they have much more tooling written with them, but it’s also possible for ones like Rust or Go.</p><p>After a quick review of the repositories, I found one issue for each of them to work on. Both well-defined, which is extremely useful for a new language and project.</p><ul><li><a href="https://github.com/sharkdp/hyperfine/issues/193">Option to disable the default output entirely</a> in Hyperfine</li><li><a href="https://github.com/Y2Z/monolith/issues/2">Make it parse CSS as well, replacing url(https://medium.com/%E2%80%A6) with url(data:…)</a> in Monolith</li></ul><p>Issues are self-contained, so I didn’t have to dig through the whole codebase of the project to get those tasks done. A good idea was to contact the repository’s maintainers.</p><p>It allows you to:</p><ol><li>Ask if the issue is still relevant (as one of them was quite old).</li><li>Claim that you’ll work on it, so we don’t end up in a weird situation where two or more people are doing the same job.</li></ol><p>Authors of both projects responded fast, so I finished my preparations and was waiting for the event. I was not completely sure if I would be able to finish them in time, so I booked the next day to be able to finish contribution on time.</p><p>I will not go into details of the implementation as that’s quite project-specific. Here are just a few things I would like to mention about this experience, both regarding the Rust language and my approach in general:</p><h4>Ownership</h4><p>It blew my mind the first time I read about it. Then, I spent some time trying to understand it. Gave up. Tried again. Gave up. Decided to do something useful and eventually got the idea. After that — enjoy it.</p><h4>Timing is important</h4><p>It is important for every activity, but the format I participated in was an offline event. So it was twice as crucial as with the online-first approach.</p><p>So, plan, maybe even create some reading lists for top five problems you believe you might encounter.</p><h4>Networking makes a difference</h4><p>Working on issues was not that hard, so I had plenty of time to spend on networking.</p><p>And, even though there weren’t all that many participants, it was a great opportunity to borrow some experience from different ecosystems with which I rarely interact.</p><h3>Outcome and Thoughts</h3><p>I didn’t learn Rust in eight hours. However, it was an amazing start after which I:</p><ul><li>Started to contribute to open-source software.</li><li>Found tons of interesting projects with Rust.</li><li>Began diving into the long-forgotten (for me) domain of the low to mid-level programming languages.</li><li>Gathered with cool people.</li></ul><p>Such activities might not be necessary for your development as an engineer, but, I believe it will help with that.</p><p>So, I strongly encourage you to check the <a href="https://hacktoberfest.digitalocean.com/events">Events</a> page of <a href="https://hacktoberfest.digitalocean.com/">Hacktoberfest</a> and if there’s an event coming near you — visit it to have an amazing time.</p><h3>Additional Resources and Tutorials</h3><p>If you have heard about Rust (or other tools that you have never tried out), I strongly encourage you to give it a try!</p><p>Life’s short and we should use each opportunity to learn something new and potentially useful. Especially if the learning process will bring some value, not only for us but for the whole community as well.</p><p>If you, like myself, tried or are going to try Rust, you might end up liking it, looking for more information and tutorials.</p><p>In this part I will list the ones which I find the most useful:</p><ol><li><a href="https://doc.rust-lang.org/book/">The book</a> is an overview of the language from the first principles up to quite complicated approaches.</li><li><a href="https://github.com/rust-lang/rustlings/">Rustlings</a> guides you through installing the Rust toolchain and teaches you the basics of the language.</li><li><a href="https://doc.rust-lang.org/stable/rust-by-example/">Rust by example</a> is an alternative to reading pages of docs, Rust by example shows off a bunch of code and keeps the talking to a minimum. It also includes exercises!</li><li><a href="https://www.snoyman.com/blog/2018/10/introducing-rust-crash-course">Rust crash course</a><strong> </strong>is a set of articles that guide you from the very beginning to quite detailed language details. However, the style is reader-friendly to follow.</li></ol><p>Also, there are two books which tend to be highly appreciated on the web, as a good starting point. I did not read them yet, but will mention them here to show you the full picture:</p><ol><li><a href="https://www.amazon.com/Rust-Programming-Language-Covers-2018/dp/1718500440/ref=sr_1_1?keywords=The+Rust+Programming+Language.&amp;qid=1571179720&amp;sr=8-1">The Rust Programming Language</a>.<strong> </strong>This is a guide to Rust, written by two members of the Rust core team, with feedback and contributions from 42 members of the community.</li><li><a href="https://www.amazon.com/Programming-Rust-Fast-Systems-Development/dp/1491927283/ref=sr_1_1?keywords=Programming+Rust%3A+Fast%2C+Safe+Systems+Development&amp;qid=1571179757&amp;sr=8-1">Programming Rust: Fast, Safe Systems Development.</a> Written by two experienced systems programmers, this book explains how Rust manages to bridge the gap between performance and safety, and how you can take advantage of it.</li></ol><p>Finally, there are some useful questions and answers which you can find on <a href="https://stackoverflow.com/questions/tagged/rust">Stack Overflow</a>.</p><p>That’s the end of this piece, but I hope it’s the start of your experience with Rust. Keep hacking!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b788883c665" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/learning-rust-at-hacktoberfest-in-8-hours-7b788883c665">Learning Rust at Hacktoberfest in Eight Hours</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[Creating a GitHub App with Node.js]]></title>
            <link>https://medium.com/@knidarkness/creating-a-github-app-with-node-js-3bda731d45b9?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bda731d45b9</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Fri, 06 Sep 2019 00:23:32 GMT</pubDate>
            <atom:updated>2019-09-07T08:29:56.551Z</atom:updated>
            <content:encoded><![CDATA[<h4>Create your first GitHub Application to automate package management using Node.js</h4><p>Recently GitHub have updated their policy regarding integration with 3rd-parties applications. Earlier we could use Personal Access Tokens or OAuth apps to automate our interactions with the platform. Now they have added a third option — GitHub Apps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*zvwgIycD5ePCTv1fUNdeJg.png" /><figcaption>GitHub’s logo from the <a href="https://ru.wikipedia.org/wiki/%D0%A4%D0%B0%D0%B9%D0%BB:GitHub_Logo.png">Wikipedia</a></figcaption></figure><p>The main difference between OAuth Apps and GitHub Apps is that first act as an authorized user, who gave his credentials, while the latter act as a separate entity after installed to the organization or repository.</p><p>That might be a little confusing at first (as it was to me), so at GitHub they even have a flow-chart to decide what should you build. Generally, if you have a task which requires access to specific set of resources and also acts rather as an app (or bot, if you prefer), you can do pretty well with GitHub App.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MCM-tjDchJKYgFsbxg-Jjw.png" /><figcaption><a href="https://developer.github.com/apps/about-apps/">GitHub’s how-to on the decision making for OAuth vs GitHub Apps.</a></figcaption></figure><p>They do have quite nice <a href="https://developer.github.com/apps/quickstart-guides/">tutorials</a> on GitHub regarding these innovations. But they only use Ruby and cover quite a small amount of topics. Although it’s not that hard to apply those tutorials to Node.js environment I prefer to have handy information for each tool I use.</p><p>Today we will go through a quite simple flow to better understand nature of GitHub Apps as well as create our own with Node.js.</p><p>Here is the task we are going to solve. Let’s say we have a repository with a JavaScript package which is used across our other projects. When we release a new version of the package we wan’t to update the version used across our whole organization.</p><p>There are several options how we can approach this task with GitHub Apps:</p><ul><li>Automated. On push to master (and possibly successful CI build) iterate over all the repositories which are mentioned in the “used by” section of our package repository’s metadata. For each of such repositories we then create a branch for the update. In that branch modify “package.json” file to match a new version of our package and the create a Pull Request to the repository’s default branch.</li><li>Manual. If we do have reasonably small amount of projects dependent on our package what we can do is to have a list of all such repositories. When a new version of the package is released we can then go over them and update in the manner described for fully automated approach.</li></ul><p>In this tutorial we will start with the “manual” way (which actually isn’t that manual, as it is ran by a separate script). When done with it we’ll update to fully automated search of the repositories which use our package and proposing them PRs with updates to a newer version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6EYrY_v0HRq0d2nDTcCgZg.jpeg" /><figcaption><a href="https://www.needpix.com/photo/download/1530368/checklist-list-check-tick-box-boxes-pen-paper-white">Image from Needpix.com</a></figcaption></figure><p>Our mini-project consists of such parts:</p><ol><li>Create GitHub App on the GitHub Dashboard</li><li>Install it to the repository where we will test our interactions</li><li>Set up Node.js application for our project</li><li>Get started with authorization flow as a GitHub App and a single installation instance</li><li>Retrieve data from a remote repository</li><li>Modify <em>package.json </em>with a new version of the package</li><li>Create a new branch for our changes</li><li>Commit single file to a remote branch</li><li>Create Pull Request from branch we created to the default one</li></ol><p>Seems like we have stuff to do today. Then, let’s start!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6tdBR9LCneJ-U7QuQuAFKA.jpeg" /><figcaption><a href="https://www.maxpixel.net/Start-Direction-Clouds-Arrows-Sky-Change-948020">Image from Maxpixel.net</a></figcaption></figure><h4>Create GitHub App on the GitHub Dashboard</h4><p>Before actually going to GitHub we will need an address for the app’s webhook and “landing page”. You could either use your own cloud server (such as AWS, Digital Ocean or Heroku) or use a <a href="https://smee.io/">Smee.io</a>. Smee.io is a service which receives payloads at address available on the web and then sends them to your locally running application. If you use Smee.io, for now just click “Start a new channel” and copy the link in “Webhook Proxy URL” field. We’ll need it later on this step.</p><p>Now you need to <a href="https://github.com/settings/apps/new">create a GitHub App</a> in the “Developers settings” section of your GitHub settings page. Fill the name field with one you like, for example, “package.json update via Node.js” in my case. And then paste your webhook proxy URL (or actual URL of you server) into both “Homepage URL” and “ Webhook URL”.</p><p>Then, we need to set up permissions for our GitHub App. First of all, set “Repository contents” to <em>Read &amp; Write </em>access, as well as for “Pull Requests”. That should be enough for our first iteration and it is always possible to update these settings later. Finally, select “Only on this account” for <strong>“Where can this GitHub App be installed?” </strong>question and hit Create button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*gU6y4cbaFz1rhkqzLAbpDg.png" /><figcaption>Screenshot of the ‘About’ for my GitHub App for this tutorial</figcaption></figure><p>On the page of your app click “generate a private key” and save it to your project directory. We’ll use it later. Also, save App ID from the About section.</p><p>That’s it, we have created the GitHub App… at least, it is on GitHub now 😃</p><h4>Install GitHub App into the repository</h4><p>The next thing we should do is to give our new GitHub App access to one or several repositories we want it to be able to update. To do that, go to the public page of your app or click “Install App” in the app settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LPH9FnHWM_zXRqvxlGm03A.png" /></figure><p>You will see a list of accounts and organizations you have admin access to. In my case, that’s only my GitHub account, so I will press “Install”. Then we can chose if we want to give access to all or just a few repositories. I will use the first option, as it might be useful for our automated phase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ElV42jzZ6VsItCexQ2IJA.png" /></figure><p>When you click “Install” here, installation of your GitHub App is done. Now it has access to repositories in your account, can read &amp; write them as well as manage pull requests. From now we can actually start developing our implementation for the functionality we mentioned in the beginning of the article.</p><h4>Set up Node.js application for our project</h4><p>You can use your favorite boilerplate template, tutorial, or create project from scratch. What we want from our Node.js app is following:</p><ul><li>Support ES6 syntax</li><li>Have .env file with following data: GITHUB_APP_IDENTIFIER, PRIVATE_KEY, and GITHUB_WEBHOOK_SECRET if you used one.</li></ul><p>In my project I have just used such initialization sequence:</p><pre>npm install --save-dev <a href="http://twitter.com/babel/core">@babel/core</a> <a href="http://twitter.com/babel/node">@babel/node</a> <a href="http://twitter.com/babel/cli">@babel/cli</a> <a href="http://twitter.com/babel/preset-env">@babel/preset-env</a> eslint eslint-config-airbnb-base eslint-plugin-import babel-eslint eslint-plugin-babel</pre><p>And created files .babelrc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f497a04956978ee3e1f832a37d229e8/href">https://medium.com/media/8f497a04956978ee3e1f832a37d229e8/href</a></iframe><p>And .eslintrc.yml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79312ce492334a8af1a3581f097da341/href">https://medium.com/media/79312ce492334a8af1a3581f097da341/href</a></iframe><p>After that we can get to the first step of mastering GitHub App — authorizing as our application.</p><h4>Implement authorization flow for GitHub App</h4><p>There are two ways in which GitHub App can authorize:</p><ul><li>As an app. In such way it can get general information about the application itself.</li><li>As a specific installation of an app. Remember, when you have installed the app into your account? That is it, each of such installation can be accessed separately. <br>In this mode GitHub App can actually use all permissions we have granted it during set up.</li></ul><p>We need to start with authenticating as an app, as it is required for any further interaction with GitHub API. To authenticate as app we need to generate a JWT signed by app’s private key which we have saved previously. After that, we can use this JWT to acquire access tokens for separate installations.</p><p>We are using <a href="https://github.com/octokit/app.js">octokip/app</a> package for this part. Besides, we are going to use <a href="https://github.com/octokit/rest.js">octokit/rest</a> which is an official REST API client for GitHub API. There is an extensive docs on the octocit/rest methods and usage on their <a href="https://octokit.github.io/rest.js">GitHub Pages</a> as well.</p><p>Let’s create a file ./src/package-updater/auth.js, which will handle this flow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdb8947d6214d78cf42a1c95e874e60f/href">https://medium.com/media/bdb8947d6214d78cf42a1c95e874e60f/href</a></iframe><p>And test if it is working correctly on one of the repositories into which we have installed our new GitHub App. For me it will be the project’s repository itself. Here’s our ./src/main.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d34558d3507f3b403fe4042ece1d69a/href">https://medium.com/media/1d34558d3507f3b403fe4042ece1d69a/href</a></iframe><p>And we can run it with npm start. If you’ve done everything right, you`ll see something like following in you terminal output:</p><pre>v1.f4ada6a4ad7132f12f4a92123fab66ad3a5f0dda</pre><p>That’s it, installation access token which allows to work directly with our repository. Now let’s do something useful with it.</p><h4>Read data from a remote repository</h4><p>The first step to update package.json file in remote repository is to read it. So, why don’t we start implementing from this point. Octokit/rest will help us to do that.</p><p>First of all, we will need to create a REST client with installation level permissions. As we are going to use for all our API calls, let’s add following function to auth.js and update our exports:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec08976a89f899ef0824ecfdf678c737/href">https://medium.com/media/ec08976a89f899ef0824ecfdf678c737/href</a></iframe><p>Now we can try to read a file from a remote repository. For that purpose let’s create a getRemotePackageJSONObject function in ./src/package-updater/index.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd20a16fca6d79499748d6c885611236/href">https://medium.com/media/fd20a16fca6d79499748d6c885611236/href</a></iframe><p>And call this function from our main.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66cbbbc2cdaa4a3c5cdd008255902d22/href">https://medium.com/media/66cbbbc2cdaa4a3c5cdd008255902d22/href</a></iframe><p>If everything is fine, you will see contents of the remote <em>package.json </em>in your console.</p><h4>Modify the package.json object</h4><p>Now we can start modifying the data and keep working to push it back to repository. However, let’s make a break and think about architecture of our app for a minute.</p><p>We will have a module named ‘package-updater’ with a single entry point called updateRemoteRepository, which will take three parameters:</p><ul><li>Name of the repository in format “owner/repo”</li><li>Package name in package.json which is to be updated. For example, “express”</li><li>New version of the package we are modifying</li></ul><p>It will do all the machinery and return only result: <em>True</em>, if update was successful, or throw error otherwise.</p><p>For that purpose, let’s update our ./src/package-updater/index.js a bit and add these functions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a97041737eae397c6770035c72a04e7/href">https://medium.com/media/9a97041737eae397c6770035c72a04e7/href</a></iframe><p>And also update main.js so that it would call our brand new god-like function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/255c56761539395fdc144d5f86a4a7fa/href">https://medium.com/media/255c56761539395fdc144d5f86a4a7fa/href</a></iframe><p>Wow, now we can return to what this section should be about.</p><h4>Modify the package.json object (now for real)</h4><p>This is actually the easiest part of our task. We can just use spread syntax and have all the work done in these few lines in ./src/package-updater/index.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/136c230df4d87705d8ae827aa0676eb2/href">https://medium.com/media/136c230df4d87705d8ae827aa0676eb2/href</a></iframe><p>Now, when we have our data updated we need to push it back to the repository. And the first step to that goal is to…</p><h4>Create a branch on the remote repository</h4><p>We can’t commit our changes to master as it can possibly break existing solution. So, we’ll fix that by creating a new branch called “packageName-update-to-newVersion” each time we modify contents of package.json file.</p><p>We will create branch from the repository default branch as it highly likely has current release version.</p><p>And again we are going to use octokit/rest for this task.</p><p>Now, when we have a branch to commit our changes it’s time to…</p><h4>Commit file to the created branch</h4><p>Updating file is actually quite similar to creating a branch: we get a hashsum of the previous version of the file, specify the target branch and commit our new file encoded with base64.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd22c9ce460f4b4ac5f838b4a40a298f/href">https://medium.com/media/cd22c9ce460f4b4ac5f838b4a40a298f/href</a></iframe><p>The code here is quite self-explanatory so we will proceed to the final steps in updating a single repository.</p><h4>Create Pull Request for the branch with updates</h4><p>When we have pulled initial package.json, updated its contents, created a branch and committed into it there is only one final step to show results to the world — create pull request and wait until it is approved.</p><p>With GitHub API you can create a PR with a single call to the endpoint</p><pre>POST /repos/:owner/:repo/pulls</pre><p>Which again will be handle for us by octokit/rest package. Just as in the web version we should provide head and base branches, title and optionally body of the pull request. One more argument which we pass will allow other maintainers to modify pull request as it might be needed to adopt a new version of the library.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/368674a8eb5eb2bae98e639a8e20a671/href">https://medium.com/media/368674a8eb5eb2bae98e639a8e20a671/href</a></iframe><p>And that was the last piece we needed to finish this part of the app. Now we can update our updateRemoteRepository function so that it would use all our work:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4affd6c41f2a649a57f209b607c919f8/href">https://medium.com/media/4affd6c41f2a649a57f209b607c919f8/href</a></iframe><p>Now you can try to run the app with npm start and if you followed this tutorial in your repository which you pass as an argument in main.js a new PR should appear!</p><p>If you have some problems — you can check the full project in the GitHub repository by the following link:</p><p><a href="https://github.com/knidarkness/github-app-nodejs">knidarkness/github-app-nodejs</a></p><p>And now we are coming to the bonus part.</p><h3>Automate updating of dependency files when a new version is released</h3><p>If you are using Smee.io it’s time to install its client and launch it to redirect requests to your local machine:</p><pre>npm install --global smee-client<br>smee -u LINK_TO_YOUR_SMEE_ENVIRONMENT</pre><p>After that, all requests made to your smee.io proxy will be redirected to the localhost.</p><p>Our plan is following:</p><ol><li>Filter events that belong to our “source repository”. We need only those which are push events to master branch.</li><li>When triggering event happens we pull package name and a new version from the repository’s package.json</li><li>And iterate over all installations of our app. For each repository which is not a one with our package we check if it has our target package in its dependencies and if so — update that repository just as we did in previous section</li></ol><p>We will use Express to create our server so install it as npm install --save express.</p><p>In this part we will touch working with webhooks. First step will be to enable notifications for Push events. To do that go to “Permissions &amp; events” tab in your GitHub App settings and scroll to “Subscribe to events” section. There check “Push” checkbox and then save changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRkx1SYUh_BJgpm2OjCoAg.png" /></figure><p>Now try to commit and push something to your repository connected to the GitHub App. After that go to the Smee.io page (or you server logs). You will see something like following (not important fields are replaced with ‘…’):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6392aa456adf0f8b6d0455696483357/href">https://medium.com/media/b6392aa456adf0f8b6d0455696483357/href</a></iframe><p>In received event we care about two fields: payload.ref and payload.repository.master_branch. If they are the same, then it is a push to master, so we should trigger out update.</p><p>There is one large drawback in our approach: as we use the same app both for the repository with package we want to track as well as for repositories which we want to update. For now, to distinguish the “source” repository we will simply store its name in our .env file.</p><p>So, let’s create Express middlewares to filter all the requests we get to our server. If you are new to Express, you can check briefly <a href="https://expressjs.com/en/guide/writing-middleware.html">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81977f7a30453b6c7c75a4a2ca0c3210/href">https://medium.com/media/81977f7a30453b6c7c75a4a2ca0c3210/href</a></iframe><p>And then we can create a final version of our main.js. Replace existing code with a single Express endpoint which listens to port 3000, filters events and calls function to update all repositories dependent on our package (which we will create a bit later).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b814de3156499e5e87856826c764186/href">https://medium.com/media/1b814de3156499e5e87856826c764186/href</a></iframe><p>The last part is to update our package-updater/index.js so that it could perform batch update. To do that we will add a couple new functions to it which will be wrapped into updateAllDependents.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/252bab1f3b37bb8db916fc43f3b95e32/href">https://medium.com/media/252bab1f3b37bb8db916fc43f3b95e32/href</a></iframe><p>Also, it’s noteworthy that we should update our updateRemoteRepository flow to check if target repository has package we are updating in its dependencies at all. Simple solution is to check <em>package.json</em> dependencies section and if target package is not there, just ignore the repo. Implement it by yourself or check my version <a href="https://github.com/knidarkness/github-app-nodejs/blob/b30064e0c2f5ebee129adf97225be38b75a44c23/src/package-updater/index.js#L91">here</a>.</p><p>When done, create a repository with a JavaScript package which will be used as “source repository” now, and add this package name to dependencies of a few other repositories. Set “source repository” in the SOURCE_REPO variable in .env file. Install your application to these new repos and run server. Then, when you push to master of the package repository our newly written GitHub app will update dependencies inside other repositories.</p><p>Congratulations! We have gone from scratch to a working prototype of GitHub App with Node.js.</p><p>I hope this tutorial was useful for you and you`ll apply this info on your real projects. And one more link to the final project on GitHub:</p><p><a href="https://github.com/knidarkness/github-app-nodejs">knidarkness/github-app-nodejs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bda731d45b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to the OpenMP with C++ and some integrals approximation]]></title>
            <link>https://medium.com/swlh/introduction-to-the-openmp-with-c-and-some-integrals-approximation-a7f03e9ebb65?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7f03e9ebb65</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Sun, 01 Sep 2019 00:59:58 GMT</pubDate>
            <atom:updated>2019-09-02T13:57:34.244Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever wondered how all those high performance servers work? How can we process huge amount of data on some computational heavy algorithm? Usually there are two approaches. You can split the task between several independent instances of the app. Basically, that’s what map-reduce approach usually does and to some degree can be implemented via microservices paradigm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iSMuY0mwRrKtXF0kXG85Gg.jpeg" /></figure><p>However today we will focus on another way. You can use more powerful machine and design your app so that single instance of it could use all the the available resources. It simplifies development as you don’t care about network interactions. Also you don’t have performance overheads for the infrastructure.</p><p>Standard C++ multi-threading is cool. It`s fast and works well with numerous tasks. For example, what could be better for hello world apps on C++ multi-threading? But when it comes for some real world apps they usually take much time for implementation and testing. And that`s when OpenMP enters the game.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*x57bygKyWzfjDhCNZiTGUg.png" /></figure><blockquote><em>… a folk definition of insanity is to do the same thing over and over again and to expect the results to be different. By this definition, we in fact require that programmers of multithreaded systems be insane. Were they sane, they could not understand their programs.</em></blockquote><blockquote><em>Edward A. Lee</em></blockquote><p>OpenMP is a set of compiler`s directives, library procedures and environment variables which was developed for creating multi-threading apps for systems with shared memory (SMP computers) in 1997. This tool was initially designed for Fortan and later C and C++ were included as well. Nowadays, OpenMP is supported by the most popular C/C++ compilers: gcc, icc, PGI compiler. (Notice, that OpenMP isn`t any kind of library in typical sense of that word. It doesn`t affect the output binary file directly.)</p><p>It`s important to understand that OpenMP works just on compilation time — it converts preprocessor directives to native Fortran/C/C++ code which is then compiled to executable. This allows the OpenMP to provide kind of “user friendly” interface without loosing performance. In the best case, of course.</p><p>So, lets finally take a closer look at this probably useful tool and start writing some code! The first example will be pretty simple, lets just say ethernal “Hello world” from multiple thread.</p><pre>#include &lt;iostream&gt;</pre><pre>int main()<br>{<br>    #pragma omp parallel<br>    {<br>        std::cout &lt;&lt; &quot;Hello World&quot; &lt;&lt; std::endl;<br>    }<br>    return 0;<br>}</pre><p>To build this code you need to add -fopenmp flag, so that the compilation command would look like the following: g++ main.c -fopenmp -o main , and run it. Surprize! Instead of several rows with “Hello World” in each we received something like this:</p><pre>HelloHello</pre><pre>Hello<br>Hello</pre><pre>HelloHello<br>Hello<br>Hello</pre><p>That happens because multpiple threads are trying to write into the same “variable” (std::cout) simultaneously that causes data jumbling. Such situations are called race conditions and more details on that can be found <a href="https://support.microsoft.com/en-us/help/317723/description-of-race-conditions-and-deadlocks">here</a>.</p><p>By default, OpenMP runs as many threads as there`re separate execution units are available in your system. However, these values can be adjusted with the following method:</p><pre>#include &lt;iostream&gt;<br>#include &lt;omp.h&gt;<br>#include &lt;unistd.h&gt;</pre><pre>#define THREAD_NUM 4</pre><pre>int main()<br>{<br>    omp_set_thread_num(THREAD_NUM); // set number of threads in &quot;parallel&quot; blocks<br>    #pragma omp parallel<br>    {<br>        usleep(5000 * omp_get_thread_num()); // do this to avoid race condition while printing<br>        std::cout &lt;&lt; &quot;Number of available threads: &quot; &lt;&lt; omp_get_num_thread() &lt;&lt; std::endl;<br>        // each thread can also get its own number<br>        std::cout &lt;&lt; &quot;Current thread number: &quot; &lt;&lt; omp_get_thread_num() &lt;&lt; std::endl;<br>        std::cout &lt;&lt; &quot;Hello, World!&quot; &lt;&lt; std::endl;<br>    }<br>    return 0;<br>}</pre><p>And for this one, we will see the following output:</p><pre>Number of available threads: 4<br>Current thread number: 0<br>Hello, World!<br>Number of available threads: 4<br>Current thread number: 1<br>Hello, World!<br>Number of available threads: 4<br>Current thread number: 2<br>Hello, World!<br>Number of available threads: 4<br>Current thread number: 3<br>Hello, World!</pre><p>Now, when we have covered the most basic things, lets go to some more realistic situations. For example, take an array of integers and get square for each of them and store to another array. First of all, we`ll do this in usual single thread way:</p><pre>#include &lt;iostream&gt;<br>#include &lt;algorithm&gt;</pre><pre>#define ARRAY_SIZE 100000000<br>#define ARRAY_VALUE 1231</pre><pre>int main()<br>{<br>    int *arr = new int[ARRAY_SIZE];<br>    std::fill_n(arr, ARRAY_SIZE, ARRAY_VALUE);<br>    for(int i = 0; i &lt; ARRAY_SIZE; i++)<br>    {<br>        // do some relatively long operation<br>        arr[i] = arr[i] / arr[i] + arr[i] / 5 - 14;<br>    }<br>    return 0;<br>}</pre><p>Lets compile (with -O0 flag to prevent filling the array with final values on compilation) and run our program. For measuring performance we`ll use simple “time” command.</p><pre>time ./main<br>real: 0m0.902s<br>user: 0m0.864s<br>sys:  0m0.032s</pre><p>Now lets add multithreading for that example. The most obvious and wrong way is to do something like:</p><pre>#pragma omp parallel<br>{<br>    for(int i = 0; i &lt; ARRAY_SIZE; i++)<br>    {<br>        arr[i] = arr[i] / arr[i] + arr[i] / 5 - 14;<br>    }<br>}</pre><p>Unfortunately, that doesn`t work. Actually, we will do loop operations in all available threads for the same value of i. Fortunately, all we need to fix that is to surround the loop with our calculations with another omp block:</p><pre>#pragma omp parallel<br>{<br>    #pragma omp for<br>    {<br>        for(int i = 0; i &lt; ARRAY_SIZE; i++)<br>        {<br>            arr[i] = arr[i] / arr[i] + arr[i] / 5 - 14;<br>        }<br>    }<br>}</pre><p>Also, this code can be written as following because we can group different blocks:</p><pre>#pragma omp parallel for<br>for(int i = 0; i &lt; ARRAY_SIZE; i++)<br>{<br>    arr[i] = arr[i] / arr[i] + arr[i] / 5 - 14;<br>}</pre><p>So, the full code of the our OpenMP program is:</p><pre>#include &lt;iostream&gt;<br>#include &lt;algorithm&gt;<br>#include &lt;omp.h&gt;</pre><pre>#define ARRAY_SIZE 100000000<br>#define ARRAY_VALUE 1231</pre><pre>int main()<br>{<br>    omp_set_num_threads(4);<br>    int *arr = new int[ARRAY_SIZE];<br>    std::fill_n(arr, ARRAY_SIZE, ARRAY_VALUE);<br>    #pragma omp parallel for<br>    for(int i = 0; i &lt; ARRAY_SIZE; i++)<br>    {<br>        arr[i] = arr[i] / arr[i] + arr[i] / 5 - 14;<br>    }<br>    return 0;<br>}</pre><p>And after running our simple benchmark we get following results:</p><pre>time ./main<br>real: 0m0.430s<br>user: 0m0.980s<br>sys:  0m0.040s</pre><p>As you can see, the total processor time consumed by the program increased. That is because creating threads and switching between them also requires some resources. However, this time (and processor instructions respectively) “splits” between different execution units and as a result the real time the program worked has decreased by half. And this improvement was achieved for algorithm in which the only operation for each thread was addition of 2 divisions, which should not take more than ~ 1000 CPU clock cycles.</p><p>Finally, we will do quite real life thing — numerical approximation of the integral of the 2-variables function. This is so called de Jong function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/0*jp8M8YOqqri4xusu.gif" /></figure><p>First of all, we need to define this function itself. As for now, we don`t need it to be multithread or optimal, so the following implementation is OK for us.</p><pre>double f(double x, double y)<br>{<br>    double result = 0.002;<br>    for(int i = -2; i &lt;= 2; i++)<br>    {<br>        for(int j = -2; j&lt;=2; j++)<br>        {<br>            double temp_res = 1 / (5*(i+2) + j + 3 + std::pow(x - 16*j, 6) + std::pow(y - 16*i, 6));<br>            result += std::abs(temp_res);<br>        }<br>    }<br>    return 1.0 / result;<br>}</pre><p>And the main integrating function, which will do the most of the job. Now, lets marks fragments which can be quite easily paralleled.</p><pre>double integrate(double x_from, double x_to, double y_from, double y_to, double precision){<br>    int threads = omp_get_max_threads();<br>    double xInterval = std::abs((x_from - x_to)) / (double)threads;<br>    double result = 0;</pre><pre>    #pragma omp parallel for<br>    for(int i = 0; i &lt; threads; i++){<br>        double x_from_val = i * xInterval;<br>        double x_to_val = (i + 1) * xInterval;<br>        double y_from_val = y_from; // we create these variables, to avoid race condtions between different threads and moreover braking the data. Now, this is a thread-local variable.<br>        double y_to_val = y_to;<br>        double sum = 0;</pre><pre>        while(x_from_val &lt;= x_to_val)<br>        {<br>            double y0 = y_from_val;<br>            while(y0 &lt;= y_to_val) {<br>                sum += f1((2 * x_from_val + precision) / 2, (2 * y0 + precision) / 2) * precision * precision;<br>                y0 += precision;<br>            }<br>            x_from_val += precision;<br>        }<br>        #pragma omp critical<br>            result += sum;<br>    }<br>    return result;<br>}</pre><p>So, we have split our whole input space into such number of subspaces as many threads we can create. And now lets test it! And after that a single thread version of this code (simply comment #pragma lines).</p><pre>time ./mainThreads (4 threads)    time ./mainSingleThread<br>real: 0m0.030s                    real: 0m0.101s<br>user: 0m0.132s                    user: 0m0.100s<br>sys:  0m0.008s                    sys:  0m0.000s</pre><p>As we can see, OpenMP allows to simplify using of multi-threading in C/C++ and save you a lot of time. And you should not care about the locks and race conditions besides some quite straight tools.</p><p>So, is it a kind of silver bullet which can do just everything in multi-threading domain? Unfortunately, there`s no silver bullets. And business systems are slow, and legacy code is everywhere. OpenMP can`t deal with computations which rely on previous chains of computation chain. So, if you were going to calculate factorials or something like that, it is a bad idea. However, if you want to do something that you could do with usual C/C++ threads but don`t want to spend your time for playing with some low level stuff which will not give you any benefits, OpenMP might be just a right tool for you.</p><p>If you want to read more about OpenMP, these links are quite cool:</p><ul><li><a href="http://www.linux-mag.com/id/4609/">Nice start for further reading</a></li><li><a href="https://computing.llnl.gov/tutorials/openMP/">List of tutorials from Lawrence Livermore National Laboratory</a></li><li><a href="https://hpc.llnl.gov/training/tutorials">Set of tutorials here on Medium</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7f03e9ebb65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-the-openmp-with-c-and-some-integrals-approximation-a7f03e9ebb65">Introduction to the OpenMP with C++ and some integrals approximation</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>
        <item>
            <title><![CDATA[JavaScript DOM access methods speed benchmark]]></title>
            <link>https://medium.com/@knidarkness/javascript-dom-access-methods-speed-benchmark-64bb6fd4f8a6?source=rss-11c7292a812b------2</link>
            <guid isPermaLink="false">https://medium.com/p/64bb6fd4f8a6</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dom]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Sergey Dubovyk]]></dc:creator>
            <pubDate>Mon, 09 Apr 2018 14:18:56 GMT</pubDate>
            <atom:updated>2018-04-09T16:53:09.289Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_NTV4bDmjU7VaMZ6_5unWw.jpeg" /><figcaption>Some HTML code to attract your attention. Forget it</figcaption></figure><p>As a web-developer I had to work with with DOM tree uncountable number of times. Well, without all those shiny frameworks like Angular, React or Vue.js it’s actually impossible to do anything with a webpage without something like this:</p><pre>document.querySelector(&#39;#my-selector&#39;).innerText = &#39;Welcome to a new benchmark&#39;;</pre><p>In my experience I have met several most common approaches to accessing DOM elements:</p><ol><li>Using document.querySelector method</li><li>Accessing elements by id (with getElementById method)</li><li>Accessing elements by class names (e.g. document.getElementsByClassName(‘my_class’)[4])</li></ol><p>Also, some front-end developers store received elements as variables, and some not.</p><p>After having quite intensive discussion about which method should be used and if there`s any sense in storing elements in your app as variables/constants I have finally decided to investigate this question a little and write this review.</p><p>So, join me and let’s found who’s the fastest.</p><p>First of all we should understand that a single selection operation is ultimately fast and there`s probably no sense in measuring single operation time. To notice any significant performance difference we will use the following function skeleton:</p><pre><strong>const </strong>ITERATION_NUM = 500000; // this number allows us to have execution time of 1-2 seconds<br><br><strong>const </strong>measureSomeMethod = () =&gt; {<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++) {<br>        document.querySelector(&#39;#my-target&#39;).innerText = &#39;Benchmark test&#39;;<br>    }<br>};</pre><p>And to measure performance we introduce a simple benchmark method:</p><pre><strong>const </strong>benchmark = (func, params) =&gt; {<br>    <strong>const </strong>start = Date.now();<br>    <strong>const </strong>result = func(...params);<br>    <strong>const </strong>end = Date.now();<br>    <strong>const</strong> executionTime = end - start<br>    console.log(`Function took ${executionTime} ms to finish`);<br>    <strong>return </strong>[result, executionTime]<br>};</pre><p>Now, we can move to the testing itself. For this example we will use such a simple elements tree:</p><pre>&lt;div class=&quot;parent&quot;&gt;<br>    &lt;div class=&quot;child-1&quot;&gt;&lt;/div&gt;<br>    &lt;div class=&quot;child-1&quot;&gt;&lt;/div&gt;<br>    &lt;div class=&quot;child-1&quot;&gt;&lt;/div&gt;<br>    &lt;div class=&quot;child-1&quot;&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;&lt;/div&gt;<br>        &lt;div class=&quot;child-2&quot;&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;&lt;/div&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;&lt;/div&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;&lt;/div&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;&lt;/div&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;&lt;/div&gt;<br>            &lt;div class=&quot;child-3&quot;&gt;<br>                &lt;div class=&quot;child-4&quot; id=&quot;target&quot;&gt;&lt;/div&gt;<br>            &lt;/div&gt;<br>        &lt;/div&gt;<br>    &lt;/div&gt;<br>&lt;/div&gt;</pre><p>Firstly, we are going to evaluate the most straight forward approach — query selector by classpath without any caching. Method should just render 500 00 updates (set innerText property of our element with some random text). It will look as following:</p><pre><strong>const </strong>ITERATION_NUM = 500000;</pre><pre><strong>const </strong>accessByQuerySelector = (selector, text) =&gt; {<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>        document.querySelector(selector).innerText = text;<br>    }<br>};</pre><pre><strong>benchmark</strong>(accessByQuerySelector, [&#39;.parent .child-1 .child-2 .child-3 .child-4&#39;, &#39;z tv cjhhb, Ybyf&#39;]);</pre><p>This method tooks 1780 ms to complete on my MacBook Pro 2015. As for me, that’s a bit long. So, let’s try to improve this result.</p><p>The first idea I had was to use querySelector, but instead class path search element by id. So, my new benchmark was:</p><pre><strong>const </strong>ITERATION_NUM = 500000;</pre><pre><strong>const </strong>accessByQuerySelector = (selector, text) =&gt; {<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>        document.querySelector(selector).innerText = text;<br>    }<br>};</pre><pre><strong>benchmark</strong>(accessByQuerySelector, [&#39;#target&#39;, &#39;z tv cjhhb, Ybyf&#39;]);</pre><p>In this version, it gives relatively better result — it takes only 1498 ms to render our updates, which is ~15% better than initial results. However, I believe we could do it better. Probably, much better.</p><p>What if we will look by class name using getElementsByClassName instead of querySelector?</p><p>Here it is:</p><pre><strong>const </strong>accessByClassName = (text) =&gt; {<br>   <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>       <strong>const </strong>parent = document.getElementsByClassName(&#39;parent&#39;)[2];<br>       <strong>const </strong>child1 = parent.getElementsByClassName(&#39;child-1&#39;)[3];<br>       <strong>const </strong>child2 = child1.getElementsByClassName(&#39;child-2&#39;)[7];<br>       <strong>const </strong>child3 = child2.getElementsByClassName(&#39;child-3&#39;)[5];<br>       <strong>const </strong>child4 = child3.getElementsByClassName(&#39;child-4&#39;)[0];<br>       child4.innerHTML = text;<br>   }<br>};</pre><pre><strong>benchmark</strong>(accessByClassName, [&#39;z tv cjhhb, Ybyf&#39;]);</pre><p>This version seems to be faster — 1292 ms. Probably, that’s because in case of querySelector our browser had to do syntax parsing of the query itself, while here we did it for him by using <strong>document.getElementsByClassName</strong> method.</p><p>It was logically to think, that we can do the same trick with <strong>document.getElementById </strong>and that’s right.</p><pre><strong>const </strong>accessById = (id, text) =&gt; {<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>        document.getElementById(id).innerText = text;<br>    }<br>};</pre><pre><strong>benchmarkTime</strong>(accessById, [&#39;target&#39;, &#39;z tv cjhhb, Ybyf&#39;]);</pre><p>Due to DOM ids’ nature — hashmap it gives another performance improvement, now it takes only 1148 ms to render element changes.</p><p>Not bad, ya? This is approximately ~35.5% faster than the first approach (<strong>document.querySelector</strong> and class path). But can we do it better (hmm, probably that’s my new favorite question)? <strong>YES</strong>. We don’t cache our elements? Let’s see if it will help us.</p><p>We will test only two approaches with caching: naive first one (class path and document.querySelector) and the fastest (accessing by ids).</p><pre><strong>const </strong>ITERATION_NUM = 500000;<br><br><strong>const </strong>accessByIdAndCache = (id, text) =&gt; {<br>    <strong>const </strong>element = document.getElementById(id);<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>        element.innerText = text;<br>    }<br>};</pre><pre><strong>const </strong>accessByQuerySelectorAndCache = (selector, text) =&gt; {<br>    <strong>const</strong> element = document.querySelector(selector);<br>    <strong>for </strong>(<strong>let </strong>i = 0; i &lt; ITERATION_NUM; i++){<br>        element.innerText = text;<br>    }<br>};</pre><pre><strong>benchmark</strong>(accessByIdAndCache, [&#39;target&#39;, &#39;z tv cjhhb, Ybyf&#39;]);<br><strong>benchmark</strong>(accessByQuerySelectorAndCache, [&#39;.parent .child-1 .child-2 .child-3 .child-4&#39;, &#39;z tv cjhhb, Ybyf&#39;]);</pre><p>And that gave us 784 ms and 870 ms… Query selector approach still loses, but that’s due to long time it needs to parse query (which is then cached by broswser). And this is ~55% and ~51% faster than my initial approach.</p><p>So, if you want to do DOM tree modifications fast — it might be usable to go as simple as caching and using old simple methods as accessign elements by id or class name.</p><p>To conclude and recap, let’s just look at the following comparison diagram of the methods listed above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aLP7BhWGNwHN07B3scMj2g.png" /></figure><p><em>P.S. It’s important to understand, that this experiment has its limitations. For example, you can’t assign elements to variables if you need to rebuild DOM tree or set innerHTML to their parents somewhere else in the code. However, even regarding that, it’s quite important to keep in mind that way you access DOM elements might be another method to increase performance of your application.</em></p><p>Thank you for your time and have fun with coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64bb6fd4f8a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>