<?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 Stas on Medium]]></title>
        <description><![CDATA[Stories by Stas on Medium]]></description>
        <link>https://medium.com/@s.kutyepov?source=rss-9cb3582ac954------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*luNEazCfbW1ToaeUDXi8GA.jpeg</url>
            <title>Stories by Stas on Medium</title>
            <link>https://medium.com/@s.kutyepov?source=rss-9cb3582ac954------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:47:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@s.kutyepov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How ~~ a.k.a double bitwise NOT operator in JavaScript can help boost performance of your app]]></title>
            <link>https://medium.com/@s.kutyepov/how-a-k-a-double-bitwise-not-operator-in-javascript-can-help-boost-performance-of-your-app-3d973078ff42?source=rss-9cb3582ac954------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d973078ff42</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bitwise]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ecmascript]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Stas]]></dc:creator>
            <pubDate>Thu, 07 May 2020 00:34:26 GMT</pubDate>
            <atom:updated>2020-05-07T00:41:16.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhBP1DMaF5Q3BW4phs2QxQ.png" /></figure><h3>tl;dr;</h3><p><em>~~</em> casts NaN, undefined, null, false, [], {} , “” and other falsey values to 0. It can also replace Math.floor() with ~~5.5 // 5. Rumor has it ~~ is faster than Math.floor() in some browsers.</p><p><strong>Warning: </strong>~~ will not work as a replacement of Math.floor()on an integer greater than 2147483647 or less than -2147483648 (max value for signed 32 bit integer).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0df39daa75679409c2b531fc695a7c2b/href">https://medium.com/media/0df39daa75679409c2b531fc695a7c2b/href</a></iframe><h3>!tl;dr;</h3><p>~~ comes very handy when there is a need to increment something that can be undefined.<br>Let’s say we want to build a frequency table that shows how many times numbers appears in an array:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfb586994e96eeb8f805d7ae4a765b40/href">https://medium.com/media/cfb586994e96eeb8f805d7ae4a765b40/href</a></iframe><p>During the very first iteration n = 1, and such key “1” doesn’t exist in map yet, so its value is undefined. Thus, undefined + 1 // NaN. The same is happening during the second iteration, and then during the third: NaN + 1 is also NaN. So, resulting frequency table will look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce9e3f45c9c55e2a915e02994322a065/href">https://medium.com/media/ce9e3f45c9c55e2a915e02994322a065/href</a></iframe><p>To fix that, we would need to check for the value in map[n].</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1cdf23f0ea12ecd5b122558a2b4563c/href">https://medium.com/media/d1cdf23f0ea12ecd5b122558a2b4563c/href</a></iframe><p>This is a one liner, but can we do better? Yes, with ~~!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3642abbd8d7651d6c52fe25d1019bb5/href">https://medium.com/media/f3642abbd8d7651d6c52fe25d1019bb5/href</a></iframe><p>Some <strong><em>naive </em></strong>tests with the average value of 10 runs on node v10.13.0</p><p>Math.floor(): <em>3.390ms</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad5590c6dbe83a57e2821ed8236c8bf9/href">https://medium.com/media/ad5590c6dbe83a57e2821ed8236c8bf9/href</a></iframe><p>~~: <em>1.458ms</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9a582b2ed97589208a97a7ad7635796/href">https://medium.com/media/b9a582b2ed97589208a97a7ad7635796/href</a></iframe><p><strong>WOW</strong>! That’s more than 2x performance improvement!</p><p>Strict equality check (<em>something </em>=== undefined): <em>0.994ms</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/088682a940b2b76bb4e050c371b97da7/href">https://medium.com/media/088682a940b2b76bb4e050c371b97da7/href</a></iframe><p>~~: <em>0.866ms</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4eae86f0192bbfb5a1edac2d4e988aba/href">https://medium.com/media/4eae86f0192bbfb5a1edac2d4e988aba/href</a></iframe><p>and roughly 10% improvement here.</p><p>So, not only ~~ helps to write more concise code but also improves performance.</p><p>Let’s discuss here or tweet @<a href="https://twitter.com/kutyepov">kutyepov</a></p><p>As mentioned in the very beginning of the post, ~~ cannot be used with an integer greater than 2147483647 or less than -2147483648. This is due to fact that JavaScript is using 64 bits for <a href="https://developer.mozilla.org/en-US/docs/Glossary/Number">number</a> type and converts it to 32 bit to perform bitwise operations. I will go into detail on how bitwise operators work in on my next posts. Stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d973078ff42" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>