<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>JavaScript Archives - Ryan Peden</title>
	<atom:link href="https://ryanpeden.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://ryanpeden.com/category/javascript/</link>
	<description>Full Stack DevRel</description>
	<lastBuildDate>Tue, 21 Jan 2020 14:50:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.3</generator>
<site xmlns="com-wordpress:feed-additions:1">155423463</site>	<item>
		<title>How to find the version of a globally installed NPM package</title>
		<link>https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/</link>
					<comments>https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/#respond</comments>
		
		<dc:creator><![CDATA[Ryan Peden]]></dc:creator>
		<pubDate>Tue, 21 Jan 2020 14:49:44 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">https://ryanpeden.com/?p=171</guid>

					<description><![CDATA[<p>Sometimes, you really want to know the version of a globally installed NPM package. That&#8217;s the position I found myself in this morning. Googling didn&#8217;t help me much. Most of the answers I found just didn&#8217;t answer my question. Here what you need to know. If you want to see</p>
<div class="belowpost"><a class="btnmore icon-arrow" href="https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/"><span>Read More</span></a></div>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/">How to find the version of a globally installed NPM package</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Sometimes, you <em>really</em> want to know the version of a globally installed NPM package. That&#8217;s the position I found myself in this morning. Googling didn&#8217;t help me much. Most of the answers I found just didn&#8217;t answer my question. </p>



<p>Here what you need to know. If you want to see the version of a globally installed NPM package, run this:</p>



<p><code>npm ls -g package-name --depth 0</code></p>



<p>Replace <code>package-name</code> with the name of whatever package you&#8217;re looking for. </p>



<p>In most cases, adding <code>--depth 0</code> isn&#8217;t necessary. But in rare cases, the global package you&#8217;re searching for is <em>also</em> a dependency of other globally installed packages. When this happens, you&#8217;ll see the package you want, but you&#8217;ll also see a partial dependency tree of all of the packages that <em>use</em> the package you&#8217;re searching for. </p>



<p>Adding <code>--depth 0</code> eliminates this problem. Feel free to leave that part out and only add it if you need it. </p>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/">How to find the version of a globally installed NPM package</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ryanpeden.com/how-to-find-the-version-of-a-globally-installed-npm-package/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">171</post-id>	</item>
		<item>
		<title>JavaScript Array Push Performance</title>
		<link>https://ryanpeden.com/array-prototype-push-performance/</link>
					<comments>https://ryanpeden.com/array-prototype-push-performance/#respond</comments>
		
		<dc:creator><![CDATA[Ryan Peden]]></dc:creator>
		<pubDate>Mon, 07 Oct 2019 16:37:33 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://ryanpeden.com/?p=7</guid>

					<description><![CDATA[<p>This is a short response I wrote to a question on /r/javascript. The user who asked it was curious whether there would be any performance difference between adding elements to a JavaScript array by calling push, or manually adding a new object to an array by making a call like&#160;myArray[myArray.length]</p>
<div class="belowpost"><a class="btnmore icon-arrow" href="https://ryanpeden.com/array-prototype-push-performance/"><span>Read More</span></a></div>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/array-prototype-push-performance/">JavaScript Array Push Performance</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This is a short response I wrote to <a href="https://www.reddit.com/r/javascript/comments/a3qxjj/hello_all_im_a_jr_dev_and_ive_never_dealt_with/">a question on /r/javascript</a>. The user who asked it was curious whether there would be any performance difference between adding elements to a JavaScript array by calling <code>push</code>, or manually adding a new object to an array by making a call like&nbsp;<code>myArray[myArray.length] = obj</code>. Let&#8217;s take a look at the <a href="https://www.ecma-international.org/ecma-262/6.0/">ECMAScript specification</a> to see what it says.&nbsp;&nbsp;</p>



<p>In the case of&nbsp;<a rel="noreferrer noopener" target="_blank" href="https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.push">Array.prototype.push</a>, the JS runtime must first call&nbsp;<code>toObject</code>&nbsp;on the argument passed to&nbsp;<code>push</code>. It must also do a bit of work to handle the case where more than one item was passed to&nbsp;<code>push</code>, since you are allowed to make a call like this:&nbsp;<code>abc.push(1,2,3)</code>. After from calling&nbsp;<code>toObject</code>&nbsp;and checking how many arguments were provided, it then goes through each one and does a regular property set call, which ends doing the same as&nbsp;<code>myArray[myArray.length] = obj</code>.</p>



<p>If you&#8217;re only adding one thing to your array, you may as well call&nbsp;<code>push</code>, since it is easier to read and the&nbsp;<code>toObject</code>&nbsp;call and args length check is going to make an immeasurably small difference to execution time.</p>



<p>If you&#8217;re adding multiple things to your array, then call&nbsp;<code>myArray.push(...things)</code>. because when you do that, the JS engine&#8217;s compiled C++ will handle all of the iteration, instead of thunking back and forth between native code and JavaScript if you&#8217;re looping through yourself and calling&nbsp;<code>push</code>&nbsp;every time. In reality, with all of the optimization and JITing that modern JS engines do, looping through yourself probably isn&#8217;t all that much slower than passing everything to&nbsp;<code>push</code>at once. I haven&#8217;t tested this to verify, though.</p>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/array-prototype-push-performance/">JavaScript Array Push Performance</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ryanpeden.com/array-prototype-push-performance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7</post-id>	</item>
		<item>
		<title>JavaScript indexOf vs includes performance</title>
		<link>https://ryanpeden.com/javascript-indexof-vs-includes-performance/</link>
					<comments>https://ryanpeden.com/javascript-indexof-vs-includes-performance/#comments</comments>
		
		<dc:creator><![CDATA[Ryan Peden]]></dc:creator>
		<pubDate>Wed, 28 Aug 2019 12:02:16 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://ryanpeden.com/?p=21</guid>

					<description><![CDATA[<p>This is a short response I wrote to&#160;a question on /r/javascript. The user who asked it was curious whether there would be any performance difference between using a JavaScript string&#8217;s indexOf vs includes performance when trying to find a substring within a larger string.&#160; Let’s take a look at the&#160;ECMAScript</p>
<div class="belowpost"><a class="btnmore icon-arrow" href="https://ryanpeden.com/javascript-indexof-vs-includes-performance/"><span>Read More</span></a></div>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/javascript-indexof-vs-includes-performance/">JavaScript indexOf vs includes performance</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This is a short response I wrote to&nbsp;<a href="https://www.reddit.com/r/javascript/comments/a3qxjj/hello_all_im_a_jr_dev_and_ive_never_dealt_with/">a question on /r/javascript</a>. The user who asked it was curious whether there would be any performance difference between using a JavaScript string&#8217;s <code>indexOf</code> vs <code>includes</code> performance when trying to find a substring within a larger string.&nbsp;</p>



<p>Let’s take a look at the&nbsp;<a href="https://www.ecma-international.org/ecma-262/6.0/">ECMAScript specification</a>&nbsp;to see what it says.&nbsp;&nbsp;</p>



<p>In the case of&nbsp;<code>indexOf</code>&nbsp;vs&nbsp;<code>includes</code>, you&#8217;ll find them right next to each other&nbsp;<a rel="noreferrer noopener" target="_blank" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.includes">here in the ECMAScript spec</a>. The only difference is that&nbsp;<code>includes</code>&nbsp;checks if you&#8217;ve passed it a regular expression instead of a string, and throws an exception if you have.&nbsp;<code>indexOf</code>&nbsp;will accept a regular expression but always return -1, which isn&#8217;t too helpful.</p>



<p>So while&nbsp;<code>includes</code>&nbsp;will be a tiny, tiny amount slower because it has to check if you passed it a regex, in reality this will make no difference to how fast your code runs. You should use&nbsp;<code>indexOf</code>&nbsp;if you care about&nbsp;<em>where</em>&nbsp;the substring is in the original string. If you don&#8217;t care, just call&nbsp;<code>includes</code>&nbsp;because it makes the intent of your code more clear.</p>



<p>You should also consider that&nbsp;<code>indexOf</code>&nbsp;won&#8217;t find the location of&nbsp;<code>NaN</code>&nbsp;values in an Array. For cases where you really need to find the first location of something that&nbsp;<code>indexOf</code>&nbsp;can&#8217;t find, and you don&#8217;t want to loop the array through yourself, then&nbsp;<code>Array.prototype.findIndex</code>&nbsp;is your friend. You can pass it a function that returns true when it sees the value you&#8217;re looking for. So&nbsp;<code>[1, 2, 3, NaN, 4].findIndex(Number.isNaN)</code>&nbsp;would return 3.</p>



<p>The verdict: in most cases, in terms of JavaScript <code>indexOf</code> vs <code>include</code> performance it doesn&#8217;t matter much whether you use index vs includes. You should pick the one that&#8217;s the clearest fit for the problem you&#8217;re trying to solve. </p>



<p><strong>If you liked this, you might like these other articles I&#8217;ve written:</strong><br><a href="https://www.grapecity.com/blogs/using-web-components-with-react-2019">Using Web Components with React in 2019</a> <br><a href="https://www.grapecity.com/blogs/angular-roadmap-the-past-present-and-future-of-angular">An Angular Roadmap &#8211; The Past, Present, and Future of Angular</a> <br><a href="https://ryanpeden.com/how-do-javascript-arrays-work-under-the-hood/">How do JavaScript Arrays Work Under the Hood?     </a>                  </p>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/javascript-indexof-vs-includes-performance/">JavaScript indexOf vs includes performance</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ryanpeden.com/javascript-indexof-vs-includes-performance/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">21</post-id>	</item>
		<item>
		<title>How do JavaScript arrays work under the hood?</title>
		<link>https://ryanpeden.com/how-do-javascript-arrays-work-under-the-hood/</link>
		
		<dc:creator><![CDATA[Ryan Peden]]></dc:creator>
		<pubDate>Sat, 27 Jul 2019 14:30:00 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://ryanpeden.com/?p=23</guid>

					<description><![CDATA[<p>Front-end and full-stack developers use JavaScript arrays every day. Most of them, however, haven&#8217;t done a deep dive to understand how JS arrays are implemented in native code. Not doing this doesn&#8217;t mean you&#8217;re a bad developer. It&#8217;s entirely possible to use JavaScript effectively without digging in to understand how</p>
<div class="belowpost"><a class="btnmore icon-arrow" href="https://ryanpeden.com/how-do-javascript-arrays-work-under-the-hood/"><span>Read More</span></a></div>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/how-do-javascript-arrays-work-under-the-hood/">How do JavaScript arrays work under the hood?</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Front-end and full-stack developers use JavaScript arrays every day. Most of them, however, haven&#8217;t done a deep dive to understand how JS arrays are implemented in native code. </p>



<p>Not doing this doesn&#8217;t mean you&#8217;re a bad developer. It&#8217;s entirely possible to use JavaScript effectively without digging in to understand how JS arrays are implemented. But taking a closer look can help you understand performance issues your might run into, so it&#8217;s a worthwhile exercise. </p>



<p>How exactly they are implemented depends on the specific JavaScript interpreter or VM you&#8217;re using. I&#8217;m going to use the V8 JavaScript engine (used by Chrme and Node.js) as an example. Other JavaScript engines, such as Mozilla&#8217;s SpiderMonkey and Microsoft&#8217;s Chakra will be similar but not identical.</p>



<h2>JavaScript Arrays in V8</h2>



<p>In V8, if your array only contains integers, it&#8217;ll be backed by a C++ array of integers. Typically, the backing array will be bigger than the number of integers it currently contains. If it contains a mixture of integers and floating point values or only floating point values, it&#8217;ll be backed by an array of doubles. </p>



<p>If the array contains only objects, or a mixture of numbers and objects, it&#8217;ll backed by an array of pointers. Even though JavaScript itself doesn&#8217;t have a concept of &#8216;integer&#8217; or &#8216;double&#8217; &#8211; it just sees them all as &#8216;number&#8217;, V8 keeps track and makes it so arrays are a bit faster and more memory efficient if you only put integers in them.</p>



<p>If you call&nbsp;<code>push()</code>&nbsp;when the backing array is full, it&#8217;ll allocate a new, bigger backing array, copy the existing elements over, and then add the new value you pushed. This is similar to the implementation of&nbsp;<code>ArrayList</code>&nbsp;in Java or&nbsp;<code>vector</code>&nbsp;in C++.</p>



<p>All of the above only is only sure to apply if your array is packed, and not sparse &#8211; i.e. you don&#8217;t have any gaps in the array. If you do something like</p>



<pre class="wp-block-code"><code>let abc = [1,2,3];
abc[100] = 50;</code></pre>



<p>you now have a sparse array. If is not too spare, it&#8217;ll still be backed by an array, with empty array indices replaced with a &#8216;hole&#8217; value. If you look at V8&#8217;s C++ array source (linked below), you&#8217;ll see calls to&nbsp;<code>element-&gt;is_the_hole(i)</code>. If an array is very sparse, it&#8217;ll no longer be backed by an array in memory. Instead, it will be backed by a dictionary/hashtable, and it&#8217;ll take longer to both access elements and iterate through the array.</p>



<p>If you&#8217;re interested, you can read through V8&#8217;s array implementation in C++&nbsp;<a href="https://github.com/v8/v8/blob/master/src/builtins/builtins-array.cc">here</a>. You&#8217;ll notice that it often checks the following constants:</p>



<ul><li><code>PACKED_SMI_ELEMENTS</code> &#8211; a packed integer array</li><li><code>PACKED_DOUBLE_ELEMENTS</code> &#8211; a packed double array</li><li><code>PACKED_ELEMENTS</code> &#8211; a packed object array</li><li><code>HOLEY_SMI_ELEMENTS</code> &#8211; a sparse integer array</li><li><code>HOLEY_DOUBLE_ELEMENTS</code> &#8211; a sparse double array</li><li><code>HOLEY_ELEMENTS</code> &#8211; a sparse object array</li><li><code>DICTIONARY_ELEMENTS</code> &#8211; a very sparse array that is backed by a dictionary</li></ul>



<p>And you&#8217;ll see that it always tries to do whatever will be fastest for the array it is operating on. Lots of builtin functions like push, pop, shift, unshift, and concat do different things depending on the array&#8217;s density and what kind of elements it contains.</p>



<p>Some other things to keep in mind: if you have an array that only contains integers, and you push a floating point number or other type into it, it will be &#8216;downgraded&#8217; for the rest of its life, even if you purge the non integers from it.</p>



<p>Also keep in mind that none of these implementation details are guaranteed. A naive implementation of JavaScript&#8217;s Array object could be backed by a linked list, and it would still work the same way it does now. It would just be slower. </p>



<p>Actually, if you grab an early copy of the Mozilla source code from 20 years ago, you&#8217;ll find that arrays were backed by ordinary JS objects without much optimization, just some extra code to handle special cases like the `length` property.</p>



<h2>Digging Deeper</h2>



<p>If you&#8217;re interested in diving even deeper into V8&#8217;s internals, I suggest starting by reading through the source files I linked to above. If you&#8217;re not used to C++, understanding it will be a bit of a chore at first. </p>



<p>I recommend sticking with it, though. Learning and understanding a new language is always good &#8211; and every extra insight you gain into how JavaScript VMs run your code will make you a more effective JavaScript developer. </p>



<p><strong>If you liked this, you might like these other articles I&#8217;ve written:</strong><br><a href="https://www.grapecity.com/blogs/using-web-components-with-react-2019">Using Web Components with React in 2019</a><br><a href="https://www.grapecity.com/blogs/angular-roadmap-the-past-present-and-future-of-angular">An Angular Roadmap &#8211; The Past, Present, and Future of Angular</a></p>
<p>The post <a rel="nofollow" href="https://ryanpeden.com/how-do-javascript-arrays-work-under-the-hood/">How do JavaScript arrays work under the hood?</a> appeared first on <a rel="nofollow" href="https://ryanpeden.com">Ryan Peden</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">23</post-id>	</item>
	</channel>
</rss>
