<?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: Nikita Galkin</title>
    <description>The latest articles on Forem by Nikita Galkin (@galkin).</description>
    <link>https://forem.com/galkin</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%2F28917%2Fefa4ec0e-6974-41c5-b913-4b85a039a574.png</url>
      <title>Forem: Nikita Galkin</title>
      <link>https://forem.com/galkin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/galkin"/>
    <language>en</language>
    <item>
      <title>Why is NODE_ENV=beta an error?</title>
      <dc:creator>Nikita Galkin</dc:creator>
      <pubDate>Mon, 01 Aug 2022 03:52:08 +0000</pubDate>
      <link>https://forem.com/aws-builders/why-is-nodeenvbeta-an-error-1mig</link>
      <guid>https://forem.com/aws-builders/why-is-nodeenvbeta-an-error-1mig</guid>
      <description>&lt;p&gt;The third factor of &lt;a href="https://12factor.net/"&gt;The Twelve Factors App&lt;/a&gt; says: &lt;em&gt;Store config in the environment&lt;/em&gt;. NODE_ENV is an example of following this factor. The variable name says that it defines the environment for Node.js, not your application. It's common practice to prefix environment variables. For example, AWS SDK configures by AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. &lt;/p&gt;

&lt;p&gt;Examples of how the behavior of the JavaScript ecosystem changes with NODE_ENV=production:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm install command will not install devDepencencies&lt;/li&gt;
&lt;li&gt;express.js caches templates and css&lt;/li&gt;
&lt;li&gt;Apollo Server disables schema introspection &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;You should not use NODE_ENV to determine the name of your environment.&lt;/strong&gt; But which env var to use instead of NODE_ENV?  The answer depends on whether you will use a prefix for business logic-related variables. You can think about these variables as feature flags.&lt;/p&gt;

&lt;p&gt;Example with prefix FOO:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FOO_ENV=beta
FOO_FEATURE_FLAG=on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENV=beta
FEATURE_FLAG=on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's get back to NODE_ENV. Usually, it has one of these values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;development&lt;/em&gt; – used if not defined explicitly. Set this value during development at your computer.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;test&lt;/em&gt; – test frameworks like Jest automatically set this value.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;production&lt;/em&gt; – use this value during run code at the server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tenth factor is &lt;em&gt;Dev/prod parity&lt;/em&gt;. Therefore, it right approach to use NODE_ENV=production in every business environment. Dockerfile is the simplest place for doing this. Just add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENV NODE_ENV=production
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Use NODE_ENV with development|test|production&lt;/li&gt;
&lt;li&gt;Define your ENV to determine your environment&lt;/li&gt;
&lt;li&gt;In Dockerfile add ENV NODE_ENV=production&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>node</category>
      <category>devops</category>
      <category>cloud</category>
      <category>12factor</category>
    </item>
    <item>
      <title>crypto.randomUUID is three times faster uuid.v4</title>
      <dc:creator>Nikita Galkin</dc:creator>
      <pubDate>Wed, 19 May 2021 00:36:30 +0000</pubDate>
      <link>https://forem.com/galkin/crypto-randomuuid-vs-uuid-v4-47i5</link>
      <guid>https://forem.com/galkin/crypto-randomuuid-vs-uuid-v4-47i5</guid>
      <description>&lt;p&gt;Node.js v14.17 release added &lt;code&gt;crypto.randomUUID()&lt;/code&gt;. This method allows to generate random &lt;a href="https://www.rfc-editor.org/rfc/rfc4122.txt"&gt;RFC 4122&lt;/a&gt; Version 4 UUID strings. Example:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;randomUUID&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;crypto&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// '43c98ac2-8493-49b0-95d8-de843d90e6ca'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wondered how big the difference between uuid generation by Node.js API and uuid package.&lt;/p&gt;

&lt;p&gt;For benchmarking I prefer to use &lt;a href="https://github.com/sharkdp/hyperfine"&gt;hyperfine&lt;/a&gt;. It is like apache benchmark, but for CLI commands. There are have two cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;require('crypto').randomUUID()&lt;/li&gt;
&lt;li&gt;require('uuid').v4()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's put them into two files:&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="c1"&gt;// test-native.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;randomUUID&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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;randomUUID&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// test-uuid.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;v4&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;uuid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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;v4&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;Now we ready for benchmarking:&lt;br&gt;
&lt;code&gt;hyperfine 'node test-native.js' 'node test-uuid.js'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command shows that native generation is three times faster than &lt;code&gt;uuid&lt;/code&gt; package. Awesome!&lt;/p&gt;

</description>
      <category>node</category>
      <category>benchmark</category>
    </item>
  </channel>
</rss>
