<?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>Forem: Fernando Larrañaga</title>
    <description>The latest articles on Forem by Fernando Larrañaga (@xabadu).</description>
    <link>https://forem.com/xabadu</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%2F155045%2F727a6a2f-8981-4098-9f43-53fb4e5abd84.jpeg</url>
      <title>Forem: Fernando Larrañaga</title>
      <link>https://forem.com/xabadu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xabadu"/>
    <language>en</language>
    <item>
      <title>Data Structures with JavaScript: Queues</title>
      <dc:creator>Fernando Larrañaga</dc:creator>
      <pubDate>Mon, 06 Jan 2020 18:17:07 +0000</pubDate>
      <link>https://forem.com/xabadu/data-structures-with-javascript-queues-46jm</link>
      <guid>https://forem.com/xabadu/data-structures-with-javascript-queues-46jm</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover image by &lt;a href="https://unsplash.com/@florenciaviadana?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Florencia Viadana&lt;/a&gt; on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;¿Español? Puedes encontrar la versión traducida de este artículo aquí: &lt;a href="https://medium.com/noders/estructuras-de-datos-con-javascript-parte-2-colas-queues-dbd86125d3d5" rel="noopener noreferrer"&gt;Estructuras de datos con JavaScript — Parte 2: Colas (Queues)&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a series about data structures in JavaScript. The previous one can be found here:&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/xabadu" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F155045%2F727a6a2f-8981-4098-9f43-53fb4e5abd84.jpeg" alt="xabadu"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/xabadu/data-structures-with-javascript-stacks-56ej" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Data Structures with JavaScript: Stacks&lt;/h2&gt;
      &lt;h3&gt;Fernando Larrañaga ・ Sep 30 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#computerscience&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Continuing this &lt;em&gt;(not so)&lt;/em&gt; magical adventure with data structures in JavaScript, it is now time to talk about queues!&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a queue?
&lt;/h3&gt;

&lt;p&gt;A queue is a data structure very similar to a stack, meaning that it also operates in a linear and unidirectional way &lt;em&gt;(elements are added from beginning to end)&lt;/em&gt;. The main difference is in the way that elements are extracted from it. While a stack uses the &lt;strong&gt;LIFO&lt;/strong&gt; (&lt;strong&gt;L&lt;/strong&gt;ast &lt;strong&gt;I&lt;/strong&gt;n &lt;strong&gt;F&lt;/strong&gt;irst &lt;strong&gt;O&lt;/strong&gt;ut) principle, a queue uses the FIFO (&lt;strong&gt;F&lt;/strong&gt;irst &lt;strong&gt;I&lt;/strong&gt;n &lt;strong&gt;F&lt;/strong&gt;irst &lt;strong&gt;O&lt;/strong&gt;ut) principle instead. That means that the first element added to the queue will be the first one that will be extracted.&lt;/p&gt;

&lt;p&gt;A common example to understand the behavior of a queue would be the line that we have to stand in when we go to the bank or a grocery store for checkout. In these lines, the person that is at the front of the line will be the first one to receive service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F945bbwxi3reu38wdgx1c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F945bbwxi3reu38wdgx1c.png" alt="graphic representation of a queue"&gt;&lt;/a&gt;&lt;/p&gt;
graphic representation of a queue (sort of)



&lt;h4&gt;
  
  
  When to use a queue
&lt;/h4&gt;

&lt;p&gt;Two of the most common uses for queues are present in tools and processes we probably use on a daily basis: Job queues, such as &lt;a href="https://github.com/resque/resque" rel="noopener noreferrer"&gt;Resque&lt;/a&gt;, &lt;a href="https://github.com/mperham/sidekiq" rel="noopener noreferrer"&gt;Sidekiq&lt;/a&gt; or &lt;a href="https://github.com/Automattic/kue" rel="noopener noreferrer"&gt;Kue&lt;/a&gt;, and messaging services. In both cases, the data will be processed in a first-come-first-serve basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity of a queue
&lt;/h3&gt;

&lt;p&gt;The complexities of a queue are practically the same as the &lt;a href="https://dev.to/estructuras-de-datos-con-js-pilas"&gt;ones for stacks&lt;/a&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  Time Complexity
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Space complexity (in memory): O(n).
&lt;/h4&gt;

&lt;h3&gt;
  
  
  Methods/functionality of a queue
&lt;/h3&gt;

&lt;p&gt;A well-implemented queue will need the provide methods that allow adding, remove and show elements, as well as return the current size of the queue. These methods are commonly named:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;enqueue:&lt;/strong&gt; Adds a new element &lt;strong&gt;at the end of the queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dequeue:&lt;/strong&gt; Returns the first element, &lt;strong&gt;removing it from the queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;peek:&lt;/strong&gt; Returns the first element, &lt;strong&gt;without removing it from the queue.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;size:&lt;/strong&gt; Returns the number of elements of the queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;print:&lt;/strong&gt; Displays the content of the queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optionally, some implementations also include a method called &lt;strong&gt;isEmpty&lt;/strong&gt;. that will return a boolean &lt;em&gt;(true/false)&lt;/em&gt; depending if the queue is empty or not. If the method is not available, we can infer such information by calling &lt;strong&gt;size&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to implement a queue
&lt;/h3&gt;

&lt;p&gt;The most direct way to implement a queue in JavaScript is by using an Array. Most of the methods we'll need are already implemented in its prototype, so we'll just have to create wrappers to return the values to the user.&lt;/p&gt;

&lt;p&gt;The only additional method that we'll have to implement manually is &lt;strong&gt;peek&lt;/strong&gt;, for which we'll return the first element of the array (of index 0).&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;class&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Initializes the queue as an empty string&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Adds a new element by using push and returns the queue&lt;/span&gt;
    &lt;span class="k"&gt;this&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;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;dequeue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Removes the first element by using shift&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Shows the element in the first position&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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="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="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Returns the size of the array using length&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Checks if there are no elements available by using length&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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="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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Displays the entire queue&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="k"&gt;this&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="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;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Rock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["The Rock"]&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Diamond Dallas Page&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["The Rock", "Diamond Dallas Page"]&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stone Cold Steve Austin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["The Rock", "Diamond Dallas Page", "Stone Cold Steve Austin"]&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dequeue&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// "The Rock"&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// "Diamond Dallas Page"&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// false&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;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ["Diamond Dallas Page", "Stone Cold Steve Austin"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Source Code
&lt;/h3&gt;

&lt;p&gt;You can find the source code of this example here:&lt;a href="https://github.com/Xabadu/js-data-structures" rel="noopener noreferrer"&gt;https://github.com/Xabadu/js-data-structures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Originally published on my blog at &lt;a href="https://xabadu.dev/js-data-structures-queues/" rel="noopener noreferrer"&gt;xabadu.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Build an SMS Panic Button with Twilio Functions</title>
      <dc:creator>Fernando Larrañaga</dc:creator>
      <pubDate>Tue, 22 Oct 2019 19:22:01 +0000</pubDate>
      <link>https://forem.com/twilio/build-an-sms-panic-button-with-twilio-functions-377g</link>
      <guid>https://forem.com/twilio/build-an-sms-panic-button-with-twilio-functions-377g</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Are you part of a nonprofit or social enterprise that wants to build things for social good? At &lt;a href="https://twilio.org"&gt;Twilio.org&lt;/a&gt;, we offer discounts and credits to help you get started! &lt;a href="https://www.twilio.org/benefits/"&gt;Learn more&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In an emergency such as an accident, an insecure situation, or a natural disaster, a successful response time is often measured in seconds.&lt;/p&gt;

&lt;p&gt;Communicating a call for help quickly is essential. If someone is unable to make a phone call or send a text message, a readily accessible panic button can enable them to make an urgent call for help discreetly and efficiently.&lt;/p&gt;

&lt;p&gt;In this guide, we'll show you how to build an SMS Panic Button in a couple of steps using an IoT button and Twilio Functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.twilio.com/try-twilio"&gt;A free Twilio account&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://flic.io/"&gt;A Flic button&lt;/a&gt; (or any other button capable of sending an HTTP request)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How will this work?
&lt;/h2&gt;

&lt;p&gt;IoT buttons, such as Flic, offer the possibility to configure and trigger different actions depending on what we do with them. In this case, we can link different outcomes to these 3 different actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single press/click &lt;em&gt;(the one we'll be using in this guide)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;A double press/click.&lt;/li&gt;
&lt;li&gt;A long press or hold.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other side, Twilio Functions allow you to send a request from any internet-connected device capable of sending outgoing HTTP requests, and from there interact with any Twilio API.&lt;/p&gt;

&lt;p&gt;With these two pieces connected, we can build a flow that whenever we do a single click, sends an SMS to a preconfigured number.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are we doing?
&lt;/h2&gt;

&lt;p&gt;The logic of this entire flow works as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We'll create a Twilio Function that has a predefined phone number and sends a message whenever an HTTP request comes in.&lt;/li&gt;
&lt;li&gt;We'll configure our button to execute an outgoing HTTP request whenever someone executes a single button click.&lt;/li&gt;
&lt;li&gt;We'll link the outgoing HTTP request to the path of the Twilio Function we created in Step 1.&lt;/li&gt;
&lt;li&gt;We'll have an epic SMS panic button ready to go!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating your Twilio Function
&lt;/h2&gt;

&lt;p&gt;Next, we’ll create a Twilio Function that receives the request after we click the Flic button and sends an SMS message to a list of numbers that we’ll define.&lt;/p&gt;

&lt;p&gt;To do this, first go to the &lt;a href="https://www.twilio.com/console/functions/manage"&gt;Manage Functions page&lt;/a&gt; in the Twilio Console, by accessing the Functions icon in the Runtime menu:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uxjAWwha--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/23bn0kmdq0bcig4d5ceo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uxjAWwha--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/23bn0kmdq0bcig4d5ceo.png" alt="Twilio Functions menu option in the console" width="339" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, create a new function, by clicking the red &lt;strong&gt;“Create new function”&lt;/strong&gt; button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sGp9GFxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/pwmk77vx38yfwr1k0kxi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sGp9GFxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/pwmk77vx38yfwr1k0kxi.png" alt="Create new function button" width="303" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;“Blank”&lt;/strong&gt; in the New Function wizard, and then click on &lt;strong&gt;“Create”&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C0BUAM0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/wuwkm9vrvu4sxho1e1iy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C0BUAM0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/wuwkm9vrvu4sxho1e1iy.png" alt="New function template wizard" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, with your new function created, you’ll need to fill these two sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function name:&lt;/strong&gt; It's a friendly name, and it can be whatever you want. In this case, we are using "SMS Panic Button" as the name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path:&lt;/strong&gt; It's the specific path for the URL associated with this function. It can also be whatever you want, but we recommend keeping it short, so it's easier to remember. In this case, we are using &lt;strong&gt;"/sms-panic-button"&lt;/strong&gt;. We are using this URL in the next section when we configure the action for the button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--10grJtaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/iv1bwxb48pd0oo8qmyym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--10grJtaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/iv1bwxb48pd0oo8qmyym.png" alt="New function properties menu" width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, add this in the &lt;strong&gt;code&lt;/strong&gt; section below:&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your-number-here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;twilioClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getTwilioClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;twilioClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one-of-your-twilio-numbers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a panic button test message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;callback&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;For this code, you need to configure two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"your-number-here"&lt;/strong&gt; &lt;em&gt;(line 1)&lt;/em&gt;: The destination number. Must be in e164 format &lt;em&gt;(e.g: +1 1234567890)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"One-of-your-twilio-numbers"&lt;/strong&gt; &lt;em&gt;(line 8)&lt;/em&gt;: Your Twilio number that sends the messages. You can get a phone number or look at your existing ones in the &lt;a href="https://www.twilio.com/console/phone-numbers/incoming"&gt;Active Numbers&lt;/a&gt; section in the Twilio console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, click &lt;strong&gt;"Save"&lt;/strong&gt; to deploy the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up your Flic button
&lt;/h2&gt;

&lt;p&gt;After getting your Flic button and going through the initial setup &lt;em&gt;(creating your account/signing in and connecting your button to the app)&lt;/em&gt;, tap your button in the main screen of the app:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EHAZxkUI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r2y4tvn2xelyyvox3ob9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EHAZxkUI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/r2y4tvn2xelyyvox3ob9.png" alt="Buttons list in the Flic button app" width="350" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the action selection screen, tap on the &lt;strong&gt;“Click”&lt;/strong&gt; option to configure what happens when we do a single click with the button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZxdOcz2z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/ieo5gm8ok0ee3odyhlcx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZxdOcz2z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/ieo5gm8ok0ee3odyhlcx.png" alt="Buttons actions list in the Flic button app" width="350" height="678"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After selecting the single-click option and going to the configure action screen, scroll all the way down and select the &lt;strong&gt;“Tools”&lt;/strong&gt; option:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xo2p5eLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/l7t0kkgvadg1nhrxir02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xo2p5eLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/l7t0kkgvadg1nhrxir02.png" alt="Action configuration screen in the Flic button app" width="350" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in the tools list, scroll down until you find the &lt;strong&gt;“Internet Request”&lt;/strong&gt; option and select it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PNiMH_Gq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/z2w5kv056q9i315qvbp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PNiMH_Gq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/z2w5kv056q9i315qvbp3.png" alt="Button actions options in the Flic button app" width="350" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you’ll need to configure two things in the &lt;strong&gt;“Internet Request”&lt;/strong&gt; details screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--46Lsd6f_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/2my1zgggsrzn8eean53g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--46Lsd6f_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/2my1zgggsrzn8eean53g.png" alt="Internet request configuration screen in the Flic button app" width="350" height="679"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Method:&lt;/strong&gt; Select POST here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;URL:&lt;/strong&gt; Here, you need to add the URL of the Twilio Function we created in the previous step.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After you fill those two options, scroll all the way down and tap the &lt;strong&gt;“Save Action”&lt;/strong&gt; button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5hDWVELr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/moqndik1c8j5270v8f8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5hDWVELr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/moqndik1c8j5270v8f8u.png" alt="Internet request save configuration button in the Flic button app" width="350" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with that, your Panic button should be all good and ready to go. Next step is to try it out and see if everything is working correctly, but before we do that, one thing to keep in mind:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  What if I want to text multiple numbers?
&lt;/h3&gt;

&lt;p&gt;If you want to add more numbers as destination and notify more people after pressing your panic button, &lt;a href="https://www.twilio.com/blog/2017/12/send-bulk-sms-twilio-node-js.html"&gt;this guide&lt;/a&gt; walks you through sending multiple SMS messages to many different numbers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, time for a demo!&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mksTVesi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/c2upck1oriv5x7q0la8w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mksTVesi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/c2upck1oriv5x7q0la8w.gif" alt="Demo of someone pressing the panic button and receiving an SMS on a phone right next to it" width="640" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have any ideas on how to extend this or make it even better? Let us know! We can't wait to see what you build!&lt;/p&gt;

&lt;p&gt;Got any questions or comments? You can find me on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Email:&lt;/strong&gt; &lt;a href="//mailto:flarranaga@twilio.com"&gt;flarranaga@twilio.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Twitter:&lt;/strong&gt; &lt;a href="https://twitter.com/xabadu"&gt;@xabadu&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/Xabadu"&gt;Xabadu&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>twilio</category>
      <category>serverless</category>
      <category>sms</category>
    </item>
    <item>
      <title>Data Structures with JavaScript: Stacks</title>
      <dc:creator>Fernando Larrañaga</dc:creator>
      <pubDate>Mon, 30 Sep 2019 16:25:40 +0000</pubDate>
      <link>https://forem.com/xabadu/data-structures-with-javascript-stacks-56ej</link>
      <guid>https://forem.com/xabadu/data-structures-with-javascript-stacks-56ej</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover image by &lt;a href="https://unsplash.com/@clemono2?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Clem Onojeghuo&lt;/a&gt; on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;¿Español? Puedes encontrar la versión traducida de este artículo aquí: &lt;a href="https://medium.com/noders/estructuras-de-datos-con-javascript-parte-1-pilas-stacks-5a2092cff16a"&gt;Estructuras de datos con JavaScript — Parte 1: Pilas (Stacks)&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Data Structures! - Now, now, before we start to panic, let's just take a deep breath and jump into this &lt;em&gt;(not so)&lt;/em&gt; magical adventure to unveil the mysteries of the commonly feared data structures in JavaScript: What are they, what are they good for and most important of all, how to implement them.&lt;/p&gt;

&lt;p&gt;In this article, we'll start with what's probably one of the most common ones: &lt;strong&gt;Stacks&lt;/strong&gt;. So where do we start? At the beginning!&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a stack?
&lt;/h3&gt;

&lt;p&gt;A stack is a structure used to store data that functions in a &lt;strong&gt;linear and unidirectional&lt;/strong&gt; way. That last part is really important because it sets the rules that we'll need to follow when working with stacks: Every element or piece of data added to a stack will be included in the same order and direction &lt;em&gt;(from start to end)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Stacks manage their data under a principle called &lt;strong&gt;LIFO&lt;/strong&gt; (&lt;strong&gt;L&lt;/strong&gt;ast &lt;strong&gt;I&lt;/strong&gt;n &lt;strong&gt;F&lt;/strong&gt;irst &lt;strong&gt;O&lt;/strong&gt;ut). This means that the last element we add will always be the first one that will be extracted from it.&lt;/p&gt;

&lt;p&gt;A commonly used analogy to describe stacks is to think of the way that plates are stored in a kitchen cabinet. Every time we go to grab a new plate, we'll always take the one that sits at the top, which coincidentally was the last one we put there. &lt;em&gt;(Sometimes we'll try to be clever and take the one in the middle, but then the plates fall, break, and we'll get in trouble&lt;/em&gt; 🤕&lt;em&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's look at a graphical representation of how a stack would work: &lt;em&gt;(we'll go through what every part of this is later)&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RzwfOsGN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p7bhlf196zad0m4e65ac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RzwfOsGN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p7bhlf196zad0m4e65ac.png" alt="A diagram of a Stack"&gt;&lt;/a&gt;&lt;/p&gt;
Looks like a stack



&lt;h4&gt;
  
  
  When to use a stack
&lt;/h4&gt;

&lt;p&gt;There are a lot of practical examples that we can find nowadays where stacks are being used. There is also a good chance that we've been using them on a daily basis without even knowing. In fact, to get to this article, we did it with one of the most used stacks that there is: &lt;strong&gt;the navigation history of our browser&lt;/strong&gt;. Every new page that we visit is stored on top of the previous one, and it creates a stack of values that allows us to go back, one by one &lt;em&gt;(with the back button)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, stacks are useful when we need a data structure to store data that will be displayed in chronological order &lt;em&gt;(such as a list of latest tweets or articles)&lt;/em&gt;. For this example, the most recent piece of data added will be the first one shown, and so on, and so on.&lt;/p&gt;

&lt;p&gt;So every time that we need to store data in order, and then remove that data from the last one to the first one added, a stack will be your best friend.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity of a stack
&lt;/h3&gt;

&lt;p&gt;Depending on the type of implementation of a stack &lt;em&gt;(by using an array or an object)&lt;/em&gt;, there are different levels of complexity, both for space &lt;em&gt;(amount of memory that it will use)&lt;/em&gt; and time &lt;em&gt;(how long it will take to perform operations on that stack, such as: adding, reading, searching and deleting elements)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Note: Let's consider n = depending on the number of elements in the structure, 1 = direct access).&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Space complexity
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Array:&lt;/strong&gt; O(n).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object:&lt;/strong&gt; O(n).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For both cases, the space complexity will be O(n), meaning that it will increase or decrease proportionally to the number of elements that are stored.&lt;/p&gt;

&lt;h4&gt;
  
  
  Time complexity
&lt;/h4&gt;

&lt;p&gt;For an implementation using arrays:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An using objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Methods and/or functionality of a stack
&lt;/h3&gt;

&lt;p&gt;Traditionally, a stack needs to have functionality that allows to add new elements, extract them, and review them. Even though we can choose whatever name we want for these methods, there is a convention of using the following names to define them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;push:&lt;/strong&gt; Adds a new value &lt;strong&gt;at the end of the stack&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pop:&lt;/strong&gt; Returns the last value, &lt;strong&gt;removing it from the stack.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;peek:&lt;/strong&gt; Returns the last value inserted, &lt;strong&gt;without removing it from the stack.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;size:&lt;/strong&gt; Returns the number of elements that the stack has.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;print:&lt;/strong&gt; Displays the contents of the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to implement a stack
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Option 1: Using an array
&lt;/h4&gt;

&lt;p&gt;Implementing a stack using arrays in JavaScript is relatively straightforward since most of the methods from the previous list are already included in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;Array prototype implementation&lt;/a&gt;, so we only need to write a small wrapper that interacts with these methods and return the corresponding values.&lt;/p&gt;

&lt;p&gt;The only method that we'll need to implement manually is &lt;strong&gt;peek&lt;/strong&gt;, which will return the last value of the array, equal to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length"&gt;length of the array&lt;/a&gt; minus one &lt;em&gt;(since arrays are zero index-based, but length shows the total amount of elements that the array contains, starting from 1)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The implementation would look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// we create and initialize the stack as an empty array.&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&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;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// pushing an element uses the native push method.&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// pop will return the last element by using the native pop method.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// peek checks the last element of the array by using the length&lt;/span&gt;
    &lt;span class="c1"&gt;// (total number of elements) minus 1 to find the right index.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// size just returns the length of the array.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&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="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// print will do a console log of the array&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Stack&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stone Cold Steve Austin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["Stone Cold Steve Austin"]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Rock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// ["Stone Cold Steve Austin", "The Rock"];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ["Stone Cold Steve Austin", "The Rock"];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// The Rock&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// The Rock&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Stone Cold Steve Austin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Option 2: Using an object
&lt;/h4&gt;

&lt;p&gt;Implementing a stack with an object requires a bit of additional work since the native methods of the arrays will not be available here, so we'll have to implement them manually.&lt;/p&gt;

&lt;p&gt;One of the ways to achieve this is, when creating the stack, initialize a variable that will act as a cursor and keep the current position of the last element added, as well as the total number of elements inserted. Since the default behavior of a stack just requires us to add/remove the last element added, as long as we keep track of the current position, we should be able to achieve this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To add elements, we'll use &lt;em&gt;&lt;strong&gt;this.count&lt;/strong&gt;&lt;/em&gt; as a reference of the current position and we'll use JavaScript's bracket notation to do a direct insertion in the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&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;For &lt;strong&gt;peek, print&lt;/strong&gt; y &lt;strong&gt;size&lt;/strong&gt;, the implementation is basically the same as with arrays. The main difference is that we'll use &lt;em&gt;&lt;strong&gt;this.count&lt;/strong&gt;&lt;/em&gt; instead of &lt;em&gt;&lt;strong&gt;Array.length&lt;/strong&gt;&lt;/em&gt; to identify the index of the element that we'll need to show or to return the total number of elements added.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;peek&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="nx"&gt;size&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&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;Finally, for &lt;strong&gt;pop&lt;/strong&gt; it'll be necessary to do some extra work. The difference with the last case is that after returning the element, we'll need to delete it from the object and move the cursor back to track the &lt;em&gt;&lt;strong&gt;new last element&lt;/strong&gt;&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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;element&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;The full implementation would be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;peek&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="nx"&gt;size&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Stack&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stone Cold Steve Austin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// { "0": "Stone Cold Steve Austin" }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Stone Cold Steve Austin&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Rock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// { "0": "Stone Cold Steve Austin", "1": "The Rock" }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// { "0": "Stone Cold Steve Austin", "1": "The Rock" }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// The Rock&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// The Rock&lt;/span&gt;
&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;print&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// { "0": "Stone Cold Steve Austin" }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Stone Cold Steve Austin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Source Code
&lt;/h3&gt;

&lt;p&gt;You can find the source code of this example here:&lt;a href="https://github.com/Xabadu/js-data-structures"&gt;https://github.com/Xabadu/js-data-structures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Originally published on my blog at &lt;a href="https://xabadu.dev/js-data-structures-stacks/"&gt;xabadu.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>You, me and package.json</title>
      <dc:creator>Fernando Larrañaga</dc:creator>
      <pubDate>Tue, 30 Jul 2019 15:36:17 +0000</pubDate>
      <link>https://forem.com/xabadu/you-me-and-package-json-1dc2</link>
      <guid>https://forem.com/xabadu/you-me-and-package-json-1dc2</guid>
      <description>&lt;p&gt;&lt;em&gt;¿Español? Puedes encontrar la versión traducida de este artículo aquí: &lt;a href="https://medium.com/noders/t%C3%BA-yo-y-package-json-9553929fb2e3" rel="noopener noreferrer"&gt;Tú, yo, y package.json&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have worked in a Node.js or client-side JavaScript project, there is a pretty good chance that you have seen a file called &lt;strong&gt;package.json&lt;/strong&gt;, and that you've poked around its contents. Even though there is a lot of stuff in there that you probably are very familiar with, you may have run into some stuff that is not completely sure what it means or even how it got in there in the first place.&lt;/p&gt;

&lt;p&gt;In this article, we'll go over the details of what each thing inside this file means, what kind of stuff you can put in there and how it can help you become even more productive while developing your projects.&lt;/p&gt;

&lt;p&gt;But first things first:&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the package.json file?
&lt;/h2&gt;

&lt;p&gt;Historically, Node has been using a tool called &lt;strong&gt;npm&lt;/strong&gt; to manage package and dependencies. This tool, which is usually installed along Node, has two main jobs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Publishing your project to npm's public registry &lt;em&gt;(so other users can download it as a dependency for their projects)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Manage the dependencies of your own project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To be able to do this, the npm client creates and uses a file called &lt;strong&gt;package.json&lt;/strong&gt;. This file contains information about the project such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name.&lt;/li&gt;
&lt;li&gt;Version.&lt;/li&gt;
&lt;li&gt;Dependencies.&lt;/li&gt;
&lt;li&gt;Repository.&lt;/li&gt;
&lt;li&gt;Author(s).&lt;/li&gt;
&lt;li&gt;License.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Additionally, as well as keeping a record, by using this file the integrity of the project can be guaranteed for everyone that gets a copy. This means that any user, at any point in time, will be able to access the same set of &lt;strong&gt;similar compatible dependencies&lt;/strong&gt;. In a way, we can think of the &lt;strong&gt;package.json&lt;/strong&gt; file as our project's manifesto. One thing to keep in mind here is that while the dependencies listed in a package.json file should be similar and compatible to the original ones, is not guaranteed that the project will be able to run without any issues if a considerable time has passed since the original declaration _(could be cases where changes were introduced in a different version of the package that is also considered compatible but could break some functionality). For this, is the use of &lt;a href="https://docs.npmjs.com/files/package-lock.json" rel="noopener noreferrer"&gt;&lt;strong&gt;lock files&lt;/strong&gt;&lt;/a&gt; is recommended.&lt;/p&gt;

&lt;p&gt;Let's look at an example by considering the following scenario:&lt;/p&gt;

&lt;p&gt;Two devs are working on the same project, with independent copies in their own computers. Dev #1 decides that in order to complete the new feature, he'll need to use a new library inside the project.&lt;/p&gt;

&lt;p&gt;Without any dependency management, he would need to do one of two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manually download the library into the project's directory and keep a copy in there that has to be included wherever the project is being stored, potentially increasing the amount of data that needs to be transferred anytime a new Dev gets a copy.&lt;/li&gt;
&lt;li&gt;Download a copy of the library without keeping a copy, but letting every person working on the project &lt;em&gt;(present and future)&lt;/em&gt; know that they'll need to get a copy of it and making sure they're using the exact same version. &lt;em&gt;(Good way to make new friends, but not so good in terms of time management)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With a dependency management tool, such as npm, neither of these steps are necessary anymore. Anyone that gets a copy of the project, now and forever &lt;em&gt;(as long as the package hasn't been unpublished)&lt;/em&gt;, will be able to install each and every one of the dependencies without the need to transfer the actual copies of them. As a result, the actual project that it's stored in a repository, and shared, is a lot lighter and redundant data is not being transferred.&lt;/p&gt;

&lt;p&gt;It's worth mentioning that, even though a lot of the information contained inside a package.json file seems to be specific for publishing projects in the npm registry, we can still use npm to manage other kinds of projects that will never be published in there, such as Web and/or mobile apps, games, and others.&lt;/p&gt;

&lt;p&gt;As a final note about dependency management, a while back, my very good friends at Facebook &lt;em&gt;(note: They don't really know we're friends... yet :()&lt;/em&gt; launched a similar tool called &lt;strong&gt;yarn&lt;/strong&gt;, which for all intents and purposes of this article is able to do the same two tasks we mentioned above, and its use of the package.json file is the same, unless explicitly stated.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create a package.json file
&lt;/h2&gt;

&lt;h3&gt;
  
  
  One rule, to ring them all (?)
&lt;/h3&gt;

&lt;p&gt;Before creating a package.json file, there's one rule to know: The file must be in a valid JSON format and must abide by the &lt;a href="https://google.github.io/styleguide/jsoncstyleguide.xml" rel="noopener noreferrer"&gt;JSON style spec.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that in mind, there are 2 different ways to create the file: Manually or using the npm/yarn cli:&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the package.json manually
&lt;/h3&gt;

&lt;p&gt;If for any reason the option of using the npm/yarn cli is not available and we really need to create a file manually, we'll need to add a new file &lt;em&gt;(named &lt;strong&gt;package.json&lt;/strong&gt;)&lt;/em&gt; to the root of the project containing the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;version&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every other field &lt;em&gt;(listed in the next section)&lt;/em&gt; is optional, though recommended.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a package.json using the npm/yarn cli
&lt;/h3&gt;

&lt;p&gt;This is the recommended way to do it. Creating a package.json file can be done by running any of these commands (depending on which package manager you're using) in the root directory of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on whether npm or yarn is being used, certain information will have to be provided before the file is created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5cl8hyb2fncvh0j60w4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5cl8hyb2fncvh0j60w4.png" alt="Creating a package.json using npm" width="800" height="938"&gt;&lt;/a&gt;&lt;/p&gt;
Creating a package.json using npm



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faytrmu8oh9fajlwf7upl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faytrmu8oh9fajlwf7upl.png" alt="Creating a package.json using yarn" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;
Creating a package.json using yarn



&lt;p&gt;Once finished, a brand new &lt;strong&gt;package.json&lt;/strong&gt; file will be created in the root directory of the project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick tip&lt;/strong&gt;: If you need to quickly create a package.json file with default values, you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The sections of the package.json file
&lt;/h2&gt;

&lt;p&gt;After creating the package.json file, either manually or using the cli, we'll find inside a big object with different keys and values &lt;em&gt;(like the initial image of this article)&lt;/em&gt;. Also, as time passes and new dependencies/configurations are included, new keys and values will also be included here. The following is a list of the most common ones that we'll probably run into at some point in time:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/em&gt;: This list only includes properties officially declared and supported by npm. There are multiple external libraries that also support keys to read from the package.json file &lt;em&gt;(ie. Jest and the property “jest”)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  name
&lt;/h3&gt;

&lt;p&gt;This is one of the two required fields that need to be included in the file &lt;em&gt;(along with version)&lt;/em&gt;. It's a string that represents the name of the current project and also works as a unique identifier in case the project is published in the registry.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rules:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The name must be lowercase and cannot start with a period or an underscore.&lt;/li&gt;
&lt;li&gt;The maximum length of the name is 214 characters and must be URL safe &lt;em&gt;(more info about URL safe characters can be found &lt;a href="https://www.ietf.org/rfc/rfc3986.txt" rel="noopener noreferrer"&gt;here&lt;/a&gt;, section 2.3)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few other things to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the project will be published in the npm registry, the name must be unique and available &lt;em&gt;(no other projects published before using the same name)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Even though it's considered a good practice to use related names if the package belongs to a certain technology &lt;em&gt;(like using &lt;strong&gt;react-{something}&lt;/strong&gt; for React libraries)&lt;/em&gt;, it's also recommended to not use &lt;strong&gt;node&lt;/strong&gt; or &lt;strong&gt;js&lt;/strong&gt; in the name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  version
&lt;/h3&gt;

&lt;p&gt;The other required field, along with the name. It's a string indicating the current version of the project. Node.js and JavaScript projects usually abide by the conventions defined in Semantic Versioning &lt;em&gt;(or semver)&lt;/em&gt;, which defines the following structure for versions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;MAJOR.MINOR.PATCH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.semver.org/" rel="noopener noreferrer"&gt;More info about semver.&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  description
&lt;/h3&gt;

&lt;p&gt;A string with a short explanation of what the project is about. If the package is ever published, this text will also be relevant for search results.&lt;/p&gt;

&lt;h3&gt;
  
  
  keywords
&lt;/h3&gt;

&lt;p&gt;Same as description, but instead of text, it's an array of strings that includes relevant terms that can be used to search the package.&lt;/p&gt;

&lt;h3&gt;
  
  
  homepage
&lt;/h3&gt;

&lt;p&gt;A string with a valid URL for the project's Website.&lt;/p&gt;

&lt;h3&gt;
  
  
  bugs
&lt;/h3&gt;

&lt;p&gt;A string with a valid URL where users can report issues found in the project. Usually, the &lt;strong&gt;issues&lt;/strong&gt; repository URL is used for this.&lt;/p&gt;

&lt;h3&gt;
  
  
  license
&lt;/h3&gt;

&lt;p&gt;A string that specifies the type of license under which this project is being released. This can be personal, commercial, open, or private.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://spdx.org/licenses/" rel="noopener noreferrer"&gt;More info on available licenses.&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  author
&lt;/h3&gt;

&lt;p&gt;It can either be a string or an object with information about the creator of the project.&lt;/p&gt;

&lt;p&gt;If it's an object, must be in the following format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name.&lt;/li&gt;
&lt;li&gt;email.&lt;/li&gt;
&lt;li&gt;URL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And if it's a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="s2"&gt;"Name &amp;lt;email&amp;gt; (URL)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  contributors
&lt;/h3&gt;

&lt;p&gt;Similar to author, it's an array of objects &lt;em&gt;(or an array of strings)&lt;/em&gt; with the information of the contributors of the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  files
&lt;/h3&gt;

&lt;p&gt;An array of strings or patterns &lt;em&gt;(such as *.js)&lt;/em&gt; of the files that will be included in the project if it's ever published in the registry. If this section is not defined, every file &lt;em&gt;(that is not explicitly excluded in a file like .gitignore)&lt;/em&gt; will be included.&lt;/p&gt;

&lt;h4&gt;
  
  
  Some things to keep in mind about this:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;By default, every file listed inside &lt;strong&gt;.gitignore&lt;/strong&gt; will be excluded from publishing.&lt;/li&gt;
&lt;li&gt;Instead of adding a &lt;em&gt;files&lt;/em&gt; section, a &lt;strong&gt;.npmignore&lt;/strong&gt; file can be included in the root of the project with a list of files to exclude from publishing &lt;em&gt;(similar as what .gitignore does)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Some files &lt;strong&gt;will always be included&lt;/strong&gt;, regardless of explicit exclusion. Amongst these files are: package.json, README, CHANGES / CHANGELOG / HISTORY, LICENSE / LICENCE, NOTICE and the file defined as an entry point of the app &lt;em&gt;(more about this in the following section)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Some files &lt;strong&gt;will always be ignored&lt;/strong&gt;, regardless of explicit inclusion. A list of these files can be found &lt;a href="https://docs.npmjs.com/files/package.json#files" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  main
&lt;/h3&gt;

&lt;p&gt;A string that defines the entry point of the project. If the project is a package/library, this is the file that will be imported whenever someone requires it. For instance:&lt;/p&gt;

&lt;p&gt;If your project is called &lt;strong&gt;super-awesome-library&lt;/strong&gt; and a user installs it and then inside their app they do:&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;superAwesomeLibrary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;super-awesome-library&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;the &lt;strong&gt;superAwesomeLibrary&lt;/strong&gt; variable will have the contents of whatever your main file is exporting, so if your package.json file has a declaration such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lib/foo.js"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then the &lt;code&gt;superAwesomeLibrary&lt;/code&gt; variable will contain the content that is exported in &lt;code&gt;lib/foo.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If this section is omitted, then the contents of the &lt;code&gt;index.js&lt;/code&gt; file that lives in the root directory of the project will be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  bin
&lt;/h3&gt;

&lt;p&gt;A string &lt;em&gt;(if it's only one)&lt;/em&gt; or an object &lt;em&gt;(if it's multiple)&lt;/em&gt; defining scripts that will be installed and will be available as commands in the PATH. After the package is installed, a symbolic link will be created from &lt;strong&gt;/usr/local/bin&lt;/strong&gt; to the respective file inside the project and will be available as a command-line program.&lt;/p&gt;

&lt;p&gt;For instance, let's say we have a file called &lt;em&gt;cli.js&lt;/em&gt; inside our project, and we wanna make it available for users to call it directly from their terminals. The way to achieve it would be to include a single string as &lt;strong&gt;bin&lt;/strong&gt; inside package.json as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"super-awesome-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"bin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cli.js"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the contents of &lt;em&gt;cli.js&lt;/em&gt; can be used by running whatever we put as &lt;strong&gt;name&lt;/strong&gt; of the project in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;super-awesome-library
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the user is running that friendly name, in reality, something like this is happening &lt;em&gt;"behind the scenes"&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node cli.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then whatever is on that file will be run.&lt;/p&gt;

&lt;p&gt;If instead of that, we have multiple files that we want to turn into executable scripts, we can use the object format instead. This will add a symbolic link for every &lt;em&gt;key-value&lt;/em&gt; pair, using the &lt;strong&gt;key&lt;/strong&gt; as the command that will be available afterward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"bin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"script-1"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"super-h4x0r-script1.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"script-2"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"on-your-left.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that object, both &lt;strong&gt;“script-1”&lt;/strong&gt; and &lt;strong&gt;“script-2”&lt;/strong&gt; will be included in the PATH, each pointing to the respective .js file that was their pair inside the bin object.&lt;/p&gt;

&lt;p&gt;This is what many known packages, such as &lt;em&gt;nodemon&lt;/em&gt; or &lt;em&gt;react-native&lt;/em&gt;, include so we can use them as terminal commands directly without having to run &lt;em&gt;node whatever-the-path-to-the-file-is&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  man
&lt;/h3&gt;

&lt;p&gt;A string, or an array of strings, defining one &lt;em&gt;(or many)&lt;/em&gt; file(s) that will be available/shown if the &lt;strong&gt;man&lt;/strong&gt; command is run for this project.&lt;/p&gt;

&lt;h3&gt;
  
  
  directories
&lt;/h3&gt;

&lt;p&gt;An object defining the structure of the project and where every folder is located for certain sections. The most common ones are &lt;em&gt;bin&lt;/em&gt;, &lt;em&gt;doc&lt;/em&gt;, &lt;em&gt;example&lt;/em&gt;, &lt;em&gt;lib&lt;/em&gt;, &lt;em&gt;man&lt;/em&gt;, &lt;em&gt;test&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"bin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./bin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"doc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./doc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./lib"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  repository
&lt;/h3&gt;

&lt;p&gt;An object defining where this project is stored and can be found for contribution. The object has the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;string&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;strong&gt;type&lt;/strong&gt; refers to the type of repository &lt;em&gt;(such as svn or git)&lt;/em&gt; and &lt;strong&gt;URL&lt;/strong&gt; is a valid URL where it can be found.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"git"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://github.com/my-user/super-awesome-project"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  scripts
&lt;/h3&gt;

&lt;p&gt;An object defining commands that can be used with the npm/yarn cli for the project. Some scripts are predefined and reserved and can be used without defining them, such as &lt;strong&gt;start, install, preinstall, pretest, test, and posttest&lt;/strong&gt; amongst others. &lt;em&gt;(a full list can be found &lt;a href="https://docs.npmjs.com/misc/scripts" rel="noopener noreferrer"&gt;here&lt;/a&gt;)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the same way, we can define our own scripts and use custom names and instructions for them. This is very useful to create shortcuts and/or combined tasks without having to remember the full commands and/or params every time.&lt;/p&gt;

&lt;p&gt;For instance, let's pretend that we have an app that needs to run a task to minify the JS files before creating a new version, and we do that with a script that lives in &lt;strong&gt;tasks/minify.js&lt;/strong&gt; and passing a flag or param that uses internally. Usually, we would run &lt;strong&gt;node tasks/minify.js --someflag --maybeanother&lt;/strong&gt; every time we want to achieve this &lt;em&gt;(and we would need to remember the name of the flags as well)&lt;/em&gt;. However, if we instead add it to the npm scripts, we could do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"minify"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node tasks/minify.js --someflag --maybeanother"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run minify
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This achieves the exact same result. The cool thing about this is not only not having to remember the exact commands we need to run each time, but also that npm scripts can be combined and executed sequentially, so we can create complex tasks and even trigger some automatically if we use any of the &lt;strong&gt;&lt;em&gt;pre&lt;/em&gt;&lt;/strong&gt; hooks &lt;em&gt;(like pretest or prepublish)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For instance, let's say that we want to run that same minify task, as well as run our code through a linter, right before we run the tests of our app. For that, we could add something like this &lt;em&gt;(pretending our app code lives in the &lt;strong&gt;src&lt;/strong&gt; folder)&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"pretest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node tasks/minify.js --someflag --maybeanother &amp;amp;&amp;amp; eslint src"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we could include it directly as part of the &lt;strong&gt;test&lt;/strong&gt; script &lt;em&gt;(this example uses jest, but you can replace that with mocha/ava/tape/etc or the tool of your choice)&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node tasks/minify.js --someflag --maybeanother &amp;amp;&amp;amp; eslint src &amp;amp;&amp;amp; jest"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a final note about this, these scripts need to be run as &lt;strong&gt;npm run 'script'&lt;/strong&gt; unless it's one of the predefined/reserved by npm &lt;em&gt;(listed at the beginning of this section)&lt;/em&gt;. However, if you are using yarn, you can omit the &lt;strong&gt;run&lt;/strong&gt; part completely and just do &lt;strong&gt;yarn 'script'&lt;/strong&gt;, regardless of it it's a predefined script or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  config
&lt;/h3&gt;

&lt;p&gt;An object where values can be set to use as environment variables that can be later accessed from within your code.&lt;/p&gt;

&lt;p&gt;To set a config value, we can do this inside the package.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"config"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"port"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, values can be referenced from within the code using &lt;strong&gt;process.env.npm_package_config_{value}&lt;/strong&gt;, 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;npm_package_config_port&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;`App listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These config values can be changed from outside the package.json file at any time by executing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm config set {name of the project}:{config key} {config value}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For our previous example, we could do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm config &lt;span class="nb"&gt;set &lt;/span&gt;my-app:port 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  dependencies
&lt;/h3&gt;

&lt;p&gt;An object that stores the name and version of each dependency installed &lt;em&gt;(and saved)&lt;/em&gt; during the project's history. Every time that someone gets a new copy of this project and runs &lt;em&gt;&lt;em&gt;npm install&lt;/em&gt;&lt;/em&gt;, all of these dependencies will be installed &lt;em&gt;(with the newest compatible version)&lt;/em&gt;. These dependencies, as well as the next two categories, are defined with the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"name-of-the-dependency"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"(^|~|version)|url"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"backbone"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lodash"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.6.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mocha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~3.5.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"super-mega-library"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/super-mega-library-4.0.0.tar.gz"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These dependencies can have either the version installed and saved or a valid URL where the package with the current version can be obtained &lt;em&gt;(this URL can also be a local path inside the same computer)&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  What are the ^ and ~ symbols at the beginning of the versions?
&lt;/h4&gt;

&lt;p&gt;Every dependency installed can have a character defining the range of acceptable compatible versions. These 2 are the most commons, but a full list can be found &lt;a href="https://docs.npmjs.com/files/package.json#dependencies" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In other words, these characters add instructions on how this dependency should be treated the next time an &lt;strong&gt;npm install&lt;/strong&gt; happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If the version has a caret &lt;code&gt;(^)&lt;/code&gt;:&lt;/strong&gt; Allow installing a different version, as long as it's just a minor change &lt;em&gt;(the second number of the version)&lt;/em&gt;. If no different minor versions are found, the same version will be installed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If the version has a tilde &lt;code&gt;(~)&lt;/code&gt;:&lt;/strong&gt; Allows installing a different version, as long as it's just a patch change &lt;em&gt;(the last number of the version)&lt;/em&gt;. If no different patches are found, the same version will be installed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If the version just has the number and no characters&lt;/strong&gt;: The exact same version defined has to be installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance, with the dependencies specified above, if we run &lt;strong&gt;npm install&lt;/strong&gt; and new versions are available:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;backbone&lt;/strong&gt; and &lt;strong&gt;super-mega-library&lt;/strong&gt; would stay using the same versions (1.0.0 and 4.0.0 respectively).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;lodash&lt;/strong&gt; could either install the same version, or any versions between &lt;strong&gt;4.6.1&lt;/strong&gt; and &lt;strong&gt;4.9.9&lt;/strong&gt;, but never anything equal to &lt;strong&gt;5.x.x&lt;/strong&gt; or above.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mocha&lt;/strong&gt; could either install the same version, or any versions between &lt;strong&gt;3.5.3&lt;/strong&gt; and &lt;strong&gt;3.5.9&lt;/strong&gt;, but never anything above that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  devDependencies
&lt;/h3&gt;

&lt;p&gt;The same format as the dependencies listed above, but this section will include all the dependencies that the project uses but are not needed for a production environment &lt;em&gt;(like testing tools, local dev servers, optimization tools, etc.)&lt;/em&gt;. Any computer that gets a copy of this project and has &lt;strong&gt;production&lt;/strong&gt; set as the &lt;strong&gt;NODE_ENV&lt;/strong&gt; variable will not install the dependencies listed in this section.&lt;/p&gt;

&lt;h3&gt;
  
  
  peerDependencies
&lt;/h3&gt;

&lt;p&gt;This uses the same format as well, but these dependencies while not necessarily installed, define the compatibility required for this app/package to work correctly. For instance, if we are developing a library that is only compatible with version 16 of React, we would need to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"peerDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"16.0.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Older versions of npm &lt;em&gt;(1 and 2)&lt;/em&gt; used to install these peerDependencies automatically, but that's not the case anymore. As of version 3, if a compatible version is not found when this project is installed, a warning will be triggered.&lt;/p&gt;

&lt;h3&gt;
  
  
  engines
&lt;/h3&gt;

&lt;p&gt;An object where we can define the minimum versions of node and npm that this project supports. It's defined in the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"engines"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;gt;= 6.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"npm"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;gt;= 3.0.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the project is installed, a check will be run to assure the compatibility. If this is not met, the install process will stop.&lt;/p&gt;

&lt;p&gt;As is the case with dependencies, we can use ranges &lt;em&gt;(like &lt;code&gt;**&amp;gt;=**&lt;/code&gt;, &lt;code&gt;**^**&lt;/code&gt;, &lt;code&gt;**~**&lt;/code&gt;, etc.)&lt;/em&gt; to define compatible versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  More information
&lt;/h2&gt;

&lt;p&gt;Even though these are the most common things we'll find and use inside a package.json file, there is still some additional ones that can be interesting or useful to check. For additional references, I'd recommend reviewing the official docs of npm on a regular basis, since it's constantly being updated anytime a new version is released.&lt;/p&gt;

&lt;p&gt;Useful links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/files/package.json" rel="noopener noreferrer"&gt;Official package.json docs from npm.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/misc/scripts" rel="noopener noreferrer"&gt;npm-scripts docs.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/misc/config" rel="noopener noreferrer"&gt;npm-config docs.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally published on my blog at &lt;a href="https://xabadu.dev/you-me-and-package-json/" rel="noopener noreferrer"&gt;xabadu.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>npm</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Getting started with Web Performance</title>
      <dc:creator>Fernando Larrañaga</dc:creator>
      <pubDate>Wed, 22 May 2019 23:56:47 +0000</pubDate>
      <link>https://forem.com/xabadu/getting-started-with-web-performance-3phi</link>
      <guid>https://forem.com/xabadu/getting-started-with-web-performance-3phi</guid>
      <description>&lt;p&gt;Performance is a term that we know we should be constantly thinking about &lt;em&gt;(and working on)&lt;/em&gt;, but for different reasons, we tend to forget about it. The truth is that it can be such an overwhelming topic that when face these issues, it's hard to know where to even begin.&lt;/p&gt;

&lt;p&gt;And even though, nowadays we have a lot of tools that will help us test our apps to see how they're doing, at the same time understanding how they work can be a bit tricky &lt;em&gt;(and sometimes really hard)&lt;/em&gt;, so with the limited time we have, we often put it to the side because results seem to be less noticeable than other things like shipping a new feature or fixing a nasty bug.&lt;/p&gt;

&lt;p&gt;In this article, I'll cover some initial notions on why should we care, how to get started and how to make it part of our daily workflows.&lt;/p&gt;

&lt;p&gt;But first, it's a good idea to go over a few things to both understand the importance of performance and to set the right expectations about it:&lt;/p&gt;

&lt;h3&gt;
  
  
  Why should I care about performance?
&lt;/h3&gt;

&lt;p&gt;While less noticeable at first glance, the performance of our app or Website can have an even bigger &lt;em&gt;(and often negative)&lt;/em&gt; impact on our users, sometimes even driving them away and choosing to go to a different site. If your app belongs to a business, this could turn into an even worse result, driving customers away and losing a potential sale to a competitor that has a &lt;em&gt;"faster"&lt;/em&gt; site than yours.&lt;/p&gt;

&lt;p&gt;We can find &lt;a href="https://www.google.com/search?ei=11jkXKmCAcSEtQW9_pbIDA&amp;amp;q=cost+of+web+latency&amp;amp;oq=cost+of+web+latency&amp;amp;gs_l=psy-ab.3...10361.10945..11223...0.0..0.129.507.0j4......0....1..gws-wiz.......0i71.EjlLmCXqWvU" rel="noopener noreferrer"&gt;plenty of stats&lt;/a&gt; shared by big companies about how delays of even &lt;strong&gt;1 second or less&lt;/strong&gt; while loading their content can impact their sales or drive traffic away, so it makes you wonder if that kind of impact can happen to well-known brands, what can it do to ours?&lt;/p&gt;

&lt;h3&gt;
  
  
  No silver bullet
&lt;/h3&gt;

&lt;p&gt;Like most good things in life, having a performant app doesn't come easy, and it's important to understand that right off the bat. Working on performance related issues involve a lot of... well, work. Sadly, there's no miraculous package or library that we can use and will solve all of our problems &lt;em&gt;(yet, who knows, maybe one day, you know, with like 🤖🤖🤖 and stuff)&lt;/em&gt;, but that doesn't mean it's impossible to achieve, in fact, quite the opposite. The process of solving performance issues and improving our apps is relatively straightforward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collect some data.&lt;/li&gt;
&lt;li&gt;Experiment.&lt;/li&gt;
&lt;li&gt;Collect some new data and compare.&lt;/li&gt;
&lt;li&gt;Draw conclusions &lt;em&gt;(a.k.a keep what worked, undo what didn't)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While some of these parts might have different implications &lt;em&gt;(and some of them might be a bit tricky)&lt;/em&gt;, the process is always the same, which makes these two things even more important to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What works on some other app might not be the right thing for yours&lt;/strong&gt;: This doesn't mean that good practices and general advice should be dismissed, it's often useful advice that will help us get there, just that &lt;em&gt;it might not be right (or the biggest priority) for your use case&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance is, a lot of times, all about trade-offs&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Rewrites are less likely than improvements
&lt;/h3&gt;

&lt;p&gt;As developers, a lot of times our first reaction when we get involved in a project that has issues is to think about rewriting that code. Truth is, most of the times, we won't be able to do so due to time constraints, budgets or other priorities. That's why it's a good idea to think about improving the current codebase we're working on instead of throwing it out and writing a new one. With this, we'll be able to compare results with actual data that has been live for a long time and get a better idea of the impact every change makes.&lt;/p&gt;

&lt;p&gt;And, if we ever actually get to do a rewrite, there's a lot of things that we'll probably keep in mind to make it better.&lt;/p&gt;

&lt;p&gt;So, with all of this in mind, how do we start?: &lt;strong&gt;With a plan&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ready to make a plan
&lt;/h2&gt;

&lt;p&gt;Going head first into working not knowing what we're supposed to achieve will probably cause more problems than it solves. Understanding what should be the focus of the work and making a plan on how to execute it will allow us to get actual wins that are relevant to the main purpose of our app.&lt;/p&gt;

&lt;p&gt;Wait, purpose, what do you mean?&lt;/p&gt;

&lt;h3&gt;
  
  
  Identify the main goal of your application
&lt;/h3&gt;

&lt;p&gt;When building an app, or a Website, probably one of the first questions we should ask ourselves is &lt;em&gt;What is this app trying to achieve?&lt;/em&gt;. In most cases, the goal is pretty clear: sell something, show content, provide a service and so on. Now, while identifying the main goal can be straightforward, a lot of times it's hard to get a clear idea of how the app should focus to accomplish that goal.&lt;/p&gt;

&lt;p&gt;That focus tends to be different between apps, especially if they fall under different categories, and it relates closely to where our performance efforts should be concentrating on.&lt;/p&gt;

&lt;p&gt;For instance, let's say we build a Website to sell cookies. The main goal of the site would be pretty clear: selling those delightful 🍪🍪🍪. Now, when planning the features of the homepage of the site, we know we'll probably have these sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A header with a nice logo and a menu with cool animations.&lt;/li&gt;
&lt;li&gt;A list with all the cookies available.&lt;/li&gt;
&lt;li&gt;A sidebar with contact info, social media links, newsletter signup for promotions and some ads.&lt;/li&gt;
&lt;li&gt;A cool footer with all the legal info.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we go back to what our main goal is &lt;em&gt;(selling cookies)&lt;/em&gt;, and we think about the importance of each section and how it relates to the goal, we can probably assume that our users don't really care about some of the stuff on that list and that they're visiting the site to get some cookies &lt;em&gt;(and that's what we want too)&lt;/em&gt;. So with that in mind, we should probably put our efforts in showing them the list with the cookies first and as fast as possible instead of spending time and resources displaying the menu and the animations &lt;em&gt;(as cool as they might be)&lt;/em&gt; or the sidebar with the social media links, so that's where our plan should focus on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make a plan for gradual improvements (5-10% at a time)
&lt;/h3&gt;

&lt;p&gt;One very important thing to understand when talking about performance is that it's an ongoing effort and that it's very likely that we won't be able to fix every issue right away. A big part of this is measuring and experimentation, so there's a lot of back and forth between trying new things, analyzing trade-offs and making decisions based on that. As well, there's a pretty good chance that the changes we make will not have huge differences in terms of percentages, but that doesn't mean that are not wins, on the contrary, every improvement we make will have an impact on every user that visits our site and will make their experience better.&lt;/p&gt;

&lt;p&gt;Think about it this way: If we have an app built with JavaScript, and it lives in a 500kb bundle file, a user that visits our site will be downloading 500kb of code that will have to be parsed, interpreted and compiled by their browsers. Now, let's say we find a way to make some improvements on that file and we end up decreasing the size of the bundle by 10%. Even though 10 doesn't seem like much, it's still 50kb less that we're sending &lt;em&gt;(and that will need to be parsed, interpreted and compiled)&lt;/em&gt;, so not only things will load faster, but our users will appreciate having to download less amount of data to use the app &lt;em&gt;(especially those on mobile networks)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, with that in mind, a good rule of thumb will be to plan for gradual improvements between 5-10% on each cycle of work. If we can get more, excellent, but everything between those numbers should be great and realistic since initially, the wins will be bigger and more noticeable, but as we progress, it'll be harder to find places for improvement. After every cycle of improvements, we can get some new samples and plan for the next one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get your data
&lt;/h3&gt;

&lt;p&gt;The last step before getting to work is to get real data from the current performance of our app to identify the pain points and prioritize the work. We do that by measuring.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why you should measure
&lt;/h3&gt;

&lt;p&gt;Whenever thinking about why should we measure, I like to think about these principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes things look fast, but they're not.&lt;/li&gt;
&lt;li&gt;Sometimes things look ok but could be better.&lt;/li&gt;
&lt;li&gt;Sometimes things look slow, but it's not &lt;em&gt;&lt;strong&gt;their&lt;/strong&gt;&lt;/em&gt; fault.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Browsers are faster than ever, so nowadays are able to do a lot of things faster than we can notice, but that doesn't necessarily mean that they are, indeed, &lt;em&gt;fast&lt;/em&gt;. A lot of stuff happens underneath that we won't get a clear idea until we peek under the hood and see how everything is being loaded, how much time each part takes and if any of it it's causing issues.&lt;/p&gt;

&lt;p&gt;By using tools to measure each part of our app, we'll get a clear picture of how everything's truly looking, and it'll be easier to identify the problems and plan the work.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to measure
&lt;/h3&gt;

&lt;p&gt;There is a lot of tooling these days that allow us to get a clear idea of how our app is performing, and even get some advice on how to improve any issues that are found. Out of those alternatives, the ones I like to use are:&lt;/p&gt;

&lt;h4&gt;
  
  
  Lighthouse (Web, CLI, CI)
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developers.google.com/web/tools/lighthouse/" rel="noopener noreferrer"&gt;Google Lighthouse&lt;/a&gt; is probably the best tool nowadays to run performance audits against our app. It takes care of checking different load scenarios, how we're handling resources and even give advice on how to improve any issues found, even when it comes to accessibility and SEO. The best thing about it, is that there are multiple ways to run it &lt;em&gt;(through &lt;a href="https://web.dev/measure" rel="noopener noreferrer"&gt;Google's Web Dev site&lt;/a&gt;, &lt;a href="https://chrome.google.com/webstore/detail/lighthouse/blipmdconlkpinefehnmjammfjpmpbjk" rel="noopener noreferrer"&gt;Chrome extension&lt;/a&gt; or even &lt;a href="https://developers.google.com/web/tools/lighthouse/#devtools" rel="noopener noreferrer"&gt;Chrome Dev Tools&lt;/a&gt;)&lt;/em&gt;, and can even be incorporated to our daily workflow as a build check with their &lt;a href="https://github.com/GoogleChromeLabs/lighthousebot" rel="noopener noreferrer"&gt;GitHub integration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frt5kfvu0dfv2yyy4z4lg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frt5kfvu0dfv2yyy4z4lg.png" alt="Lighthouse in Google's Web Dev site"&gt;&lt;/a&gt;&lt;/p&gt;
Lighthouse in Google's Web Dev site



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv428jozl6c0tdsjey3zb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fv428jozl6c0tdsjey3zb.png" alt="Lighthouse in Chrome Dev Tools"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lighthouse in Chrome Dev Tools&lt;/p&gt;

&lt;h4&gt;
  
  
  PageSpeed Insights
&lt;/h4&gt;

&lt;p&gt;A while back, if you measured a URL in &lt;a href="https://developers.google.com/speed/pagespeed/insights/" rel="noopener noreferrer"&gt;PageSpeed Insights&lt;/a&gt; and Lighthouse, you would often get &lt;em&gt;some&lt;/em&gt; different information, scores, and advice, so it is useful to run both and consolidate that information. Currently, it's a lot closer, but it doesn't hurt to actually keep checking on both to make sure we're tackling all the issues found.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmfozflm2qfqae9gp4wll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmfozflm2qfqae9gp4wll.png" alt="PageSpeed Insights"&gt;&lt;/a&gt;&lt;/p&gt;
PageSpeed Insights



&lt;h4&gt;
  
  
  Pingdom Website Speed Test
&lt;/h4&gt;

&lt;p&gt;Running audits with an external tool like &lt;a href="https://tools.pingdom.com" rel="noopener noreferrer"&gt;Pingdom&lt;/a&gt; is also useful to have extra data that we can compare. One of the cool things about this one, in particular, is that it allows us to test how our site is loading from different locations, so if we're reaching a global audience, we'll have a better idea of how they see our content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fujsvycj1extpavl8bxi8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fujsvycj1extpavl8bxi8.png" alt="Pingdom Website Speed Test"&gt;&lt;/a&gt;&lt;/p&gt;
Pingdom Website Speed Test



&lt;h4&gt;
  
  
  Chrome Dev Tools
&lt;/h4&gt;

&lt;p&gt;And last, but certainly not least, it's the Performance Tab of Chrome Dev Tools, which will be one of our best friends while doing local development. With it, we can record everything that happens with our site while it loads, including how much code is being parsed, in what order, if we're making our CPU work too much and how long everything takes, which will give us a clear picture on potential bottlenecks that we should work on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fei8grylyfeduebi9mt9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fei8grylyfeduebi9mt9e.png" alt="Performance Tab in Chrome Dev Tools"&gt;&lt;/a&gt;&lt;/p&gt;
Performance Tab in Chrome Dev Tools



&lt;h3&gt;
  
  
  Prioritize issues and define the bigger impact
&lt;/h3&gt;

&lt;p&gt;Remember a couple of sections ago where we discussed &lt;em&gt;&lt;strong&gt;Identifying the main goal of your app&lt;/strong&gt;&lt;/em&gt;? Now that we have real data and know where the pain points are, it's easier to get an idea of which of those are affecting that goal. With that in mind, defining priorities and putting together a list of things that will have the bigger impact it's important since those wins will not only benefit users but also whatever we're trying to achieve with our app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Figure out how to fit performance into your planning (The good ol' 20%)
&lt;/h3&gt;

&lt;p&gt;As it happens commonly with tests during software development, we need to change the way we think about performance and think of it as &lt;em&gt;&lt;strong&gt;part&lt;/strong&gt;&lt;/em&gt; of the process instead of an extra. The best way to do this and make it part of the culture of our teams is to try to find a way to include it as part of our planning. A good idea is, at the beginning of every sprint, to leave 10-20% of our time to work on performance issues. If we make this an ongoing effort and part of our weekly work, not only it'll become something everyone will care about, but eventually, there will be fewer problems to attack and efforts will not be as significant.&lt;/p&gt;

&lt;p&gt;The good thing about performance improvements is that they're usually tied to wins for the business, so making a case to dedicate time to this is not as hard as other technical tasks like refactoring some code, improving tooling, and others.&lt;/p&gt;

&lt;h3&gt;
  
  
  Premature optimization
&lt;/h3&gt;

&lt;p&gt;Premature optimization is one of the big gotchas when thinking about performance. It's easy to get caught up in thinking too far ahead and trying to come up with the best way to handle cases we may never reach, so it's a good idea to not worry about it too much at the beginning, but at the same time, keep an eye on things and try to identify possible issues that could arise at some point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test everything and everywhere
&lt;/h3&gt;

&lt;p&gt;One of the big mistakes we usually make as developers is to test apps under the best conditions &lt;em&gt;(locally, in our computer, with a fast internet connection)&lt;/em&gt;. But we have to wonder, it's that the reality of our users? Probably not. That's why when testing it's a good idea to emulate different scenarios &lt;em&gt;(slower CPUs or network connections)&lt;/em&gt; and see how our app behaves. It's important to remember that things always look great under the best conditions, but it's only under some constraints that we start seeing the cracks 😔.&lt;/p&gt;

&lt;p&gt;Lucky for us, it's simple to simulate those conditions and test now, thanks to the Chrome Dev Tools:&lt;/p&gt;

&lt;h4&gt;
  
  
  Throttle CPU
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F225jvqiaf1q7tg9dfmx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F225jvqiaf1q7tg9dfmx1.png" alt="How to throttle CPU in Chrome Dev Tools"&gt;&lt;/a&gt;&lt;/p&gt;
How to throttle CPU in Chrome Dev Tools



&lt;h4&gt;
  
  
  Throttle Network
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fec9vm7pypa6buey0eyxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fec9vm7pypa6buey0eyxy.png" alt="How to throttle the network in Chrome Dev Tools"&gt;&lt;/a&gt;&lt;/p&gt;
How to throttle the network in Chrome Dev Tools



&lt;p&gt;Play around with those settings and reload your app, and that will allow you to see it under those simulated conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Those conditions are kept as long as &lt;em&gt;&lt;strong&gt;Chrome Dev Tools&lt;/strong&gt;&lt;/em&gt; is open. As soon as we close it, we'll go back to normal settings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get data about your users (devices, browsers) and see what they're using and what you should be thinking of
&lt;/h3&gt;

&lt;p&gt;With tools like &lt;a href="https://analytics.google.com/analytics/web/" rel="noopener noreferrer"&gt;Google Analytics&lt;/a&gt;, it's easier than ever to get a clear idea of not only where your users are, but also what are they are using when visiting your site: browsers, devices, operating systems, and even screen resolutions. Having that data will be your superpower to figure out what your focus should be and to avoid wasted efforts.&lt;/p&gt;

&lt;p&gt;For instance, if you collect data and realize that 100% of your users come to your site with the latest version of Chrome on Desktop, and no one is using other browsers or mobile devices, you could safely prioritize issues that are affecting them and not focus &lt;em&gt;(so much)&lt;/em&gt; on your mobile version or supporting old versions of other browsers. On the contrary, not having that data could mean spending countless hours fixing bugs that will probably have zero impact on your users.&lt;/p&gt;

&lt;h2&gt;
  
  
  And now: Let's get to work!
&lt;/h2&gt;

&lt;p&gt;And now that we have the data, the ideas and know what we need to focus on, it's time to get to work!. But, how do we do that? Well, that's for the next article ✌️.&lt;/p&gt;

&lt;p&gt;Originally published on my blog at &lt;a href="https://xabadu.dev/getting-started-with-web-performance/" rel="noopener noreferrer"&gt;xabadu.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>webperf</category>
    </item>
  </channel>
</rss>
