<?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: Emad Hassan Khan</title>
    <description>The latest articles on DEV Community by Emad Hassan Khan (@emadkhanqai).</description>
    <link>https://dev.to/emadkhanqai</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%2F1038328%2F28a0f891-0860-4b80-a3e5-a865ab26d641.jpeg</url>
      <title>DEV Community: Emad Hassan Khan</title>
      <link>https://dev.to/emadkhanqai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emadkhanqai"/>
    <language>en</language>
    <item>
      <title>Journey from 82289ms to 975ms: Optimizing a Heavy Query in .NET Core</title>
      <dc:creator>Emad Hassan Khan</dc:creator>
      <pubDate>Fri, 01 Mar 2024 20:14:28 +0000</pubDate>
      <link>https://dev.to/emadkhanqai/journey-from-82289ms-to-975ms-optimizing-a-heavy-query-in-net-core-4k8j</link>
      <guid>https://dev.to/emadkhanqai/journey-from-82289ms-to-975ms-optimizing-a-heavy-query-in-net-core-4k8j</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Introduction: The Quest for Speed&lt;/li&gt;
&lt;li&gt;Unveiling the Challenge: A 82289ms Monster&lt;/li&gt;
&lt;li&gt;Analyzing the Culprit: Understanding the Code&lt;/li&gt;
&lt;li&gt;The Road to Optimization: Strategies Employed&lt;/li&gt;
&lt;li&gt;Refactored Elegance: Witnessing the Transformation&lt;/li&gt;
&lt;li&gt;Lessons Learned: Insights and Reflections&lt;/li&gt;
&lt;li&gt;Conclusion: From Struggle to Success&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Introduction: The Quest for Speed&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the world of software development, optimization isn’t just a goal; it’s a necessity. Recently, I embarked on a journey to tame a beast of a query that was gobbling up precious milliseconds. What began as a daunting challenge ultimately turned into a triumph of efficiency and ingenuity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unveiling the Challenge: A 82289ms Monster&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The journey began when I encountered a query that seemed to defy the laws of efficiency. Clocking in at a staggering 82289ms, it was clear that drastic measures were needed. The culprit? A tangled web of Include and ThenInclude calls fetching a colossal number of rows from our SQL Server database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analyzing the Culprit: Understanding the Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Diving into the code, I meticulously dissected each line, searching for bottlenecks and inefficiencies. It became evident that the excessive data retrieval was the primary culprit behind the sluggish performance. With approximately 60k rows being fetched, it was no wonder the query was struggling to keep pace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Road to Optimization: Strategies Employed&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Armed with insight and determination, I set out to optimize the query. Employing a combination of techniques including eager loading, selective data retrieval, and caching, I systematically tackled each bottleneck head-on. Through careful analysis and experimentation, I fine-tuned the code, inching closer to my goal with each optimization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refactored Elegance: Witnessing the Transformation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The moment of truth arrived as I executed the refactored code. Anticipation hung in the air as the query sprung to life, executing with lightning speed. In a remarkable transformation, the execution time plummeted from 82289ms to a mere 975ms. The satisfaction of witnessing such a dramatic improvement was immeasurable.&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%2Fng3cu7njue188f20ph6z.jpeg" 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%2Fng3cu7njue188f20ph6z.jpeg" alt="Image description" width="800" height="1145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lessons Learned: Insights and Reflections&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The journey of optimizing a heavy query taught me valuable lessons that extend beyond mere technical proficiency. It underscored the importance of patience, perseverance, and a willingness to challenge conventional wisdom. It also highlighted the power of collaboration and knowledge sharing within the developer community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: From Struggle to Success&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, the story of optimizing a heavy query in .NET Core is a testament to the endless possibilities that lie within the realm of software development. While the road may be fraught with challenges, with the right tools, techniques, and mindset, any obstacle can be overcome. As we continue to push the boundaries of what’s possible, let us embrace each challenge as an opportunity for growth and innovation.&lt;/p&gt;

&lt;p&gt;Have you encountered similar challenges in your development journey? Share your experiences and insights in the comments below. Together, let’s celebrate the triumphs and tribulations that unite us as developers on a quest for excellence.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Don’t use filter method on Large Datasets!</title>
      <dc:creator>Emad Hassan Khan</dc:creator>
      <pubDate>Fri, 17 Mar 2023 08:12:00 +0000</pubDate>
      <link>https://dev.to/emadkhanqai/dont-use-filter-method-on-large-datasets-460d</link>
      <guid>https://dev.to/emadkhanqai/dont-use-filter-method-on-large-datasets-460d</guid>
      <description>&lt;p&gt;I was working on the application in which I faced a performance issue, the issue was related to the fact that I used a grid in Angular application and on the grid I was triggering a click event on the td element of the table.&lt;/p&gt;

&lt;p&gt;In the method attached to that td I was passing the index of the row and on the .ts file I used .filter method of array, later I realized that if the array size reaches to 5000 records then this will hit a performance bottleneck.&lt;/p&gt;

&lt;p&gt;So I decided to use the for loop instead of filter and it boosted my performance to the level I can’t imagine. Here’s the code for your reference:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3E1NCBYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tdnygg941x4kq28if3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3E1NCBYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tdnygg941x4kq28if3d.png" alt="Code Snippet" width="880" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Cache API calls in Angular using Interceptor</title>
      <dc:creator>Emad Hassan Khan</dc:creator>
      <pubDate>Sat, 04 Mar 2023 09:27:12 +0000</pubDate>
      <link>https://dev.to/aws-builders/cache-api-calls-in-angular-using-interceptor-5b72</link>
      <guid>https://dev.to/aws-builders/cache-api-calls-in-angular-using-interceptor-5b72</guid>
      <description>&lt;p&gt;Developing web applications can be a challenging task, and among the many issues we face, one of the most common ones is the repetitive API calls that occur every time a component initializes. This can lead to a great deal of pain and frustration, as well as excessive calls that can slow down the performance of your application.&lt;/p&gt;

&lt;p&gt;But fear not, there is a strategy that can help you save multiple API hits for the same endpoint. By making use of an Interceptor in your Angular application, you can streamline your code and improve the overall efficiency of your web app.&lt;/p&gt;

&lt;p&gt;In the code snippet below, we will demonstrate how to implement this strategy by simply mentioning the endpoints we want to cache and letting the interceptor do its job. This will not only save you time and energy, but also make your application more responsive and user-friendly.&lt;/p&gt;

&lt;p&gt;So without further ado, let’s dive in and discover how to implement this powerful tool in your web development process.&lt;/p&gt;

&lt;p&gt;Here’s the code:&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%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A720%2Fformat%3Awebp%2F1%2ARjXqZtOygBzeAGbD4ywetA.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%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A720%2Fformat%3Awebp%2F1%2ARjXqZtOygBzeAGbD4ywetA.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the interceptor is created don’t forget to reference it in app.module.ts&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%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A720%2Fformat%3Awebp%2F1%2AXSKXsjEIN9KYQ1uLulJzFg.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%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A720%2Fformat%3Awebp%2F1%2AXSKXsjEIN9KYQ1uLulJzFg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, lets catch the code explanation:&lt;/p&gt;

&lt;p&gt;We have an Angular interceptor called ApiCacheInterceptor that can cache HTTP responses from certain API endpoints. The interceptor uses the HttpInterceptor class and its intercept() method to handle all HTTP requests and responses.&lt;/p&gt;

&lt;p&gt;When a request is intercepted, the interceptor checks if the request endpoint is in a predefined set of endpoints to cache. If the endpoint is in the set, the interceptor checks if the response is already cached. If the response is cached, the interceptor returns the cached response. If the response is not cached, the interceptor sends the request to the server, caches the response, and returns the response to the original caller.&lt;/p&gt;

&lt;p&gt;The ApiCacheInterceptor class defines two private properties: cache, which is a Map object that stores cached responses, and endpointsToCache, which is a Set object that lists the endpoints to cache. The intercept() method uses these properties to implement the caching logic.&lt;/p&gt;

&lt;p&gt;If you found this article informative and helpful, make sure to follow my account for more insightful and useful posts on various topics related to web development, technology, and much more. By following, you’ll be the first to know about my latest articles, tips, and tricks, and stay up to date with the latest trends in the industry.&lt;/p&gt;

&lt;p&gt;I am dedicated to providing high-quality content that is easy to understand and helpful for readers of all levels of expertise. Whether you’re a beginner or an experienced developer, you’ll find my posts to be informative, engaging, and relevant to your interests.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
