<?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 Lior Agami on Medium]]></title>
        <description><![CDATA[Stories by Lior Agami on Medium]]></description>
        <link>https://medium.com/@lioragami?source=rss-1c775666d8f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FYYuOxAro-mRE-Qd7nbyMw.jpeg</url>
            <title>Stories by Lior Agami on Medium</title>
            <link>https://medium.com/@lioragami?source=rss-1c775666d8f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 16:58:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lioragami/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[The first time I discovered JavaScript Proxy powers]]></title>
            <link>https://lioragami.medium.com/the-first-time-i-discovered-javascript-proxy-powers-5a9326eaf6a3?source=rss-1c775666d8f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a9326eaf6a3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[creativity]]></category>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[complexity]]></category>
            <dc:creator><![CDATA[Lior Agami]]></dc:creator>
            <pubDate>Fri, 25 Dec 2020 09:04:13 GMT</pubDate>
            <atom:updated>2022-10-16T10:06:47.187Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Power of Proxy</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y5cVfYzgKJnxcQmAz8JbRw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@nkachanovskyyy">Nikita Kachanovsky</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>.</figcaption></figure><p>This journey began when I was writing about Javascript’s Object and Map data types. My main goal was to demonstrate some best practices and handy tricks that we could use, but more importantly, I wanted to show how we choose between data types and why.</p><p>During my writing, I talked about getting the Map’s size with its built-in .size() method, and for an Object, there are several ways to do that. One of them is by using -Object.values(obj).length. This method allows us to get an Object’s length by converting it to an Array and use its <em>.length </em>property.</p><h3>The problem</h3><p>In my opinion, one of the downsides of the Object data type is that a Javascript Object doesn’t have a .<em>length </em>property. The .<em>length </em>property exists for arrays but objects do not contain it.</p><p>Ex:</p><p>const arrLength = [1,2,3].length; // 3</p><p>const objLength = {a:1,b:2,c:3}.length; // undefined</p><p>As I’ve mentioned earlier, Object.values() method returns an Array out of the parameters that you passed in. In our case — an Object.</p><p>But what is happening under the hood when we use this method?</p><p>Let’s talk about it.</p><h3>A deep dive into the ocean</h3><p>Take a look at Ecmascript’s 2021 language specifications (<a href="https://tc39.es/ecma262/#sec-object.values">here</a>).</p><p>When we call Object.values() three main operations occur :</p><p>1. The abstract operation ToObject takes our argument and converts it to a value of type Object.</p><p>2. The abstract operation EnumerableOwnPropertyNames takes the Object, iterates over it, doing a process (which you can find in the specs), and returns properties.</p><p>3. The abstract operation CreateArrayFromList takes those properties (l<a href="https://tc39.es/ecma262/#sec-list-and-record-specification-type">ist</a>), creating an array from this list, and returns it. It is used to create an Array object whose elements are provided by elements.</p><p><strong>Let’s talk a bit about the complexity</strong></p><p>We can see that whenever we are using Object.values(), no matter the Object’s length, it’s getting iterated over more than once but with non-nested loops. We Describe this type of operation by using the <strong>Big O notation </strong>as O(n) — read about it <a href="https://dzone.com/articles/learning-big-o-notation-with-on-complexity">here</a>.</p><p>A quicky about <strong>Big O notation -</strong></p><blockquote>Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. — Wikipedia.</blockquote><p>Simply put, the number of operations that will be executed on this object is unknown. As the object grows, the operation’s growth will be linear.<br>Remember that we want to achieve that O(1) — whatever the Object’s size will be, we want to execute only one operation. Make sense?</p><h3>So how does the Proxy object fit in?</h3><p>First, let&#39;s see what happens if we don’t use the Proxy object:</p><p>So, we want to use an object and still have access to the .<em>length </em>property in a way that would not cost too many operations under the hood.</p><p><strong>If using an object it will look like the following:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd2d62e4d2fcc0f9d7e4df17d5676a31/href">https://medium.com/media/fd2d62e4d2fcc0f9d7e4df17d5676a31/href</a></iframe><p>This code sample isn’t the best approach because we always need to take care of the .<em>length </em>property by calling the <em>manageLength </em>function. In certain cases (Ex: if we remove key-value pairs from this object), it won’t go as expected. We will need to manually call <em>manageLength </em>function since we can’t handle an object property deletion with a setter/getter kind of function. Probably we will find ourselves with an endless circle of bugs and unnecessary efforts to manage this type of operation. I hope I made myself clear.</p><h3><strong>Let’s get a bit familiar with the Proxy object.</strong></h3><blockquote>The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for that object. — MDN</blockquote><p>A Proxy is created with two parameters:<br><strong>Target: </strong>the original object which you want to proxy.<br> <strong>Handler: </strong>An object that defines which operations will be intercepted and how to redefine intercepted operations.</p><p>It may sound frightening, but I promise that you will understand Javascript Proxy’s usage at the end of this article, and you’ll not be afraid anymore 😄.</p><p><strong>Let&#39;s take a look at a basic implementation of a Proxy:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e515b38dd95a7615a13b1df0b7a82cb5/href">https://medium.com/media/e515b38dd95a7615a13b1df0b7a82cb5/href</a></iframe><p>We declared a <em>users </em>object that holds two key-value pairs. We also declared a handler object with a <em>getter</em> function that returns a string and invokes when accessing the Proxy.</p><p>Now let’s see how we solve our issue with the object’s length we want to maintain and manage, and how we work-around that traditional approach of Object.values(obj).length with the Proxy object.</p><h3>The solution</h3><p>Let’s see how we nail our goal (keep having the <em>.length </em>property within our object), reduce time complexity, and avoid unnecessary operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33704bf440de428fd2ead068351fea47/href">https://medium.com/media/33704bf440de428fd2ead068351fea47/href</a></iframe><p>Amazing. What happened was that we declared a setter method that took care of the object’s property assignments and another method that took care of the object’s property deletion (note that objects don’t have this option).</p><p>With Proxy’s built-in methods, we can have much more control of our target Object and maintain the .<em>length </em>property using readable and efficient code that will not cost us too many operations and reduces time complexity.</p><h3>In summary</h3><p>The features Proxy provides genuinely remarkable, and you may find it very useful in different situations. Now that you know the basics and you have in mind what a Proxy is capable of, you can think of implementing it when needed.</p><p><strong>But most important:</strong></p><p>The main point was to encourage you — my dear readers, to use your tools wisely, get familiar with them, and don’t be afraid to use one for the first time.</p><p>Usually, there are several ways to solve a problem. No matter if you solved a particular problem already — question it, examine other ways, try to think outside the box for a minute, and I guarantee that you will become a better version of yourself.</p><p>Thanks for reading! ❤🤘</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a9326eaf6a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>