<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nathan G Bornstein</title>
    <description>The latest articles on DEV Community by Nathan G Bornstein (@greenteaisgreat).</description>
    <link>https://dev.to/greenteaisgreat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F802698%2F29cac776-8f58-4bba-ac04-7d9c95151c4c.JPG</url>
      <title>DEV Community: Nathan G Bornstein</title>
      <link>https://dev.to/greenteaisgreat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/greenteaisgreat"/>
    <language>en</language>
    <item>
      <title>Say Goodbye to console.time() and Hello to performance.mark()!</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sat, 18 Oct 2025 05:03:38 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/say-goodbye-to-consoletime-and-hello-to-performancemark-5eom</link>
      <guid>https://dev.to/greenteaisgreat/say-goodbye-to-consoletime-and-hello-to-performancemark-5eom</guid>
      <description>&lt;p&gt;&lt;strong&gt;As&lt;/strong&gt; JavaScript developers, our primary goals can typically be summed up within three &lt;strong&gt;&lt;em&gt;very&lt;/em&gt;&lt;/strong&gt; broad concepts when it comes to our applications:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make the user's life easier&lt;/li&gt;
&lt;li&gt;Make the user's desires met&lt;/li&gt;
&lt;li&gt;Make the user's experiences efficient&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While some may not agree with those 3 points as being the focal purpose in what they create (looking at you &lt;a href="https://en.wikipedia.org/wiki/Black_hat_(computer_security)" rel="noopener noreferrer"&gt;black hats&lt;/a&gt;), I'd wager that most developers would at least want a modicum of those three points blended into their blood, sweat and tears. My post today primarily locks on to the 3rd point, efficiency. &lt;/p&gt;

&lt;p&gt;If you aren't already aware of the statistics for &lt;a href="https://www.semrush.com/blog/bounce-rate/" rel="noopener noreferrer"&gt;bounce rates&lt;/a&gt; in relation to the timing of the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/First_contentful_paint" rel="noopener noreferrer"&gt;First Contentful Paint&lt;/a&gt;, then let me illustrate a graph to show you how fast n' dirty the times we're currently living in are:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn3mfrkpz7bablx4oj50.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwn3mfrkpz7bablx4oj50.jpeg" alt="A graph depicting generalized website bounce rates, with the earliest bounce rate occurring at 90% within a 1 to 5 second load time" width="800" height="408"&gt;&lt;/a&gt;&lt;br&gt;
Credit for image to &lt;a href="https://www.semrush.com/blog/bounce-rate/" rel="noopener noreferrer"&gt;Semrush&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;As you can see, in an &lt;em&gt;extremely&lt;/em&gt; depressing fashion, if your application doesn't load within 1 to 5 seconds, a devastating percentage of your users are going to GTFO on outta there. Oh, you say your page is going to load at the 7 second mark? Kiss any &lt;a href="https://www.investopedia.com/terms/c/call-action-cta.asp" rel="noopener noreferrer"&gt;CTA's&lt;/a&gt; goodbye, my friend. &lt;/p&gt;

&lt;p&gt;Alright, have I scared you enough? Well I hope I have, because now we can finally get to what it is I want to talk about and stop with the background information like I'm some kind of YouTuber needing to increase their video duration. &lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;LET'S GET TO IT&lt;/strong&gt;
&lt;/h2&gt;



&lt;p&gt;Since the inception of &lt;a href="https://nodejs.org/download/rc/v8.0.0-rc.1/docs/api/console.html#console_console_time_label" rel="noopener noreferrer"&gt;Node v 0.1&lt;/a&gt;, &lt;code&gt;console.time()&lt;/code&gt; has been an extremely valuable method to determine how long a given &lt;strong&gt;process&lt;/strong&gt; takes. This "&lt;strong&gt;process&lt;/strong&gt;" could be a range of any number of things, from a specific function within an app to the entirety of that application altogether. The way that &lt;code&gt;console.time()&lt;/code&gt; operates is within either a Node.js environment or within a browser. This isn't a core method within ECMAScript and therefore, doesn't run purely within JavaScript (but what does nowadays?). &lt;/p&gt;

&lt;p&gt;However, as a baby that's freshly born grows and absorbs information like a sponge, so does technology progress. There are some quirks within &lt;code&gt;console.time()&lt;/code&gt; that renders it inadequate for certain modern applications. Let's sum those up now: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;Inconsistent Precision Across Environments:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
When measuring performance locally using &lt;code&gt;console.time()&lt;/code&gt;, Node is using the &lt;a href="https://nodejs.org/api/process.html#processhrtimebigint" rel="noopener noreferrer"&gt;&lt;code&gt;process.hrtime()&lt;/code&gt;&lt;/a&gt; method internally to assess your performance metric. Generally, browsers nowadays internally use &lt;code&gt;performance.now()&lt;/code&gt; to gauge their own metrics via their own built-in devtools. This basically amounts to you checking timing on your own device vs what the user's given browser is going to implement for efficiency metrics and that difference can be &lt;em&gt;staggering&lt;/em&gt;. Which leads us to our second point:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;Console Overhead:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
While I'm sure many of us have machines more than capable of benchmarking the latest graphically and process intensive metrics, there still comes a price with using your terminal to benchmark your application's performance. This rings especially true when it comes to shorter tasks and tighter loops. If you're trying to shave milliseconds off of some method you're developing, &lt;code&gt;console.time()&lt;/code&gt; is going to leave you scratching your head when it performs within a browser context vs what your console informed you of. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;And finally, the bright, shining beacon that led me to write this psychological vomit: &lt;/p&gt;



&lt;p&gt;3. &lt;u&gt;&lt;strong&gt;No Built-In Statistics:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
That's right, within the &lt;code&gt;Performance&lt;/code&gt; object, you're given a plethora of built-in methods to determine a wide variety of performance metrics. For &lt;code&gt;console.time()&lt;/code&gt;, you're just measuring milliseconds on your own device without any additional context. That's like reading the headline to an article and then arguing with your friends to "just trust" you, "bro".&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Now&lt;/strong&gt; that I've been dragging &lt;code&gt;console.time()&lt;/code&gt; through the mud, I hope that's been a sort of catalyst to show you how much better &lt;code&gt;performance.mark()&lt;/code&gt; is in relation to all that smack talk. And best of all, &lt;code&gt;Performance&lt;/code&gt; is a "native" process that works within both Node and practically all browsers! I mean, I guess &lt;code&gt;console.time()&lt;/code&gt; does too, but it's better; just trust me, bro. However, instead of drawing on any more comparisons, as I've been doing, let me just speak on the performance of &lt;code&gt;Performance&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;Performance is Non-Thread-Blocking&lt;/strong&gt;&lt;/u&gt;: &lt;br&gt;
&lt;code&gt;console.time()&lt;/code&gt; inherently thread-blocks within its processes (look, I know I just said I wasn't going to shit-talk &lt;code&gt;console.time()&lt;/code&gt; anymore, but just stick with me, OK?). The way in which the &lt;code&gt;Performance&lt;/code&gt; object works is quite elegant in its execution, via its own &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance" rel="noopener noreferrer"&gt;Performance API&lt;/a&gt;. Whenever you use &lt;code&gt;Performance&lt;/code&gt;, it creates a high-resolution timestamp within the &lt;strong&gt;&lt;em&gt;environment's&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://blog.codeminer42.com/the-monotonic-clock-and-why-you-should-care-about-it/" rel="noopener noreferrer"&gt;monotonic clock&lt;/a&gt; and then registers that within JavaScript as a basic integer for later use, to determine the speed. And &lt;strong&gt;YES&lt;/strong&gt;, that does mean you should test your application across all major browser environments using &lt;code&gt;performance.mark()&lt;/code&gt;. &lt;em&gt;However&lt;/em&gt;, that essentially amounts to an O(1) performance register within memory across ALL devices!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;SO many metrics:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
As I had mentioned previously, &lt;code&gt;Performance&lt;/code&gt; offers an incredible number of metrics to finely tune precision for basically any quantitative measurement you're looking for. This deserves its own post by itself, but here's a brief summary of the major methods within it:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;strong&gt;performance.now():&lt;/strong&gt;&lt;/u&gt;
Precisely measures code duration via high-resolution timestamps, regardless of your machine's console fuckery, or pretty much any other external factors. Oh and, I know I keep saying "high-resolution timestamp", but all that means is a number within sub-millisecond precision. For comparisons' sake, &lt;code&gt;Date.now()&lt;/code&gt; is within a millisecond's precision (I s2g, if you chastise me about comparisons one more time...).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a very general method, meaning you can plug just about any process, no matter how big or how small between it. Here's a working example of such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;yourFunctionOrAppIDontKnow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sub-ms&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;strong&gt;performance.mark(label):&lt;/strong&gt;&lt;/u&gt;
This is essentially the same label you would use with &lt;code&gt;console.time()&lt;/code&gt;; it marks the beginning and the end of the process you wish to benchmark. In use, it would look something similar to this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;startFetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;endFetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's important to keep in mind that this particular method does &lt;em&gt;&lt;strong&gt;NOT&lt;/strong&gt;&lt;/em&gt; do the actual performance metric, like &lt;code&gt;performance.now()&lt;/code&gt; would do. All this method encapsulates are the &lt;em&gt;beginning&lt;/em&gt; and the &lt;em&gt;ending&lt;/em&gt; markers between your time comparison. This is useful for pinpointing specific processes within your application, if you don't wish to test the entirety of it. So with that, let's move on to see how to actually capture those specific metrics in conjunction with &lt;code&gt;performance.mark(label)&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;strong&gt;performance.measure()&lt;/strong&gt;&lt;/u&gt;
Let's say we have a &lt;em&gt;super sweet&lt;/em&gt; Fibonacci sequence that's tail-call optimized (I can hear the groans now). If we wanted to measure that using this new &lt;a href="https://www.merriam-webster.com/dictionary/fandangle" rel="noopener noreferrer"&gt;fandangled&lt;/a&gt; performance metric, we'd apply it as such:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// start the timing&lt;/span&gt;
&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;startFib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// start the function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fibTail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// end the timing&lt;/span&gt;
&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;endFib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// measure the actual duration:&lt;/span&gt;
&lt;span class="c1"&gt;// `fibDuration` is the specific label given to performance.measure():&lt;/span&gt;
&lt;span class="c1"&gt;// `startFib` and `endFib` were given labels to performance.mark() earlier&lt;/span&gt;
&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;measure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fibDuration&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;startFib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;endFib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// retrieve the measurement&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;measure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getEntriesByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fibDuration&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// ?????&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`fib(&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;) = &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Execution time: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;measure&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; sub-ms`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;strong&gt;WHAT&lt;/strong&gt; is this new &lt;code&gt;performance.getEntriesByName()&lt;/code&gt; method I speak of?? Let's dive into it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;u&gt;&lt;strong&gt;performance.getEntriesByName():&lt;/strong&gt;&lt;/u&gt;
This method returns an array of &lt;code&gt;PerformanceEntry&lt;/code&gt; objects, each with their own designated purpose or &lt;code&gt;type&lt;/code&gt;. In our instance, we want the default &lt;code&gt;"mark"&lt;/code&gt; type, as we've &lt;em&gt;marked&lt;/em&gt; our function we wish to benchmark. If you don't provide an argument for that second &lt;code&gt;type&lt;/code&gt; parameter, &lt;code&gt;"mark"&lt;/code&gt; is what it defaults to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many, many more methods contained within the &lt;code&gt;Performance&lt;/code&gt; object, and for my eagle-eyed readers out there, you probably noticed the &lt;code&gt;measure.duration()&lt;/code&gt; method in the previous code snippet. Since I don't have the bandwidth to continue elaborating on such things, you'll just have to figure that out &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/PerformanceEntry/duration" rel="noopener noreferrer"&gt;on your own&lt;/a&gt; ;)&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;And&lt;/strong&gt; that about sums it up, in terms of demonstrating the betterment of &lt;code&gt;performance.mark()&lt;/code&gt; in relation to&lt;code&gt;console.time()&lt;/code&gt;. In summary, I don't think &lt;code&gt;console.time()&lt;/code&gt; is doo-doo ca-ca, but I do think that applications needing more rigid of constraints in their timing can 100% benefit from using the &lt;code&gt;Performance&lt;/code&gt; object and its &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance" rel="noopener noreferrer"&gt;many methods&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you made it this far, I Love You! &lt;br&gt;
&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>benchmark</category>
      <category>profiler</category>
    </item>
    <item>
      <title>crypto.randomUUID</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Wed, 25 Jun 2025 20:53:14 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/cryptorandomuuid-5ab9</link>
      <guid>https://dev.to/greenteaisgreat/cryptorandomuuid-5ab9</guid>
      <description>&lt;p&gt;If you clicked on this article thinking it has something to do with crypto currency, I'll let you off easy and quickly say that it most certainly has nothing to do with &lt;em&gt;that&lt;/em&gt; kind of crypto. But if you've ever found yourself in a pinch needing a unique ID when none exists, then you're at the right place!&lt;/p&gt;

&lt;p&gt;Today's post is going to be short and sweet, with a little bit of context surrounding that sweetness.&lt;/p&gt;

&lt;p&gt;As developers, we're constantly iterating over items and needing to generate unique IDs or keys for each of those items. Sometimes we're able to retrieve it pretty easily, based on what's in the returned API call. Other times, we're needing to hack together our own helper function or &lt;strong&gt;&lt;em&gt;GASP&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  USE THE INDEX AS THE KEY
&lt;/h2&gt;




&lt;p&gt;Which actually does work in some cases, but not all. But that forbidden practice isn't what I'm here to talk about. What I'm here to talk about is a handy-dandy method you can use that's widely supported across all major browsers, &lt;em&gt;AND&lt;/em&gt; you don't even need to import it or list it in your dependencies file! So without further ado, let's see it in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resultFromApiCall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;resultFromApiCall&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;YourComponent&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;))}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's pretty much all there is to it. Simply plug that into your key or anything else that will need its own unique ID and it will safely generate that for you. The returned ID will be a string that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;e75b6774-580c-41f1-8c70-bb6a8be40591&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ID that's generated is currently a &lt;a href="https://www.rfc-editor.org/rfc/rfc4122#section-4.4" rel="noopener noreferrer"&gt;version 4 UUID&lt;/a&gt;, which basically means that for each section of that ID separated by hyphens, it uses cryptographically secure byte generation using hexadecimal encoding. The very same hexadecimal system that we use when choosing colors in CSS, 0-9 and a-f! &lt;/p&gt;

&lt;p&gt;If you'd like to read more about this process, check out this &lt;a href="https://w3c.github.io/webcrypto/#Crypto-method-randomUUID" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's also important to note that this method is only functional within secure contexts, so don't use it on your project if it uses the dated &lt;code&gt;http://&lt;/code&gt; protocol, only &lt;code&gt;https://&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's all for this one, thank you as always and I'll catch you in the next one! &lt;/p&gt;

</description>
      <category>generate</category>
      <category>unique</category>
      <category>id</category>
      <category>javascript</category>
    </item>
    <item>
      <title>The Ongoing War Between CJS &amp; ESM: A Tale of Two Module Systems</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Thu, 11 Jul 2024 22:27:03 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/the-ongoing-war-between-cjs-esm-a-tale-of-two-module-systems-1jdg</link>
      <guid>https://dev.to/greenteaisgreat/the-ongoing-war-between-cjs-esm-a-tale-of-two-module-systems-1jdg</guid>
      <description>&lt;p&gt;(This was originally an in-person talk I gave, so the following graphics are from said presentation slides, the bulk of which are attributed to &lt;a href="https://slidesgo.com/" rel="noopener noreferrer"&gt;SlidesGO&lt;/a&gt;; check them out for awesome &amp;amp; free slides! Just be sure to credit them during your presentation &amp;lt;3)&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;em&gt;&lt;strong&gt;A&lt;/strong&gt;&lt;/em&gt; battle has raged on for &lt;em&gt;millennia&lt;/em&gt;, claiming numerous casualties in its wake, where no single individual, corporation nor entity was safe from the wreckage that followed in its trail; hidden deep within the binary bunkers, there festered something so awful, so &lt;em&gt;terrible&lt;/em&gt; that mankind is still reeling from it today.
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Of course, I'm speaking of the dreaded 20-year war that is commonly known as...
&lt;/h2&gt;




&lt;h2&gt;
  
  
  COMMON JAVASCRIPT (CJS)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  VS
&lt;/h2&gt;

&lt;h2&gt;
  
  
  ECMA SCRIPT MODULES (ESM)
&lt;/h2&gt;




&lt;p&gt;So with &lt;strong&gt;&lt;em&gt;that&lt;/em&gt;&lt;/strong&gt;, my friends, let's dive into the ongoing war between the two different kinds of module implementations that exist within our favorite language, known as JavaScript; CJS &amp;amp; ESM.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwrz4i43a6mj7vdyes3r.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwrz4i43a6mj7vdyes3r.JPG" alt="What even is CJS and ESM?" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yeah, what &lt;em&gt;&lt;strong&gt;is&lt;/strong&gt;&lt;/em&gt; ESM and CJS, anyways? Well, by definition, ESM stands for ECMAScript modules and CJS stands for Common JavaScript. ECMA is the governing body for JavaScript standardization and CJS, well…that’s kind of just the term that was given before any governing body had any say in the matter. Let’s first dive into some of the history surrounding these two methodologies&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxhdxdtk0sofygpu4nzvw.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxhdxdtk0sofygpu4nzvw.JPG" alt="Differences between ESM &amp;amp; CJS" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By now, I’m sure we’ve all used both variations of these syntaxes to bring in some sort of code that we’ve needed within our existing codebase. CJS syntax uses the &lt;code&gt;require()&lt;/code&gt; and &lt;code&gt;export.modules()&lt;/code&gt; commands to either bring in and/or send out some bit of code we’ve either written ourselves or have used by someone else to make our code do what it’s supposed to do. &lt;/p&gt;

&lt;p&gt;Same exact thing for ESM syntax; it uses the &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;export&lt;/code&gt; commands to either bring in or ship out some code we’ve either written ourselves or have used by someone else to make our code work. Seems like both are basically doing the same thing, right? So...&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3jr2e28gvx1fr3veot1.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3jr2e28gvx1fr3veot1.JPG" alt="What's the difference between ESM and CJS?" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, that’s my current job to explain to you, but just let me say that difference is literally EVERYTHING. Let's explore how these two seemingly comparative modules differ so greatly.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlse0oyxrajls7keimox.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlse0oyxrajls7keimox.JPG" alt="CJS vs ESM methods" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At its core, CJS is &lt;strong&gt;&lt;em&gt;synchronous&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;dynamic&lt;/em&gt;&lt;/strong&gt; in its methods of importing and exporting modules. This means that a single module is only able to be loaded at a time and the next module is unable to be processed until the previous one has finished. The dynamic aspect comes into play when only certain modules are required in, as they’re needed at run-time. &lt;/p&gt;

&lt;p&gt;ESM on the other hand, is &lt;strong&gt;&lt;em&gt;asynchronous&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;static&lt;/em&gt;&lt;/strong&gt;; this means that modules are able to be loaded in, regardless of any current processes occurring. The static aspect of ESM pertains to the &lt;em&gt;analysis&lt;/em&gt; of a given module and its dependencies, once they’re compiled. &lt;/p&gt;

&lt;p&gt;Overall, both of these module implementations have their own strengths and weaknesses; the most common use-case for either of these, is for front end processes to use ESM and back end processes to use CJS. &lt;/p&gt;

&lt;p&gt;But how did we arrive at this point? And why are computer nerds everywhere choosing factions to represent their chosen side to fight the good fight? To answer that, we need to consult the dusty tomb of JavaScript’s history.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdccj12g99aomc956p3v.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftdccj12g99aomc956p3v.JPG" alt="ESM &amp;amp; CJS history" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CJS was first introduced in the year 2009 by Mozilla engineer &lt;a href="https://www.kevindangoor.com/about/" rel="noopener noreferrer"&gt;Kevin Dangoor&lt;/a&gt;, after the need for the modularizing of code became evident for larger code bases. &lt;strong&gt;&lt;em&gt;Imagine&lt;/em&gt;&lt;/strong&gt; having to contain an entire full stack project within a single file of code; &lt;em&gt;NO THANK YOU!&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;So with the mother of innovation fully at work, CJS was born to address the lack of a module system in JavaScript, primarily for server-side applications. And a fun fact about CJS is that it was initially called &lt;em&gt;ServerJS&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So that’s great and all, but if CJS solved so many problems, why was there a need for ESM to exist in the first place? The answer to that lies within the need for a standardized approach to the existing module system, as well as the need for asynchronous functionality that front end based applications necessitate.&lt;/p&gt;




&lt;h2&gt;
  
  
  ENTER: ESM
&lt;/h2&gt;

&lt;p&gt;ESM was introduced in 2015, alongside the advent of ES6, which brought about significant changes that a lot of us take for granted as the baseline approach for operating within JavaScript. &lt;/p&gt;

&lt;p&gt;Ever since, the question on everyone’s mind has been, “why don’t we get rid of CJS and only implement ESM as the main source of truth for the modules we use?” &lt;/p&gt;

&lt;p&gt;…do you &lt;em&gt;want&lt;/em&gt; the truth? Do you &lt;em&gt;really want&lt;/em&gt; the truth?&lt;/p&gt;

&lt;h2&gt;
  
  
  Well I don’t think you can &lt;em&gt;handle&lt;/em&gt; the truth!
&lt;/h2&gt;

&lt;p&gt;But alright, the truth is… &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy0kzhh72469vdia9ovc.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy0kzhh72469vdia9ovc.JPG" alt="Legacy code and an unwillingness to change" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you see before your very eyes. That’s right my fellow friends, CJS solely exists because old engineers &lt;strong&gt;&lt;em&gt;refuse&lt;/em&gt;&lt;/strong&gt; to adapt to any kind of new technology and are firmly set in their tired and decrepit ways.&lt;/p&gt;

&lt;p&gt;Alright, so not really, but based off of the digging around I’ve done, that seems to make up at &lt;em&gt;least&lt;/em&gt; 20% of the reason why. &lt;/p&gt;

&lt;p&gt;The bulk of the reason why these two approaches exist lies within the fact that Node.js has signed a blood-pact with CJS and isn’t functional without it, unless of course you bring in some external configurations to make server-side processes use ESM syntax, against their will (for example Typescript). &lt;/p&gt;

&lt;p&gt;It’s also worth it to note that the vast majority of browsers simply refuse to acknowledge CJS’s existence, unless you bring in some kind of transpiler to force that acknowledgement. Although, before I get too ahead of myself, let’s gaze into the opening statement I had first written out at the beginning of this post; insecurity vs. security: &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F30mna21wbcr4ytrg5yfz.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F30mna21wbcr4ytrg5yfz.JPG" alt="insecurity vs. security" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back in 2016, a single individual disrupted the entire ecosystem of the web by deleting a mere 11 lines of code, as you see above. Sites that used React, such as Facebook, Instagram…basically any website you visit these days, experienced a downtime of about 2 hours thanks to this deletion of only &lt;em&gt;11 lines&lt;/em&gt; of code. &lt;/p&gt;

&lt;p&gt;Seems like nothing super impressive, right? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Wrong.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The single fact that over half of the Internet’s most-visited sites experienced that long of a down time, and billions of dollars lost in ad revenue, speaks volumes to just how fragile of an ecosystem we’re operating within. This piece of code, called "&lt;strong&gt;left-pad&lt;/strong&gt;", is a package found within npm which solely operates based off of CJS modules. &lt;/p&gt;

&lt;p&gt;The beauty and downfall of npm-based packages, and in turn, CJS modules, is that anyone is able to alter (or delete) their packages operating within CJS boundaries, and if the vast majority of websites are using that very code you’ve written, either knowingly or blissfully ignorant, significant changes can happen in the blink of an eye. &lt;/p&gt;

&lt;p&gt;I encourage you to &lt;a href="https://www.theregister.com/2016/03/23/npm_left_pad_chaos/" rel="noopener noreferrer"&gt;research&lt;/a&gt; more into this event, to see the full brevity of this small change that disrupted countless operations. &lt;/p&gt;

&lt;p&gt;This isn’t to say that if this code had been using ESM modules that this could’ve been avoided, I only point this out to show that regardless of how much security we try to incorporate into the tools we use, we very much rely on external factors, even if we know it or not. &lt;/p&gt;

&lt;p&gt;So with that, let’s dive into how ESM and CJS incorporate their different versions of security:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc0e7ou044lfkirbnr06.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc0e7ou044lfkirbnr06.JPG" alt="Pick your flavor of security" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ESM&lt;/strong&gt; introduced a number of security enhancements, which I’ll touch on a few of them briefly. &lt;/p&gt;

&lt;p&gt;The first big implementation was an improved &lt;em&gt;&lt;strong&gt;module-specific scoping&lt;/strong&gt;&lt;/em&gt;, which means whoever is importing a package has to define their own variables, as opposed to having variables brought into the global scope. &lt;/p&gt;

&lt;p&gt;Another big security feature upgrade was the use of &lt;strong&gt;&lt;em&gt;import maps&lt;/em&gt;&lt;/strong&gt;. Import maps are aiming to replace the use of bundlers, such as webpack, by calling in the packages you need in the actual &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag in an HTML file. You simply link the URL within an &lt;code&gt;imports&lt;/code&gt; property and this helps with the resolution of the module, as well as controlling the origin of where that module is coming from. Currently, only a single import map is allowed per document, but changes are in the pipeline to address this limitation. &lt;/p&gt;

&lt;p&gt;Now let’s get into the &lt;em&gt;extremely&lt;/em&gt; limited security features of the CJS module.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CJS&lt;/strong&gt; offers a &lt;em&gt;little&lt;/em&gt; bit of &lt;em&gt;&lt;strong&gt;encapsulation&lt;/strong&gt;&lt;/em&gt;, but this is basically just from the encapsulation that JavaScript already offers natively from its scope within functions. So, not so much of a CJS-specific security implementation, it’s moreover a nice thing that happened to already be there. &lt;/p&gt;

&lt;p&gt;The next security feature that was, in fact, implemented specifically for CJS is our trusty friend &lt;code&gt;package-lock.json&lt;/code&gt;. This concept was introduced by npm to ensure &lt;strong&gt;&lt;em&gt;reproducibility&lt;/em&gt;&lt;/strong&gt; within any given module, so that what you got was going to function as the creator intended.&lt;/p&gt;

&lt;p&gt;Although the beauty and terror within that statement is the latter; you’re at the behest of the creator. So by and large, most npm packages aren’t going to be malicious. But it only takes one bad actor to alter a single file and half of the Internet is down for an indeterminate amount of time. &lt;/p&gt;

&lt;p&gt;So with all of these competing factors...&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9ai3d4z93n9llii1wla.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9ai3d4z93n9llii1wla.JPG" alt="What's a dev to choose?" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Should we choose &lt;strong&gt;&lt;em&gt;team ESM&lt;/em&gt;&lt;/strong&gt; for their improved security features and the slowly encroaching takeover that all modules, both back and front end, are eventually going to  succumb to? &lt;/p&gt;

&lt;p&gt;Should we choose &lt;strong&gt;&lt;em&gt;Team CJS&lt;/em&gt;&lt;/strong&gt; for their tried and true methods standing the test of time and the ease of use that a vast majority of npm packages are based on? &lt;/p&gt;

&lt;p&gt;Or should we all just go the &lt;strong&gt;&lt;em&gt;nihilism route&lt;/em&gt;&lt;/strong&gt; and finally realize either of these viewpoints are essentially meaningless? &lt;/p&gt;

&lt;p&gt;Well, allow me to give you a &lt;em&gt;100%&lt;/em&gt; objective and absolutely &lt;strong&gt;&lt;em&gt;NOT&lt;/em&gt;&lt;/strong&gt; biased take:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqqiyc7p9un366q1vxe0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqqiyc7p9un366q1vxe0.JPG" alt="It depends" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though it may seem like I’ve been framing either module systems as at odds against each other, the truth is that each system has their own use-case. &lt;/p&gt;

&lt;p&gt;The main take-away here is that, if you’re writing a new app from scratch, try to implement ESM if you can. ESM has significant security improvements, and just the fact that you only need to use a single type of syntax to import and export for both the front end and the back end, is a pretty cool thing.&lt;/p&gt;

&lt;p&gt;CJS has been the de-facto standard for a very long time and is still widely implemented within the entire ecosystem that we all call home. The fact that it’s existed, and still is thriving for this long, stands as a testament to just how well it’s doing its job. So if you’re working within an existing full stack application and the back end is already using CJS, it’s probably fine to stick with it. Plus, you can also &lt;a href="https://pawelgrzybek.com/all-you-need-to-know-to-move-from-commonjs-to-ecmascript-modules-esm-in-node-js/" rel="noopener noreferrer"&gt;convert it to ESM&lt;/a&gt; pretty effortlessly. &lt;/p&gt;

&lt;p&gt;Overall, both methods have their own use-cases and a single one isn’t necessarily better than the other and both will more than likely be around for a very long time.&lt;/p&gt;




&lt;p&gt;And with that my friends, we’ve reached the end of this controversial talk. If there’s anything you take away from this, it’s that &lt;strong&gt;&lt;em&gt;absolute power&lt;/em&gt;&lt;/strong&gt; corrupts &lt;strong&gt;&lt;em&gt;absolutely&lt;/em&gt;&lt;/strong&gt; and ESM and CJS are friends, rather than foes.&lt;/p&gt;

&lt;p&gt;Thank you for reading! ~~ &lt;strong&gt;&amp;lt;3&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>modules</category>
      <category>cjs</category>
      <category>esm</category>
    </item>
    <item>
      <title>Read This if You Hate Ternary Operators</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Thu, 28 Mar 2024 19:34:05 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/read-this-if-you-hate-ternary-operators-59m5</link>
      <guid>https://dev.to/greenteaisgreat/read-this-if-you-hate-ternary-operators-59m5</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;First&lt;/em&gt;&lt;/strong&gt; things first, if you want to see the rest of the animeme banner in all of its glory, which is what more than likely drew you to this article in the first place, you can find it &lt;a href="https://preview.redd.it/ternary-operator-v0-cxvk4zf2h21b1.png?auto=webp&amp;amp;s=8bc73ffe4a0781c43f561a81575fb9a0c6e1e7a3" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Now that the critical and pressing matters are out of the way, please grant me a couple of minutes of your time to persuade you to fall &lt;strong&gt;madly&lt;/strong&gt;, &lt;em&gt;deeply&lt;/em&gt; in love with ternary operations (thank you, &lt;a class="mentioned-user" href="https://dev.to/kevinpowell"&gt;@kevinpowell&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;When I first saw ternary operators, I became &lt;em&gt;immediately&lt;/em&gt; angry. Unnecessarily angry, even. There was absolutely no reason for me to be feeling that way over some plaintext displaying on my screen. But it happened and I allowed myself to feel that way, which is usually a healthy thing. But not in this case. &lt;/p&gt;

&lt;p&gt;To save any future programmers from this irrational behavior, I want to share a &lt;a href="https://en.wikipedia.org/wiki/Mnemonic" rel="noopener noreferrer"&gt;mnemonic&lt;/a&gt; I thought of that will hopefully make sense of what is otherwise some confusing syntax at first glance.&lt;/p&gt;




&lt;p&gt;So here, we have a typical ternary operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;highest&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;highest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The way that I choose to see this expression is quite a literal one. Whenever I see that question mark after an expression, I read it as if someone was asking a question:&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is &lt;code&gt;num1&lt;/code&gt; greater than &lt;code&gt;num2&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Oh, it isn't? Well then, is &lt;code&gt;num1&lt;/code&gt; less than &lt;code&gt;num2&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's &lt;em&gt;still&lt;/em&gt; not true?? Hmmm, well then that only leaves me with one option, which is that &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; must be equivalent to each other!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Therefore, &lt;code&gt;highest&lt;/code&gt; will be assigned to &lt;code&gt;num1&lt;/code&gt; (or &lt;code&gt;num2&lt;/code&gt;, your call).&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;While this may seem pretty silly if you've already made peace with ternary operations, I really wish someone would've explained it to me in this way when I first came across them.&lt;/p&gt;

&lt;p&gt;Another important thing about ternary operators to keep in mind, is that syntax formatting is &lt;em&gt;everything&lt;/em&gt;. Much like the way I structured the ternary operator above, it really helps with readability when it's organized as such. It's pretty similar to how &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch" rel="noopener noreferrer"&gt;switch cases&lt;/a&gt; are formatted, when you think about it. &lt;/p&gt;

&lt;p&gt;There's no hard and fast rule about how a ternary should be structured, but as long as you're separating out each expression on a new line, you should be pretty good.&lt;/p&gt;




&lt;p&gt;That's all I have for this one. I hope it was a step-forward in seeing the beauty of ternary operators if you've harbored any resentment towards them in the past. They can be a beautiful thing! &lt;/p&gt;

&lt;p&gt;~~ &amp;lt;3&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ternary</category>
      <category>conditional</category>
      <category>operator</category>
    </item>
    <item>
      <title>Breadth-First Search in a 2D Matrix</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sat, 24 Feb 2024 18:07:14 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/breadth-first-search-in-2d-matrices-3e2j</link>
      <guid>https://dev.to/greenteaisgreat/breadth-first-search-in-2d-matrices-3e2j</guid>
      <description>&lt;p&gt;&lt;strong&gt;O&lt;/strong&gt;ne of the most tricky algorithms I've come across so far has been those involving bread-fir....er, I mean breadTH-first searches (&lt;strong&gt;BFS&lt;/strong&gt;) in a 2D matrix. Often times you can recognize these algorithms based on the prompt for the challenge. If the description is saying something along the lines of "find the shortest path" and a 2D matrix is involved, chances are that's a big hint to use a BFS. &lt;/p&gt;

&lt;p&gt;Before we dive into the intuition, implementation and finally the solution, I first want to point out that this type of BFS is much different from that of a binary search tree BFS. Though some similarities exist, the data structures are different (binary search tree BFS are tree-based and this BFS is graph-based).&lt;/p&gt;

&lt;p&gt;And with that, let's first explore the intuition behind a BFS!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4do3m7er7nusc24rf7td.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4do3m7er7nusc24rf7td.JPG" alt="BFS Intuition 1/2" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;This representation of a typical BFS was created by &lt;a href="https://www.youtube.com/@Reducible" rel="noopener noreferrer"&gt;Reducible&lt;/a&gt; on YouTube and I &lt;em&gt;highly&lt;/em&gt; recommend checking them out and giving them a sub. Reducible's &lt;a href="https://www.youtube.com/watch?v=xlVX7dXLS64" rel="noopener noreferrer"&gt;video&lt;/a&gt; covers this concept very well, but the gist of  it is depicted succinctly here; the green lines illuminating the path on the vertices (points) shows the order of the initial traversal: &lt;/p&gt;




&lt;blockquote&gt;
&lt;h2&gt;
  
  
  0 -&amp;gt; 1
&lt;/h2&gt;
&lt;h2&gt;
  
  
  0 -&amp;gt; 2
&lt;/h2&gt;
&lt;h2&gt;
  
  
  1 -&amp;gt; 3
&lt;/h2&gt;
&lt;h2&gt;
  
  
  1 -&amp;gt; 4
&lt;/h2&gt;
&lt;h2&gt;
  
  
  3 -&amp;gt; 5
&lt;/h2&gt;
&lt;h2&gt;
  
  
  5 -&amp;gt; 6
&lt;/h2&gt;
&lt;h2&gt;
  
  
  5 -&amp;gt; 7
&lt;/h2&gt;
&lt;/blockquote&gt;




&lt;p&gt;The reasoning for that order of searching can be found within the algorithm's name: &lt;em&gt;BREADTH&lt;/em&gt;-first. Basically that means for each vertex, the search is going to visit its closest neighbors first, and when that initial vertex doesn't have any unvisited neighbors left, the search will continue on with one of the closest neighbors to the previous vertex. That pattern continues on until we reach the ending point (in this case, &lt;strong&gt;7&lt;/strong&gt;).&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1dla4wilmq60yqzttxub.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1dla4wilmq60yqzttxub.JPG" alt="BFS Intuition 2/2" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Once we've visited all vertices, the BFS will be able to tell the shortest path to the ending point, as denoted by the yellow line that's illuminating the path. In fact, once we've reached the end-point from the initial search, the BFS will be able to automatically know what that shortest path is! &lt;/p&gt;

&lt;p&gt;That path for this example is: &lt;/p&gt;




&lt;blockquote&gt;
&lt;h2&gt;
  
  
  0 -&amp;gt; 1 -&amp;gt; 3 -&amp;gt; 5 -&amp;gt; 7
&lt;/h2&gt;
&lt;/blockquote&gt;




&lt;p&gt;A total of &lt;strong&gt;4&lt;/strong&gt; steps to go from the starting to the end-point. It's also important to note that the order in which the BFS makes its traversal does &lt;em&gt;not&lt;/em&gt; matter. So in the previous initial traversal, &lt;strong&gt;0&lt;/strong&gt; could have gone to &lt;strong&gt;2&lt;/strong&gt; first, instead of &lt;strong&gt;1&lt;/strong&gt;, and that would still be a valid BFS.&lt;/p&gt;

&lt;p&gt;Now that we have a general idea of how a BFS operates, let's explore some of the implementations of doing so.&lt;/p&gt;




&lt;p&gt;The bare-bones, most basic components of a BFS are as follows:&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Initialize a new array that will keep track of &lt;strong&gt;&lt;em&gt;visited&lt;/em&gt;&lt;/strong&gt; vertices. All vertices will be unvisited upon the initial pass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Nearly all BFS make use of a &lt;em&gt;queue&lt;/em&gt; in some shape or form. Remember that queues operate on a "first-in, first-out" basis. This queue will be used to keep track of all the &lt;strong&gt;&lt;em&gt;unvisited&lt;/em&gt;&lt;/strong&gt; vertices, to indicate that we still need to visit them. At the end of the day though, this queue is just another array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; A while loop is generally used as the "engine" to keep the BFS train rolling. This while loop will keep going as long as the queue in step 2 still has &lt;em&gt;unvisited&lt;/em&gt; vertices in it. If there &lt;em&gt;are&lt;/em&gt; unvisited vertices in the queue, we'll need to remove the first vertex in the queue and mark it as being visited, which is done by populating our &lt;em&gt;visited&lt;/em&gt; array that was mentioned in step 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Once we've marked a vertex as visited, we'll need to implement another iterative method (usually a &lt;code&gt;for&lt;/code&gt; loop) to iterate over &lt;em&gt;all&lt;/em&gt; of the neighbors for the vertex we just marked as visited. During the iteration, we'll add those neighbors into the queue as being unmarked.&lt;/p&gt;




&lt;p&gt;To reinforce all of that &lt;em&gt;terribly&lt;/em&gt; confusing theory, watch this section on Reducible's &lt;a href="https://youtu.be/xlVX7dXLS64?si=Sq8V6k9ek-RwIgRu&amp;amp;t=159" rel="noopener noreferrer"&gt;video&lt;/a&gt; about BFS. While the code for it is pretty high-level, they do a great job showing how all of this plays out.&lt;/p&gt;

&lt;p&gt;Another resource I &lt;strong&gt;greatly&lt;/strong&gt; recommend checking out is this site called &lt;a href="https://visualgo.net/en/sssp?slide=6" rel="noopener noreferrer"&gt;Visualgo&lt;/a&gt;. That link goes to the BFS subsection of a larger instruction on graph-traversal methods. Click on the green highlighted text that says &lt;code&gt;BFS(5)&lt;/code&gt;. On the bottom right, you can see the actual code implementation (which is in Java 🤮 but you can get a good idea of what's happening). You can also pause the auto-play feature and click step by step on what's happening (it goes way too fast for me, personally).&lt;/p&gt;




&lt;h2&gt;
  
  
  SO
&lt;/h2&gt;

&lt;p&gt;with all of this theory and not enough JavaScript code, you're probably wondering if you just wasted all this time, only to come away empty-handed, huh? Well fear not, I came across a pretty incredible solution to solving this algorithm &lt;strong&gt;AND&lt;/strong&gt; it isn't a wall of code like you've probably seen before on solving these types of problems! I mean, just look at this:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38zjtr4q948uxpwq5xa3.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38zjtr4q948uxpwq5xa3.JPG" alt="incredibly long snippet of code" width="760" height="1280"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgybj80jaac4i59djyii.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgybj80jaac4i59djyii.gif" alt="eyes burning" width="360" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;W&lt;/strong&gt;hile I do greatly appreciate the theory behind such approaches, it just doesn't seem intuitive to me to try and grasp all at once. &lt;/p&gt;

&lt;p&gt;To illustrate a BFS with a concrete example, I'll be using LeetCode's &lt;a href="https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/description/" rel="noopener noreferrer"&gt;Nearest Exit from Entrance in Maze&lt;/a&gt; challenge. This solution was &lt;strong&gt;NOT&lt;/strong&gt; my own work, just to clarify. This incredibly elegant solution was done by &lt;a href="https://www.linkedin.com/in/maximzorin/" rel="noopener noreferrer"&gt;Maxim&lt;/a&gt; and it is a thing of beauty. And without further ado, here is the solution: &lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** 
 * @param {string[][]} maze
 * @param {number[]} entrance
 * @return {number}
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nearestExit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;y0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;y0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;y0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nx"&gt;y0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ny&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;dy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;nx&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; 

            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;nx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
                &lt;span class="nx"&gt;maze&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;nx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;There are some differences/quirks here from the basic approach that was mentioned earlier in this article, but the basic building blocks are there. I don't want to drag this article on too much longer, as there's already a bit much in here, but if you'd like me to go over what exactly this code is doing, drop a comment below and I'll get to it!&lt;/p&gt;




&lt;p&gt;As always, thanks for sticking around and I'll see ya in the next one &lt;/p&gt;

&lt;p&gt;~ ~ &amp;lt;3&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bfs</category>
      <category>graphtraversal</category>
      <category>nearestexit</category>
    </item>
    <item>
      <title>The New CSS Color Format You Didn't Know You Needed; OKLCH()</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Wed, 24 Jan 2024 17:48:34 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/the-new-css-color-format-you-didnt-know-you-needed-oklch-10hf</link>
      <guid>https://dev.to/greenteaisgreat/the-new-css-color-format-you-didnt-know-you-needed-oklch-10hf</guid>
      <description>&lt;p&gt;&lt;strong&gt;T&lt;/strong&gt;he other day, I was on &lt;a href="https://uicolors.app/create" rel="noopener noreferrer"&gt;uicolors&lt;/a&gt; trying to find a suitable color scheme to base a current project's theme off of. The client requested a muted green as the primary color and so I derived that using a dropper tool to extract a hexadecimal code. I plugged that hex code into &lt;a href="https://uicolors.app/create" rel="noopener noreferrer"&gt;uicolors&lt;/a&gt; (srsly, that site is great) and got my palette. I then went to export the necessary code to plug in to my tailwind config file when I noticed this abomination of a format: &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmiv3ak8l4028g9bih3zj.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmiv3ak8l4028g9bih3zj.JPG" alt="OKLCH syntax I came across that blew my mind at first glance" width="774" height="421"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;WHAT IN THE HECK IS THAT???&lt;/strong&gt;
&lt;/h2&gt;




&lt;p&gt;Needless to say, I had to drop everything I was doing and hyperfixate on this new color format I'd never heard of and/or confirm to myself I wasn't having an existential crisis due to not being aware of OKLCH's long and storied existence (spoiler: it became accepted on all browsers in September 2023 and was only &lt;a href="https://www.youtube.com/watch?v=dOsp6u4bIwI&amp;amp;t=241s" rel="noopener noreferrer"&gt;proposed&lt;/a&gt; to &lt;a href="https://www.w3.org/about/" rel="noopener noreferrer"&gt;W3C&lt;/a&gt; back in 2021).&lt;/p&gt;




&lt;p&gt;Now that my mental crises have been averted, what even is &lt;code&gt;oklch()&lt;/code&gt; and why does it exist? We already have so many color formats in CSS and this just seems like yet another thing I need to know about for some ridiculous and arbitrary reason.&lt;/p&gt;

&lt;p&gt;I mean, we already have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hexadecimal &lt;code&gt;#663399&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;RGB/a : &lt;code&gt;rgba(102, 51, 153, 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HSL/a : &lt;code&gt;hsla(270, 50%, 40%, 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HSV : &lt;code&gt;hsv(270, 67%, 60%)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HWB : &lt;code&gt;hwb(270, 20%, 40%)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Many More I'm Forgetting (color keywords don't count)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;(All of the above color codes are &lt;a href="https://medium.com/@valgaze/the-hidden-purple-memorial-in-your-web-browser-7d84813bb416" rel="noopener noreferrer"&gt;rebeccapurple&lt;/a&gt;; if you feel like a good cry today, read that link)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So &lt;strong&gt;WHY&lt;/strong&gt; do we need any more color formats to confuse us with?? That exact question was the second portion of my hyperfixation search, which I'm hoping to condense in easy-to-reason-about terms and save you the precious time you probably need for a myriad of other things, like, oh I don't know, living a life, spending time with loved ones, etc., ad nauseum.&lt;/p&gt;

&lt;p&gt;Let's dive into it:&lt;/p&gt;




&lt;h2&gt;
  
  
  WHY DOES &lt;code&gt;oklch()&lt;/code&gt; EVEN EXIST?
&lt;/h2&gt;




&lt;p&gt;Before we can dive into why &lt;code&gt;oklch()&lt;/code&gt; exists, we first need to talk about its ancestor, &lt;code&gt;lch()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;lch stands for: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightness&lt;/li&gt;
&lt;li&gt;Chroma&lt;/li&gt;
&lt;li&gt;Hue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Much like &lt;code&gt;hsl()&lt;/code&gt; (which stands for hue, saturation and lightness), &lt;code&gt;lch()&lt;/code&gt; allows you to configure three separate parameters to determine the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;brightness (l) &lt;/li&gt;
&lt;li&gt;intensity/saturation (c)&lt;/li&gt;
&lt;li&gt;color (h)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, there are two key differences that &lt;code&gt;lch()&lt;/code&gt; possesses that &lt;code&gt;hsl()&lt;/code&gt; does not:&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Homogeneous brightness &lt;u&gt;perception&lt;/u&gt; by a human from any given color on a screen&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;lch()&lt;/code&gt; isn't bound to any one color space&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;So what exactly do those previous two points mean?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;To address the first point, it's basically what it sounds like; when you, I or anyone else that falls under the category of being a human perceives some color on a screen that's from the Internet, there exists the possibility that two colors with the same lightness value in &lt;code&gt;hsl()&lt;/code&gt; will be perceived differently due to their intrinsic hue (or color) value.&lt;/p&gt;

&lt;p&gt;Josh Comeau relays this &lt;em&gt;perfectly&lt;/em&gt; in his blog &lt;a href="https://www.joshwcomeau.com/css/color-formats/#lch-7" rel="noopener noreferrer"&gt;post&lt;/a&gt; on css color formats. You can even see the difference between &lt;code&gt;hsl()&lt;/code&gt; and &lt;code&gt;lch()&lt;/code&gt; and how one color seems brighter than the other, even though their lightness values are identical in &lt;code&gt;hsl()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;That's&lt;/em&gt;&lt;/strong&gt; what is meant by &lt;u&gt;perception&lt;/u&gt;; the incredibly complex anatomy that our eyes have developed over eons to perceive lightness via &lt;a href="https://en.wikipedia.org/wiki/Rod_cell" rel="noopener noreferrer"&gt;rods&lt;/a&gt; and color via &lt;a href="https://en.wikipedia.org/wiki/Cone_cell" rel="noopener noreferrer"&gt;cones&lt;/a&gt; doesn't work well with the mathematical formula found in the &lt;code&gt;hsl()&lt;/code&gt; format.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;strong&gt;Wild&lt;/strong&gt; stuff, right?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;To address the second point on &lt;code&gt;lch()&lt;/code&gt; not being bound to any one color space, basically all that means is that the color it produces will appear identical, regardless of what type of screen is displaying it.&lt;/p&gt;

&lt;p&gt;The majority of today's screens use a color space known as &lt;a href="https://en.wikipedia.org/wiki/SRGB" rel="noopener noreferrer"&gt;sRGB&lt;/a&gt;. sRGB has been the standard for quite some time and for the most part, it has served its purpose well.&lt;/p&gt;

&lt;p&gt;There also exists another color space that's slowly becoming the new standard, however. That color space is called &lt;a href="https://en.wikipedia.org/wiki/DCI-P3" rel="noopener noreferrer"&gt;DCI-P3&lt;/a&gt; or simply, P3. It has its roots in cinematic displays for defining a color standard for digital movies to be displayed with.&lt;/p&gt;

&lt;p&gt;This is all to say that regardless of what type of color space your screen is using, &lt;code&gt;lch()&lt;/code&gt; will produce a perceptually identical color, no matter what.&lt;/p&gt;

&lt;p&gt;And you wanna know what's even more mind-blowing about &lt;code&gt;lch()&lt;/code&gt;? Because it isn't tied to any one color space, the upper-limit for its chroma (or saturation) value &lt;strong&gt;isn't even &lt;em&gt;known&lt;/em&gt;&lt;/strong&gt;. That ultimately amounts to &lt;em&gt;guaranteed&lt;/em&gt; color perception on virtually &lt;em&gt;ANY&lt;/em&gt; screen until we discover the limit that's intrinsic to &lt;code&gt;lch()&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;WILD&lt;/em&gt; STUFF RIGHT??&lt;/strong&gt;
&lt;/h2&gt;




&lt;p&gt;&lt;strong&gt;Okay&lt;/strong&gt;, now that we've touched on the basics of &lt;code&gt;lch()&lt;/code&gt;, why is there a need for this new, arcane color format known as &lt;code&gt;oklch()&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;The main reason is a flaw found in &lt;code&gt;lch()&lt;/code&gt; where blue appears to be more of a purple when adjusting the lightness value. You can read more about that in an article written by Vojtěch Vidra and Ondřej Pešička &lt;a href="https://atmos.style/blog/lch-vs-oklch" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Aside from that, there isn't much of a difference between the two....&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;UNLESS YOU'RE A TAILWIND USER&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;That's right, if you have yet to use Tailwind in your projects, please allow me to offer yet another compelling reason as to why you should consider it:&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;DYNAMIC COLOR THEMES&lt;/strong&gt;
&lt;/h2&gt;




&lt;p&gt;What are dynamic color themes you ask?&lt;/p&gt;

&lt;p&gt;The website Evil Martians has a great &lt;a href="https://evilmartians.com/chronicles/better-dynamic-themes-in-tailwind-with-oklch-color-magic" rel="noopener noreferrer"&gt;post&lt;/a&gt; detailing this, but ultimately, using &lt;code&gt;oklch()&lt;/code&gt; in conjunction with Tailwind allows for some pretty powerful dynamic color theme generations.&lt;/p&gt;

&lt;p&gt;One such use-case can be found in allowing end-users to define what color scheme they prefer for their application's UI, which is demonstrated beautifully by Evil Martians' &lt;a href="https://evilmartians.com/chronicles/better-dynamic-themes-in-tailwind-with-oklch-color-magic" rel="noopener noreferrer"&gt;post&lt;/a&gt; in the previous paragraph.&lt;/p&gt;

&lt;p&gt;Essentially all that's needed is an array to define the various chroma values you wish to use. Tailwind comes with out-of-the-box shade generation for any given color, so determining lightness values within an array isn't necessary. Simply choose your desired primary colors and you'll have all possible ranges of those primary colors for the user to choose from via a color slider placed in the UI.&lt;/p&gt;

&lt;p&gt;Obviously, implementing the color slider and some more intricacies are needed, but a LOT of the heavy lifting is abstracted away for you thanks to Tailwind and &lt;code&gt;oklch()&lt;/code&gt;. And to emphasize it &lt;em&gt;yet&lt;/em&gt; again, check out that &lt;a href="https://evilmartians.com/chronicles/better-dynamic-themes-in-tailwind-with-oklch-color-magic" rel="noopener noreferrer"&gt;article&lt;/a&gt; from Evil Martians if you wish to actually implement that.&lt;/p&gt;

&lt;p&gt;And to make that even more of a reality, there exists an &lt;code&gt;oklch()&lt;/code&gt; &lt;a href="https://oklch.com/" rel="noopener noreferrer"&gt;color picker&lt;/a&gt; to determine &lt;em&gt;precisely&lt;/em&gt; the values you need to generate an entire palette.&lt;/p&gt;




&lt;p&gt;And that's pretty much all I have to say on that.&lt;/p&gt;

&lt;p&gt;If you're interested in diving more into these two color formats, you can check out the CIELAB Wikipedia &lt;a href="https://en.wikipedia.org/wiki/CIELAB_color_space" rel="noopener noreferrer"&gt;page&lt;/a&gt;, which is the organization that developed &lt;code&gt;lch()&lt;/code&gt; in 1976(??), or check out &lt;a href="https://bottosson.github.io/posts/oklab/" rel="noopener noreferrer"&gt;Björn Ottosson&lt;/a&gt;, who developed the new &lt;code&gt;oklch()&lt;/code&gt; format in 2020. His proposal can be found in the recently updated &lt;a href="https://www.w3.org/TR/css-color-4/#ok-lab" rel="noopener noreferrer"&gt;CSS Color 4 module&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for sticking with me through my hyperfixations and crises, as always!&lt;/p&gt;

&lt;p&gt;&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>css</category>
      <category>tailwindcss</category>
      <category>oklch</category>
      <category>color</category>
    </item>
    <item>
      <title>Singly Linked List 'Push' Method for Dummies (me)</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Thu, 04 Jan 2024 04:31:40 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/singly-linked-list-push-method-for-dummies-me-4fa</link>
      <guid>https://dev.to/greenteaisgreat/singly-linked-list-push-method-for-dummies-me-4fa</guid>
      <description>&lt;p&gt;Whenever I come back to implementing the most basic method for singly linked lists (&lt;code&gt;push&lt;/code&gt;), I'm always scratching my head, thinking to myself, "how did I implement that again?". So here I am once again my friends, typing out concepts I don't fully understand in order to make sense of them.&lt;/p&gt;




&lt;p&gt;Linked lists at their core are simply alternatives to using a basic array. &lt;strong&gt;&lt;em&gt;Why&lt;/em&gt;&lt;/strong&gt; might we choose a linked list over an array, you ask? Let's go over the benefits:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.)&lt;/strong&gt; More efficient insertion/deletion processes (typically an &lt;strong&gt;O(1)&lt;/strong&gt; time complexity) over standard arrays&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.)&lt;/strong&gt; Useful for datasets where the exact number of items isn't known; this makes for a much more flexible data structure that can be changed more efficiently&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.)&lt;/strong&gt; Serialization (conversion from an object into bytes for transfer) is more efficient with linked lists than arrays&lt;/p&gt;




&lt;p&gt;As with all data structures however, there are downsides to using them and linked lists are no exception. When might we &lt;strong&gt;&lt;em&gt;NOT&lt;/em&gt;&lt;/strong&gt; want to use a linked list? Let's go over the drawbacks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.)&lt;/strong&gt; If your dataset requires a lot of data inquiries (look-ups), linked lists will perform worse than arrays. This is due to the fact that their location in memory (e.g., an index position) doesn't &lt;em&gt;typically&lt;/em&gt; exist (but it can; the efficiency of that look-up will still perform worse than a basic array though)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.)&lt;/strong&gt; Smaller datasets won't necessarily benefit from any of the reasons for using a linked list. It may be more useful to simply use an array if the cost/benefit in time and space complexity for insertions/deletions won't be a cause for concern&lt;/p&gt;




&lt;p&gt;&lt;u&gt;So in summary for its use-cases:&lt;/u&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linked lists are good for when you have a &lt;strong&gt;&lt;em&gt;large dataset&lt;/em&gt;&lt;/strong&gt; and it will mainly be used for &lt;strong&gt;&lt;em&gt;insertion/deletion of data&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;And for its drawbacks:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It probably won't be of much use if you have a &lt;strong&gt;&lt;em&gt;small dataset&lt;/em&gt;&lt;/strong&gt; that will mainly be used for &lt;strong&gt;&lt;em&gt;searches and data-manipulation&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Alrighty, let's dive into implementing the basic structure of a linked list and its most basic &lt;code&gt;push&lt;/code&gt; method:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rcisjzjvvsvao6bua87.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rcisjzjvvsvao6bua87.JPG" alt="JavaScript code snippet for basic linked list structure" width="231" height="260"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;We first have our two constructor functions that will be the backbone in building our linked list; the &lt;code&gt;LL&lt;/code&gt; will be where we have our &lt;code&gt;head&lt;/code&gt;, &lt;code&gt;tail&lt;/code&gt; and &lt;code&gt;length&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;head&lt;/code&gt; is our entry point into the linked list. All data will be passed through the head in order to populate our list. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tail&lt;/code&gt; is our terminal point for the linked list. The tail will &lt;em&gt;always&lt;/em&gt; contain the very last data point in our list. We don't necessarily need to have a tail in a singly linked list, but it does make it more efficient with finding the very end of the list.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;length&lt;/code&gt; property is simply what it sounds like. It'll let us know how many items are contained within the list.&lt;/p&gt;

&lt;p&gt;As for the &lt;code&gt;Node&lt;/code&gt; constructor, its sole purpose is to add onto the linked list. It will contain some data (&lt;code&gt;val&lt;/code&gt;) and a &lt;code&gt;next&lt;/code&gt; pointer that will be &lt;strong&gt;&lt;em&gt;linked&lt;/em&gt;&lt;/strong&gt; to the subsequent node in the list. The default value for &lt;code&gt;next&lt;/code&gt; will be null to accommodate any future node insertions.&lt;/p&gt;




&lt;p&gt;Now let's explore our first method: &lt;/p&gt;

&lt;h2&gt;
  
  
  PUSH
&lt;/h2&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymjudvmwr09gd33nyoff.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymjudvmwr09gd33nyoff.JPG" alt="code snippet for linked list push method" width="399" height="644"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you're seeing &lt;code&gt;LL.prototype.push&lt;/code&gt; and are thinking "&lt;em&gt;the heck is a prototype?&lt;/em&gt;", &lt;a href="https://www.youtube.com/watch?v=GewOrG12pt4" rel="noopener noreferrer"&gt;this&lt;/a&gt; is a &lt;strong&gt;great&lt;/strong&gt; resource. It's a bit lengthy, but I can almost guarantee it will cement the concept for you. &lt;/p&gt;

&lt;p&gt;And if you're wondering why I'm not using ES6+ class syntax, it's because I'm exercising my constructor/prototype muscles, as they're getting flabby.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;ANYWHO&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;What we're trying to accomplish with the &lt;code&gt;push&lt;/code&gt; method is very similar to the built-in array method by the same name in JavaScript; we simply want to add some data to the end of our list. This is first accomplished by assigning the desired piece of data (or node) to a new instance of our &lt;code&gt;Node&lt;/code&gt; constructor we defined previously:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4wjchawpufk5allrdyy.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4wjchawpufk5allrdyy.JPG" alt="node instance snippet for linked list" width="302" height="29"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, the first thing we want to check for is if we even have anything assigned to our &lt;code&gt;head&lt;/code&gt; property, which as you remember, is the entry point into our linked list. If we don't have anything assigned to &lt;code&gt;head&lt;/code&gt;, then we'll want to populate it first, as well as assigning that &lt;em&gt;&lt;strong&gt;same&lt;/strong&gt;&lt;/em&gt; piece of data to our &lt;code&gt;tail&lt;/code&gt; property:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkfw44h0w2fktkb5mar4.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkfw44h0w2fktkb5mar4.JPG" alt="conditional statement to check if our linked list's head it established" width="261" height="116"&gt;&lt;/a&gt;&lt;br&gt;
The reason we want to assign &lt;code&gt;this.tail&lt;/code&gt; to &lt;code&gt;this.head&lt;/code&gt; is because any new piece of data (&lt;code&gt;newNode&lt;/code&gt;) that we are inserting into the end of our list will need to mirror our tail. This will become more apparent once we dive further into this method. Bare with me, I know it's confusing as heck the first few (or dozen+) times you're trying to make sense of this.&lt;/p&gt;




&lt;p&gt;So now that we have our node to insert and we've checked our &lt;code&gt;head&lt;/code&gt; to see if it's empty, let's see what we do once our &lt;code&gt;head&lt;/code&gt; is actually occupied:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F074t0jehwfkbo7d0bkcs.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F074t0jehwfkbo7d0bkcs.JPG" alt="else statement to populate our list if the head is already occupied" width="281" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a duplicate assignment we're doing for two different properties. &lt;code&gt;this.tail.next&lt;/code&gt; is being populated by the &lt;code&gt;Node&lt;/code&gt; constructor with what we've assigned to our constant variable &lt;code&gt;newNode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason our &lt;code&gt;LL&lt;/code&gt; and &lt;code&gt;Node&lt;/code&gt; constructors are linked is thanks to the previous line of code we performed:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3hxpee9qkty41l2p26p.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3hxpee9qkty41l2p26p.JPG" alt="conditional statement to check if our linked list's head it established" width="261" height="116"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;code&gt;this.tail.next&lt;/code&gt; is essentially just &lt;code&gt;this.head.next&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;Read that again.&lt;/p&gt;

&lt;p&gt;And then again.&lt;/p&gt;

&lt;p&gt;And then look at the code and then read it again.&lt;/p&gt;

&lt;p&gt;⁽ˢᵉʳᶦᵒᵘˢˡʸ ᵖˡᵉᵃˢᵉ ᵈᵒ ᶦᵗ⁾&lt;/p&gt;




&lt;p&gt;That previous point is essentially the crux of how this is all working. We have our &lt;code&gt;Node&lt;/code&gt; constructor which will be assigned to the &lt;code&gt;this.head&lt;/code&gt; and &lt;code&gt;this.tail&lt;/code&gt; properties in our &lt;code&gt;LL&lt;/code&gt; constructor and all future data will be passed through both of those properties once our initial &lt;code&gt;head&lt;/code&gt; is populated. &lt;/p&gt;

&lt;p&gt;Once we've arrived to our third &lt;code&gt;push&lt;/code&gt; invocation of data insertion, we'll &lt;strong&gt;&lt;em&gt;only&lt;/em&gt;&lt;/strong&gt; be working from the tail from that point on. The head exists to initially populate our list and then act as a liaison to transfer its population onto the proceeding tail properties. &lt;/p&gt;




&lt;p&gt;Again, to reiterate, once we've populated &lt;code&gt;this.head&lt;/code&gt; and assigned &lt;code&gt;this.tail&lt;/code&gt; to &lt;code&gt;this.head&lt;/code&gt;, we're simply populating the tail and the inherent &lt;code&gt;next&lt;/code&gt; property that the tail has with any subsequent data.&lt;/p&gt;




&lt;p&gt;If that's not fully making sense, don't worry. Even upon taking my time and typing all of this out, I'm having to second-guess what I'm typing out. This are &lt;em&gt;very&lt;/em&gt; unintuitive concepts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BUT&lt;/strong&gt; if you were able to make sense of that, congratulations! Either you knew about that already or I, by some miraculous intervention, clarified that a little for you. All that's left to do is return out from the method and increment the &lt;code&gt;this.length&lt;/code&gt; property on the &lt;code&gt;LL&lt;/code&gt; constructor to signify that we've populated our list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0f3xir3q8fbp7uz8t71.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0f3xir3q8fbp7uz8t71.JPG" alt=" " width="216" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some implementations of the &lt;code&gt;push&lt;/code&gt; method have multiple sections where they increase the &lt;code&gt;this.length&lt;/code&gt; property, but I like this rendition as it adheres to the fabled &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY&lt;/a&gt; principle.&lt;/p&gt;




&lt;p&gt;And that's all I have to give, friends. You can bet your bottom dollar I'll be back with the 𝓈𝓅𝑜𝑜𝓀𝓎 &lt;code&gt;pop&lt;/code&gt; method for linked lists when I get around to it. &lt;/p&gt;

&lt;p&gt;~ Thanks for stoppin' by! ~&lt;/p&gt;

&lt;p&gt;&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>singly</category>
      <category>linkedlist</category>
      <category>push</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Make Like a Binary Search Tree and...Node?</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Wed, 27 Dec 2023 23:33:43 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/make-like-a-binary-search-tree-andnode-1fng</link>
      <guid>https://dev.to/greenteaisgreat/make-like-a-binary-search-tree-andnode-1fng</guid>
      <description>&lt;p&gt;Binary Search Trees (BSTs) have always been one of those things that seems to leave my knowledge as soon as I try and commit any possible method or structure for it to memory, hence why I'm writing this article! I'll be going over some different strategies and approaches that'll hopefully save you from that same fate I've experienced time and time again&lt;/p&gt;




&lt;p&gt;Before we dive into the code, let's first talk about the general structure of a BST with the help of this handy graphic made by the wonderful &lt;a href="https://www.freecodecamp.org/news/data-structures-101-binary-search-tree-398267b6bff0/" rel="noopener noreferrer"&gt;Kevin Turney&lt;/a&gt; in conjunction with freeCodeCamp:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1844y7ccnz8jmofn73y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1844y7ccnz8jmofn73y.jpg" alt=" " width="800" height="970"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;For those who didn't study that graphic for too long (&lt;em&gt;raises hand guiltily&lt;/em&gt;), basically all it's saying is that you start with a &lt;strong&gt;&lt;em&gt;root node&lt;/em&gt;&lt;/strong&gt; that will be the starting point for all further nodes to be placed into. Think of it like the &lt;strong&gt;&lt;em&gt;root&lt;/em&gt;&lt;/strong&gt; of a tree, which is where the source of the tree's existence is managed (see what they did there when they thought to name this concept after a tree?)&lt;/p&gt;

&lt;p&gt;The next major point in the graphic is that all nodes will have a &lt;em&gt;&lt;strong&gt;left&lt;/strong&gt;&lt;/em&gt; and a &lt;em&gt;&lt;strong&gt;right&lt;/strong&gt;&lt;/em&gt; node that its associated with. If no value is defined for either the &lt;em&gt;&lt;strong&gt;left&lt;/strong&gt;&lt;/em&gt; or the &lt;em&gt;&lt;strong&gt;right&lt;/strong&gt;&lt;/em&gt; node, it's default value will be &lt;em&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;/em&gt;. Think of &lt;strong&gt;nodes&lt;/strong&gt; as simply the next value tied to another node that will have an additional two properties on it (typically labeled &lt;em&gt;left&lt;/em&gt; and &lt;em&gt;right&lt;/em&gt;) that will accommodate the insertion of even MORE &lt;strong&gt;nodes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also, the overarching theme for BST's is that if a node's &lt;em&gt;&lt;strong&gt;value&lt;/strong&gt;&lt;/em&gt; is &lt;u&gt;less than&lt;/u&gt; the root's, it will go to the left of the root. Similarly, if a node's &lt;em&gt;&lt;strong&gt;value&lt;/strong&gt;&lt;/em&gt; is &lt;u&gt;greater than&lt;/u&gt; the root's, it will go to the right of the root&lt;/p&gt;

&lt;p&gt;So once again, if it's &lt;strong&gt;&lt;em&gt;less than&lt;/em&gt;&lt;/strong&gt; the root, to the &lt;strong&gt;&lt;em&gt;left&lt;/em&gt;&lt;/strong&gt; it goes; if it's &lt;strong&gt;&lt;em&gt;greater than&lt;/em&gt;&lt;/strong&gt; the root, to the &lt;strong&gt;&lt;em&gt;right&lt;/em&gt;&lt;/strong&gt; it goes!&lt;/p&gt;

&lt;p&gt;Good. Now we've firmly solidified that concept in our brains. Awesome job, you're doing great! (&lt;em&gt;seriously&lt;/em&gt;)&lt;/p&gt;




&lt;p&gt;So now that we've gotten the formalities and "&lt;strong&gt;&lt;em&gt;theory&lt;/em&gt;&lt;/strong&gt;" (blech) out of the way, let's start diving into some actual code! The first piece I'd like to start with is simply creating the structure in which our &lt;strong&gt;nodes&lt;/strong&gt; can be formed. You can also refer to &lt;strong&gt;nodes&lt;/strong&gt; as &lt;strong&gt;&lt;em&gt;leaves&lt;/em&gt;&lt;/strong&gt; (that's the joke I was aiming for when I titled this article.........LAUGH DANG IT)&lt;/p&gt;




&lt;p&gt;Alright, so with that, let's check out our baseline approach using ES6+ classes in JavaScript:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3r6owz5n6toau19csct.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3r6owz5n6toau19csct.JPG" alt=" " width="282" height="171"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;We have our class labeled &lt;code&gt;BinarySearchTree&lt;/code&gt; and it contains a &lt;code&gt;constructor&lt;/code&gt; with a few properties within it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;this.val&lt;/code&gt; (value)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;this.left&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;this.right&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what we've already discussed in the graphic from earlier; if no root is established when we first create our BST, &lt;code&gt;this.val&lt;/code&gt; will automatically become the root. Henceforth, that &lt;strong&gt;&lt;em&gt;root&lt;/em&gt;&lt;/strong&gt; will have two additional properties on it which will accommodate additional nodes (or leaves, heh). &lt;/p&gt;




&lt;p&gt;So that's great 'n all, but what do we do after we've already established the &lt;strong&gt;&lt;em&gt;root&lt;/em&gt;&lt;/strong&gt; and wish to adhere to the BST principle of populating our tree with additional &lt;strong&gt;&lt;em&gt;nodes&lt;/em&gt;&lt;/strong&gt; that differ in value from our &lt;strong&gt;&lt;em&gt;root&lt;/em&gt;&lt;/strong&gt;? Enter the &lt;code&gt;add()&lt;/code&gt; method:&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35feji9w82m1aqfixpb8.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35feji9w82m1aqfixpb8.JPG" alt=" " width="733" height="583"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Alright, don't click off the page, please! I know it's a lot. Trust me, I &lt;strong&gt;&lt;em&gt;know&lt;/em&gt;&lt;/strong&gt;. There's a lot happening here. Let's break this down together. &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's first focus on this part:
&lt;/h2&gt;




&lt;h2&gt;
  
  
  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxspyzc12w5preogi2lb1.JPG" alt=" " width="420" height="183"&gt;
&lt;/h2&gt;

&lt;p&gt;Essentially all that's happening is we're declaring an &lt;code&gt;add()&lt;/code&gt; method in our &lt;code&gt;BinarySearchTree&lt;/code&gt; class that'll take in whatever value (&lt;code&gt;val&lt;/code&gt;) we provide to it and then perform some action on that value. The first thing we do inside of the &lt;code&gt;add&lt;/code&gt; method is declare a constant that's assigned to a new instance of the &lt;code&gt;BinarySearchTree&lt;/code&gt; class with the passed in parameter of &lt;code&gt;val&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We then check if the current property &lt;code&gt;this.val&lt;/code&gt; has anything assigned to it OR if the value for it is equivalent to the value that has been passed in. This is a duality check in seeing if a meaningful value (i.e., not null) has been assigned to the current node or if the value being passed already exists within the tree.&lt;/p&gt;

&lt;p&gt;For the vast majority of BST's, it's generally recognized that duplicate values won't be utilized, hence the statement after the &lt;code&gt;||&lt;/code&gt; (logical OR) operator. So any values that already exist, we don't give a damn about and will want to reassign! &lt;/p&gt;




&lt;p&gt;Cool, so we've established what's happening with our first variable declaration and our first conditional statement. Now let's dive into the next section of our &lt;code&gt;add()&lt;/code&gt; method:&lt;/p&gt;




&lt;h2&gt;
  
  
  Ternary Operator Heaven
&lt;/h2&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdiypx09ugjisuk7af94.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdiypx09ugjisuk7af94.JPG" alt=" " width="710" height="177"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you're not familiar with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator" rel="noopener noreferrer"&gt;ternary operators&lt;/a&gt;, I don't blame you. They're confusing as heck the first time you come across them. The &lt;a href="https://en.wikipedia.org/wiki/Mnemonic" rel="noopener noreferrer"&gt;mnemonic&lt;/a&gt; that helped me to understand this conditional operator is whatever comes prior to the &lt;code&gt;:&lt;/code&gt; will be true. If it's not &lt;code&gt;true&lt;/code&gt;, then whatever is after the &lt;code&gt;:&lt;/code&gt; will be accepted if the condition evaluates to &lt;code&gt;false&lt;/code&gt;. The mnemonic being "Is it true? That first thing. It's not true? That second thing". &lt;/p&gt;




&lt;p&gt;Alright, so with that, let's explore what these two conditional statements are doing; the first &lt;code&gt;if&lt;/code&gt; statement is checking to see if the passed in value is less than the current node's value. If it is, then we'll enter into this statment. One of two things will happen once we enter, based off this condition:&lt;/p&gt;




&lt;h2&gt;
  
  
  Does a value exist (not null) to the left of the current node?
&lt;/h2&gt;




&lt;p&gt;1.) If not, assign &lt;code&gt;this.left&lt;/code&gt; to the passed in node&lt;br&gt;
2.) If yes, recursively enter a new &lt;a href="https://www.freecodecamp.org/news/how-javascript-works-behind-the-scene-javascript-execution-context/" rel="noopener noreferrer"&gt;execution context&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;And it's pretty much the same concept for this other piece of code:&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiwx5dd6iylzgh515dfm.JPG" alt=" " width="697" height="100"&gt;
&lt;/h2&gt;

&lt;p&gt;If the passed in value is &lt;em&gt;greater than&lt;/em&gt; the current node's value, we'll enter into it. Then we repeat that very same logic we've already discussed.&lt;/p&gt;




&lt;p&gt;And that's basically it for establishing a binary search tree and implementing an &lt;code&gt;add&lt;/code&gt; method into it that will forgo duplicate values! Ternary operations really help me with memorizing complex data structure solutions and I hope this approach gives you some value too!&lt;/p&gt;




&lt;p&gt;I have a few other methods that utilize ternary operations for &lt;code&gt;includes()&lt;/code&gt;, &lt;code&gt;min()&lt;/code&gt; and &lt;code&gt;max()&lt;/code&gt; methods. If you'd like me to elaborate more on those and even more additional BST methods, please let me know! &lt;/p&gt;




&lt;p&gt;Thank you for reading this far and I'd be honored if you gave me a follow and a reaction if you found any value from this post!&lt;/p&gt;

&lt;p&gt;&amp;lt;3&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>bst</category>
      <category>binarysearchtree</category>
      <category>javascript</category>
      <category>prototypes</category>
    </item>
    <item>
      <title>PreVue 3.0: Vue’s most visual prototyping tool yet</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sat, 02 Dec 2023 02:44:28 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/prevue-30-vues-most-visual-prototyping-tool-yet-39lh</link>
      <guid>https://dev.to/greenteaisgreat/prevue-30-vues-most-visual-prototyping-tool-yet-39lh</guid>
      <description>&lt;p&gt;As web developers, it’s crucial that our ideas be tangible and easily presentable. This makes prototyping an essential first step for bringing any web application to life.&lt;/p&gt;

&lt;p&gt;In the saturated field of front-end JavaScript frameworks available in 2023, Vue.js stands out for its simplicity and flexibility, and remains one of the top choices in the field among developers. We feel that Vue deserves a reliable prototyping and design tool that can serve as a great first step towards bringing any application to life.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is PreVue?
&lt;/h2&gt;

&lt;p&gt;PreVue first sought out to solve a common issue within the CI/CD pipeline, which were delays between designers and developers to deliver consumables within preset timelines. Even with Scrum/Agile principles in place, there exists a barrier between the UX/UI team and engineers that actually produce the design with code. In the early days of PreVue, this barrier was addressed by generating layouts for single-page applications and outputting said layouts with production-ready Vue code. However, the earlier versions of PreVue lacked a robust graphical interface to let anyone who works with it know exactly what type of HTML elements they were working with.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Enter PreVue 3.0&lt;/strong&gt; &lt;br&gt;
a prototyping tool that allows developers to visualize their project layout and component hierarchy, then export it to a code editor. For the latest version, several improvements were made to make the app a more dynamic and approachable prototyping tool.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjqfs9p4y9mhxu7a0k5ja.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjqfs9p4y9mhxu7a0k5ja.gif" alt=" " width="600" height="347"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Creating a component, filling it with html elements&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What’s New in 3.0?
&lt;/h2&gt;




&lt;p&gt;&lt;strong&gt;Element Display&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PreVue now renders draggable, resizable HTML elements on each component, so Vue devs can conceptualize their projects more clearly. Previously, elements could be added, re-arranged and nested inside components, but they would not display on screen.&lt;/p&gt;

&lt;p&gt;With these changes, PreVue feels like a true design tool, removing the abstraction behind these elements and making it easy to understand their utility. This makes PreVue more useful for presenting and pitching their ideas to clients, stakeholders, or other devs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Cleaner Look&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given the aforementioned improvements to PreVue as a design tool, we felt it was important that the interface look more inviting, light, and minimalist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UI Improvements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ve streamlined state management to ensure all changes made to a user’s project, including the arrangement of components and nesting of elements, remain after a page refresh. Users can also undo actions, allowing for a more user friendly experience.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rc0moohlu94erdrf6h5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rc0moohlu94erdrf6h5.gif" alt=" " width="600" height="339"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Saving, opening and exporting project into a code editor&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A great first step for your next project
&lt;/h2&gt;

&lt;p&gt;PreVue allows users to customize their component tree, visualize their application’s layout, then bundle their project (Vite is tooled into the app) and immediately begin working in their code editor.&lt;/p&gt;




&lt;p&gt;Check us out on &lt;a href="https://github.com/oslabs-beta/PreVue3.0" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;PreVue 3.0 was crafted/nurtured/developed by these fine folks:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;April Sanders: &lt;a href="https://github.com/algorithmrhythm" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/aprilshorttsanders/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ilay Eskinazi: &lt;a href="https://github.com/Pixolino" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/ilay-eskinazi/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cole Jaeger: &lt;a href="https://github.com/colejaeger0" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/cole-jaeger-861154122/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nathan Bornstein: &lt;a href="https://github.com/greenteaisgreat" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/nathangbornstein/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vue</category>
      <category>prototyping</category>
      <category>design</category>
      <category>ui</category>
    </item>
    <item>
      <title>How to Solve the Dreaded twoSum Algorithm WITHOUT Nested For Loops!</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sun, 16 Jul 2023 23:15:03 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/how-to-solve-the-dreaded-twosum-algorithm-without-nested-for-loops-1h5i</link>
      <guid>https://dev.to/greenteaisgreat/how-to-solve-the-dreaded-twosum-algorithm-without-nested-for-loops-1h5i</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;If you're looking for a quick solution on how this works, let me give you the gist of this absolute novel I've written on this topic. However, if you wish to dive super deep into the reasoning of how this works, I invite you to read the entire article. Here's the short and sweet of it:&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const twoSum = (array, sum) =&amp;gt; {
   //declare a variable, 'numObj', and assign it to an empty object
   const numObj = {};

   //iterate over 'array' using a for...in loop
   for (const index in array) {

    //declare a variable, 'targetSum', that will be assigned 
     //to the difference between 'sum' and the current element
     let targetSum = sum - array[index];

    //check to see if 'numObj' contains 'targetSum'; 
     //if it does, return true
     if (numObj[targetSum]) return true;

    //otherwise, populate 'numObj' with the current element 
     //as the key and it's index as the value 
     numObj[array[index]] = index
   }
  //if no matches are discovered, return false
   return false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Basically all this is doing is creating an object to store all of the elements from &lt;code&gt;array&lt;/code&gt; into, with the key being the actual element value and the property's value being the index of the element. &lt;/p&gt;

&lt;p&gt;We determine &lt;code&gt;targetSum&lt;/code&gt; as the other value we need to sum with the current element to equal the value of &lt;code&gt;sum&lt;/code&gt;. If our object doesn't contain that &lt;code&gt;targetSum&lt;/code&gt; key, we cache that current element into &lt;code&gt;numObj&lt;/code&gt; and continue on until we find an element that can be summed with &lt;code&gt;targetSum&lt;/code&gt; to produce a &lt;code&gt;true&lt;/code&gt; return. Othwerwise, we just return &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If that doesn't make any sense at all, please keep reading! &amp;lt;3&lt;/p&gt;




&lt;p&gt;As of recently, I discovered that within most technical interview settings, there exists an algorithm challenge 'twoSum' that is quite prevalent for those who have had the unfortunate experience of being on the scrutinizing end of said interview (luckily for me, I'm still at the script kiddie portion of my journey and haven't gone through one yet).&lt;/p&gt;

&lt;p&gt;Regardless, as I started to work my way through this challenge and discovered the multitude of ways to solve this problem, I realized that the "brute-force" method of using nested for-loops isn't exactly ideal when you have thousands, or even hundreds of thousands of elements to deal with!&lt;/p&gt;




&lt;p&gt;Let's take a look at the brute-force method of using nested for-loops, just for posterity's sake:&lt;/p&gt;




&lt;h2&gt;
  
  
  Brute Force Method
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const bruteForceTwoSum = (arr, targetSum) =&amp;gt; {

//declare an array to hold target elements subarray
    const targetElements = [];

//iterate over 'arr' on the current element
  for (let i = 0; i &amp;lt; arr.length; i++) {

//iterate over 'arr' on the current + 1 element
    for (let j = i + 1; j &amp;lt; arr.length; j++) {

//if the sum of the outer and inner element is equal
 //to 'targetSum', push those elements to 'targetElements'
    if (arr[i] + arr[j] === targetSum) {
    targetElements.push([arr[i], arr[j]])
      }
    }
//return the entire array of subarrays when finished iterating
  } return targetElements
}

const myArr = [1, 2, 3, 4, 5, 6];

//should return [ [1, 4], [2, 3] ]
console.log(bruteForceTwoSum(myArr, 5))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;While this method is appealing, easy to reason about and doesn't take much of a concerted effort to commit to memory after you spend some time on it, the time/space complexity gets pretty unruly once you have substantial datasets to manipulate. Not that I know anything about that, but that's what I hear everyone else say, so 🤷‍♂️&lt;/p&gt;

&lt;p&gt;After I began researching other ways to solve this, sans the nested for-loop method, I quickly discovered that a lot of people have strong ideas on how to do this; implementations of the Map() object, use of the double bitwise NOT operator (&lt;code&gt;~~&lt;/code&gt;), and so on and so forth, I thought there has to be a better way for a n00b such as myself to comprehend! And I did find it! &lt;/p&gt;




&lt;h2&gt;
  
  
  Enter Object Implementation
&lt;/h2&gt;




&lt;p&gt;So let me just say that this took quite a bit of mental strain for me to comprehend, so don't feel discouraged at all if that same thing happens to you. That just means you're actively processing new, foreign information and that is a net-gain in the overall spectrum of things! &lt;/p&gt;

&lt;p&gt;I'm going to do my best to clarify the pain-points I encountered when deciphering all of this, so that you'll hopefully come away from this article in having a good understanding of the basics of this approach. Let's dive into it!&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const twoSum = (array, sum) =&amp;gt; {
   //declare a variable, 'numObj', and assign it to an empty object
   const numObj = {};

   //iterate over 'array' using a for...in loop
   for (const index in array) {

    //declare a variable, 'targetSum', that will be assigned 
     //to the difference between 'sum' and the current element
     let targetSum = sum - array[index];

    //check to see if 'numObj' contains 'targetSum'; 
     //if it does, return true
     if (numObj[targetSum]) return true;

    //otherwise, populate 'numObj' with the current element 
     //as the key and it's index as the value 
     numObj[array[index]] = index
   }
  //if no matches are discovered, return false
   return false
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Let's work through exactly what is happening with this code, as it was pretty difficult for me to comprehend the first time I saw it. &lt;/p&gt;

&lt;p&gt;So we have a pretty basic setup; we're declaring a function &lt;code&gt;twoSum&lt;/code&gt; that has two parameters: an &lt;code&gt;array&lt;/code&gt; and the &lt;code&gt;sum&lt;/code&gt; that we want to look for in our pairs of elements&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const twoSum = (array, sum) =&amp;gt; {
   //declare a variable, 'numObj', and assign it to an empty object
   const numObj = {};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Once we enter our function, we're then declaring a variable, &lt;code&gt;numObj&lt;/code&gt; and assigning it to an empty object. Here's where things get interesting! &lt;/p&gt;




&lt;p&gt;The reason why we want to declare an object is so we can store all elements in our array, &lt;code&gt;array&lt;/code&gt;, inside of 'numObj'. We store each property in &lt;code&gt;numObj&lt;/code&gt; with the current element as the key and the current element's index as the value! This will make it easy for us to determine both the element's value, as well as the element's index, if any tricky interviewer asks for it 💅&lt;/p&gt;

&lt;p&gt;Let's move on to the next chunk of code:&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const twoSum = (array, sum) =&amp;gt; {
   //declare a variable, 'numObj', and assign it to an empty object
   const numObj = {};

   //iterate over 'array' using a for...in loop
   for (const index in array) {

    //declare a variable, 'targetSum', that will be assigned 
     //to the difference between 'sum' and the current element
     let targetSum = sum - array[index];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;One thing that threw me for a &lt;strong&gt;LOOP&lt;/strong&gt; (get it?) when I first saw this implementation is the use of a &lt;code&gt;for...in&lt;/code&gt; loop when iterating over an array. &lt;/p&gt;

&lt;p&gt;I thought that broke every law in the programmer's handbook, was anathema to what we all are striving for and whoever wrote this was going to be confined to the deepest layer of programmatic hell, as you only used &lt;code&gt;for...in&lt;/code&gt; loops with objects right?? &lt;/p&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

&lt;p&gt;For reasons I won't go into right now, &lt;strong&gt;this&lt;/strong&gt; specific problem is much better handled using a &lt;code&gt;for...in&lt;/code&gt; loop. Just know that for now, &lt;code&gt;for (const index in array)&lt;/code&gt; does exactly what it says it will do; it's gonna loop over our array, where &lt;code&gt;index&lt;/code&gt; will be the current element's index value and &lt;code&gt;array&lt;/code&gt; is going to be the array we're iterating &lt;/p&gt;

&lt;p&gt;&lt;code&gt;let targetSum = sum - array[index]&lt;/code&gt; is going to determine what other number in &lt;code&gt;array&lt;/code&gt; is going to satisfy the condition of being equal to &lt;code&gt;sum&lt;/code&gt;, when added to the current element, which is &lt;code&gt;array[index]&lt;/code&gt; in this case. &lt;/p&gt;

&lt;p&gt;Basically, this line of code is saying "For this current element (&lt;code&gt;array[index]&lt;/code&gt;) we need to find another number (&lt;code&gt;targetSum&lt;/code&gt;) that when added together (&lt;code&gt;array[index] + targetSum&lt;/code&gt;) will produce the same value as &lt;code&gt;sum&lt;/code&gt;. &lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    if (numObj[targetSum]) return true;
    //otherwise, populate 'numObj' with the current element 
    //as the key and it's index as the value 
     numObj[array[index]] = index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;For this section, we're checking &lt;code&gt;numObj&lt;/code&gt; to see if we've cached any elements from &lt;code&gt;array&lt;/code&gt; into &lt;code&gt;numObj&lt;/code&gt;, &lt;strong&gt;HOWEVER&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Notice that we're checking &lt;code&gt;numObj&lt;/code&gt; for &lt;code&gt;targetSum&lt;/code&gt;. Basically what this means is that for the current element, do we have any keys in &lt;code&gt;numObj&lt;/code&gt;, when added to the current element, will produce a sum that is equal to &lt;code&gt;sum&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;If it does, we're going to return &lt;code&gt;true&lt;/code&gt;. Otherwise, we want to cache that current element into &lt;code&gt;numObj&lt;/code&gt;, so we can reference later when checking other elements in &lt;code&gt;array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And finally...&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  //if no matches are discovered, return false
   return false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We want to return &lt;code&gt;false&lt;/code&gt; if no elements, when added together, equal the value of &lt;code&gt;sum&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Let's see this with some actual data and see what we get:&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const twoSum = (array, sum) =&amp;gt; {
   const numObj = {};

   for (const index in array) {
     let targetSum = sum - array[index];

     if (numObj[targetSum]) return true;

     numObj[array[index]] = index
   }
   return false
  }

const myArr = [1, 2, 3, 4, 5]

console.log(twoSum(myArr, 6)) //should return true, as [1, 5] 
                              //is equal to 'sum', which is 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;But what if we wanted to see all possible element combinations in the array that sum up to the value of &lt;code&gt;sum&lt;/code&gt;? Objects to the rescue once again! All we do is declare yet another object to hold properties where the key is the first element and the value is the second element that sum up to the value of &lt;code&gt;sum&lt;/code&gt;:&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const twoSum = (array, sum) =&amp;gt; {
    const numObject = {}
    const finalObj = {};

    for (const index in array) {
    let targetSum = sum - array[index]   

     if (targetSum in numObject) {
        finalObj[targetSum] = array[index]      
        }
        numObject[array[index]] = index
      }
      //check 'finalObj' to see if we've retrieved any pairs
       //whose sum equals 'sum'
      if (Object.entries(finalObj).length !== 0) return finalObj;

      else return 'no pairs found';
   }

const myArr = [1, 2, 3, 4, 5]

console.log(twoSum(myArr, 6)) //should return {1:5, 2:4}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;If you've made it this far, wHAT are you doing with your life?? Don't you have one?? :P I am absolutely joking and I thank you very much for your time.&lt;/p&gt;

&lt;p&gt;Hopefully this helped to clear up some understanding on how all of this works! If for nothing else, it helped me to solidify this in my brain by spending literal hours on typing this out. &lt;/p&gt;

&lt;p&gt;Cheers! &amp;lt;3&lt;/p&gt;

</description>
      <category>twosum</category>
      <category>javascript</category>
      <category>interview</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Invoking multiple functions on a single element</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sun, 09 Apr 2023 22:02:22 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/invoking-multiple-functions-on-a-single-element-5845</link>
      <guid>https://dev.to/greenteaisgreat/invoking-multiple-functions-on-a-single-element-5845</guid>
      <description>&lt;p&gt;In this post, I'll be going over a relatively simple way of invoking multiple functions upon a single element. Both the elements and the functions will be relegated to their own respective arrays. We'll also be populating an empty object with the elements as the keys and the values as an array of the invoked functions on each element. Let's gooooo!&lt;/p&gt;




&lt;p&gt;So here's what we'll be working with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const uppercaser = str =&amp;gt; str.toUpperCase();

const capitalize = str =&amp;gt; str[0].toUpperCase() + str.slice(1);

const repeater = str =&amp;gt; str + str;

const items = ['catfood', 'glue', 'beer'];

const functions = [uppercaser, capitalize, repeater];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We have three functions, assigned to the &lt;code&gt;functions&lt;/code&gt; array and three string elements assigned to the &lt;code&gt;items&lt;/code&gt; array. Our goal is to populate an empty object with the elements in &lt;code&gt;items&lt;/code&gt; as the keys and we want to invoke every function from &lt;code&gt;functions&lt;/code&gt; upon each one of the elements in &lt;code&gt;items&lt;/code&gt;. So basically, apply those three functions on each element from &lt;code&gt;items&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's declare our function that takes in both of the arrays and sets up an empty object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiMap = (items, functions) =&amp;gt; {
  const obj = {};
}

const uppercaser = str =&amp;gt; str.toUpperCase();

const capitalize = str =&amp;gt; str[0].toUpperCase() + str.slice(1);

const repeater = str =&amp;gt; str + str;

const items = ['catfood', 'glue', 'beer'];

const functions = [uppercaser, capitalize, repeater];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're going to use the &lt;code&gt;.forEach()&lt;/code&gt; method on the &lt;code&gt;items&lt;/code&gt; parameter/argument to loop through each element within that array, but here's where it gets interesting:&lt;/p&gt;




&lt;p&gt;Once we call the &lt;code&gt;.forEach()&lt;/code&gt; method on that array, we're going to simultaneously populate our empty object &lt;code&gt;obj&lt;/code&gt; with each element from &lt;code&gt;items&lt;/code&gt; as the &lt;strong&gt;key&lt;/strong&gt; while also assigning the &lt;strong&gt;value&lt;/strong&gt; for &lt;code&gt;obj&lt;/code&gt; with every single function that's inside of the &lt;code&gt;functions&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiMap = (items, functions) =&amp;gt; {
  const obj = {};
  items.forEach(item =&amp;gt; {
    obj[item] = functions.map(callback =&amp;gt; callback(item))
  }); return obj;
}

const uppercaser = str =&amp;gt; str.toUpperCase();

const capitalize = str =&amp;gt; str[0].toUpperCase() + str.slice(1);

const repeater = str =&amp;gt; str + str;

const items = ['catfood', 'glue', 'beer'];

const functions = [uppercaser, capitalize, repeater];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We end the function by returning &lt;code&gt;obj&lt;/code&gt; at the end of the &lt;code&gt;.forEach()&lt;/code&gt; method. Our output will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ 
catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], 
glue: ['GLUE', 'Glue', 'glueglue'], 
beer: ['BEER', 'Beer', 'beerbeer'] 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;The way this works is that &lt;code&gt;.forEach()&lt;/code&gt; will loop through every element in the &lt;code&gt;items&lt;/code&gt; array and then we're populating &lt;code&gt;obj&lt;/code&gt; with each one of those elements in &lt;code&gt;items&lt;/code&gt; as the key. The &lt;code&gt;.map()&lt;/code&gt; method will then apply every function in &lt;code&gt;functions&lt;/code&gt; to each element in &lt;code&gt;items&lt;/code&gt; and return every value from those three functions in an array, since &lt;code&gt;.map()&lt;/code&gt; returns a new array.&lt;/p&gt;




&lt;p&gt;I hope this was helpful for you! Keep fighting the good fight!&lt;/p&gt;

&lt;p&gt;&amp;lt;3&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callback</category>
      <category>foreach</category>
      <category>map</category>
    </item>
    <item>
      <title>PSA For Referencing Properties in Functions!</title>
      <dc:creator>Nathan G Bornstein</dc:creator>
      <pubDate>Sat, 04 Mar 2023 02:33:49 +0000</pubDate>
      <link>https://dev.to/greenteaisgreat/psa-for-referencing-properties-in-functions-2810</link>
      <guid>https://dev.to/greenteaisgreat/psa-for-referencing-properties-in-functions-2810</guid>
      <description>&lt;p&gt;So I was going through freeCodeCamp's exercise "Profile Lookup" for the 3rd-ish time and there's always something about that exercise that puts me back in my place, when I'm feeling pretty good about myself, as far as my newfound coding skills are concerned. But it's a good thing I've gone through it that many times, as this concept needs to get cemented into my psyche, hence why I'm spewing this out into the world for all to see:&lt;/p&gt;




&lt;h2&gt;
  
  
  BE SURE TO USE BRACKET NOTATION WHEN REFERENCING OBJECT PROPERTY VARIABLES IN A LOOKUP FUNCTION
&lt;/h2&gt;




&lt;p&gt;Man, that felt &lt;em&gt;good&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If you're confused as to what exactly the heck I'm talking about, don't worry, I am too. Let me demonstrate exactly what I mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const contacts = [
 {
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  }
]

function lookUpProfile(name, prop) {

}

//I've shortened the amount of objects from the original exercise for brevity, just FYI.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FCC has an exercise where you need to create a function to look up whether an object in the array &lt;code&gt;contacts&lt;/code&gt; contains a specific &lt;code&gt;name&lt;/code&gt; and/or a specific &lt;code&gt;prop&lt;/code&gt; (property). The most basic way to solve this is to use a &lt;code&gt;for&lt;/code&gt; loop and iterate through each of the objects to see if one of the object's properties contains that person's first name and then see if there exists that property. Here's how I first &lt;em&gt;attempted&lt;/em&gt; to solve it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function lookUpProfile(name, prop) {

for (let i = 0; i &amp;lt; contacts.length; i++) {
  if (name === contacts[i].firstName) {

    return contacts[i].prop || 'No such property'
  }

 } return 'No such contact'
}

lookUpProfile("Akira", "likes");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I was using a &lt;code&gt;for&lt;/code&gt; loop to sort through the array's objects and implementing an &lt;code&gt;if&lt;/code&gt; statement to catch whether or not a given name was located in the object. By saying &lt;code&gt;name === contacts[i].firstName&lt;/code&gt;, that &lt;em&gt;should&lt;/em&gt; be able to catch what I was looking for. And it did. However, the next line is where I went wrong:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return contacts[i].prop || 'No such property'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based off the previous line of code from this one, I thought "oh, so I can just type out something similar to that format and it should return the &lt;code&gt;prop&lt;/code&gt; value if it's there, and if it's not, it'll return 'No such property'. Simple, right? WRONG.&lt;/p&gt;

&lt;p&gt;I made a dreadful error in thinking I could use dot notation to catch whatever given property was passed into the &lt;code&gt;prop&lt;/code&gt; parameter of the function. Take a look at how they're passing in the arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lookUpProfile("Akira", "likes");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see that? They're passing in &lt;em&gt;strings&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;When you pass in a string to a function's argument and expect dot notation to work within the function's statements, it's going to return this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return contacts[0].'likes' || 'No such property'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There isn't any way an object's property can be in string format when using dot notation. You HAVE to use bracket notation in order for that to work. Here's what the correct code needs to look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function lookUpProfile(name, prop) {

for (let i = 0; i &amp;lt; contacts.length; i++) {
  if (name === contacts[i].firstName) {

    return contacts[i][prop] || 'No such property'
  }

 } return 'No such contact'
}

lookUpProfile("Akira", "likes");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything is smooth sailing! Some of you may be thinking "duh, of course that didn't work, you heckin' n00b" and you're 100% correct, but I'm trYING MY BEST OKAY?? 😅&lt;/p&gt;

&lt;p&gt;Anywho, I'll get off my soapbox now. I hope this helps out at least one person and if not, at least it helped to solidify this concept into my brain by typing it all out.&lt;/p&gt;

&lt;p&gt;I wish thee well on your coding journey, wherever you may be. Never give up!&lt;/p&gt;

&lt;p&gt;&amp;lt;3&amp;lt;3&amp;lt;3&lt;/p&gt;

</description>
      <category>freecodecamp</category>
      <category>javascript</category>
      <category>profilelookup</category>
      <category>function</category>
    </item>
  </channel>
</rss>
