<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yassine Belmahfoud</title>
    <description>The latest articles on DEV Community by Yassine Belmahfoud (@belmasine).</description>
    <link>https://dev.to/belmasine</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%2F324934%2Ffaefa521-b45a-433a-88c2-45325538fd63.png</url>
      <title>DEV Community: Yassine Belmahfoud</title>
      <link>https://dev.to/belmasine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/belmasine"/>
    <language>en</language>
    <item>
      <title>🚀 7 JavaScript Performance Tricks Most Developers Don’t Use (But Should)</title>
      <dc:creator>Yassine Belmahfoud</dc:creator>
      <pubDate>Mon, 23 Jun 2025 12:57:01 +0000</pubDate>
      <link>https://dev.to/belmasine/7-javascript-performance-tricks-most-developers-dont-use-but-should-2608</link>
      <guid>https://dev.to/belmasine/7-javascript-performance-tricks-most-developers-dont-use-but-should-2608</guid>
      <description>&lt;h2&gt;
  
  
  1. Avoid Object Allocations Inside Loops
&lt;/h2&gt;

&lt;p&gt;Creating new objects in tight loops increases memory pressure and slows down garbage collection (GC).&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 1000; i++) {
  const obj = { value: i };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’re allocating 1,000 new objects — and GC has to clean them up.&lt;br&gt;
&lt;strong&gt;Better&lt;/strong&gt; (when reusability is possible):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {};
for (let i = 0; i &amp;lt; 1000; i++) {
  obj.value = i;
  // reuse obj
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: Less GC = faster execution and lower memory usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.  Cache Nested Property Access
&lt;/h2&gt;

&lt;p&gt;Repeated access to nested properties triggers multiple lookups.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const total = user.profile.details.orders.length;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This traverses multiple object layers every time.&lt;br&gt;
&lt;strong&gt;Better:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orders = user.profile.details.orders;
const total = orders.length;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: Property lookup in JavaScript is dynamic and can be expensive, especially inside loops.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.  Chaining map() + filter() Can Be Costly
&lt;/h2&gt;

&lt;p&gt;Yes, functional methods are elegant, but they often iterate more than needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = data.map(fn).filter(Boolean); // Two iterations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Faster alternative (for large arrays):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = [];
for (const item of data) {
  const transformed = fn(item);
  if (transformed) result.push(transformed);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: Combining into a single pass reduces time complexity and boosts performance on large datasets.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Don't Expect setTimeout(fn, 0) to Be Immediate
&lt;/h2&gt;

&lt;p&gt;Many devs use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(() =&amp;gt; { doSomething(); }, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;..expecting immediate execution. But browsers &lt;strong&gt;enforce a minimum delay&lt;/strong&gt;, often 4ms or more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Faster microtask alternatives&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.resolve().then(doSomething);
// or
queueMicrotask(doSomething);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: Microtasks run before the next rendering tick, ideal for ultra-fast UI reactions.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Lazy Load Expensive Code
&lt;/h2&gt;

&lt;p&gt;Large functions or libraries that aren't always used? Don’t import them by default.&lt;/p&gt;

&lt;p&gt;❌ Bad:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (condition) {
  heavyFn();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (condition) {
  import('./heavyFn.js').then(mod =&amp;gt; mod.default());
}

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

&lt;/div&gt;



&lt;p&gt;This is especially useful in SPAs or client-heavy React/Vue apps.&lt;/p&gt;

&lt;p&gt;✅ Why it matters: Reduces initial bundle size and improves Time to Interactive (TTI).&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Avoid forEach() in Performance-Critical Loops
&lt;/h2&gt;

&lt;p&gt;forEach() is elegant but slower than traditional loops.&lt;br&gt;
Benchmarks consistently show:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; arr.length; i++) { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...is faster than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.forEach(item =&amp;gt; { ... });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: With large arrays or intensive operations, for, for...of, or even while offer better performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Don’t Guess Performance — Measure It
&lt;/h2&gt;

&lt;p&gt;Developers often “optimize” based on assumptions. Don’t. Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("process");
// code block
console.timeEnd("process");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Recommended tools:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🔬 jsbench.me – create and share benchmarks&lt;/p&gt;

&lt;p&gt;🧪 Chrome DevTools &amp;gt; Performance tab – for real-world profiling&lt;/p&gt;

&lt;p&gt;📊 Lighthouse – for web performance audits&lt;br&gt;
✅ &lt;strong&gt;Why it matters&lt;/strong&gt;: Real profiling reveals hidden bottlenecks. You can't fix what you can't measure.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
