{"id":4759,"date":"2026-04-13T21:50:27","date_gmt":"2026-04-13T16:20:27","guid":{"rendered":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/"},"modified":"2026-04-13T21:50:27","modified_gmt":"2026-04-13T16:20:27","slug":"javascript-best-practices-write-cleaner-faster-code","status":"publish","type":"post","link":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/","title":{"rendered":"JavaScript Best Practices: Write Cleaner, Faster Code"},"content":{"rendered":"<p>INCIDENT REPORT #882-B: Why our migration to the &#8216;Modern Stack&#8217; nearly bankrupted the company.<\/p>\n<p><strong>DATE:<\/strong> October 14, 2023<br \/>\n<strong>TO:<\/strong> Executive Steering Committee, Engineering Leads<br \/>\n<strong>FROM:<\/strong> Lead Architect (Recovery Team)<br \/>\n<strong>SUBJECT:<\/strong> Post-Mortem of &#8220;Project Phoenix&#8221; (The Great Migration)<br \/>\n<strong>TOTAL LOSS:<\/strong> $50,420,000 USD (Estimated)<br \/>\n<strong>TIMELINE:<\/strong> January 2022 \u2013 June 2023<\/p>\n<hr \/>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69de02214305a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69de02214305a\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#1_Incident_Report_Summary\" >1. Incident Report Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#2_The_Microservices_Mirage_and_the_Distributed_Monolith\" >2. The Microservices Mirage and the Distributed Monolith<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#3_The_Barrel_File_Bottleneck_and_the_Nodejs_Startup_Death_Spiral\" >3. The Barrel File Bottleneck and the Node.js Startup Death Spiral<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#4_Memory_Leaks_and_the_V8_Garbage_Collector_Nightmare\" >4. Memory Leaks and the V8 Garbage Collector Nightmare<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#5_The_any_Pandemic_and_the_TypeScript_Lie\" >5. The any Pandemic and the TypeScript Lie<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#6_Redux_Deep_Cloning_and_the_State_Management_Bloat\" >6. Redux, Deep Cloning, and the State Management Bloat<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#7_Dependency_Hell_and_the_2GB_node_modules_Folder\" >7. Dependency Hell and the 2GB node_modules Folder<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#8_The_Cost_of_%E2%80%9CClever%E2%80%9D_Code\" >8. The Cost of &#8220;Clever&#8221; Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#Conclusion_What_%E2%80%9Cjavascript_best%E2%80%9D_Actually_Means\" >Conclusion: What &#8220;javascript best&#8221; Actually Means<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#Related_Articles\" >Related Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"1_Incident_Report_Summary\"><\/span>1. Incident Report Summary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Project Phoenix was supposed to be our &#8220;digital transformation.&#8221; We took a stable, boring, revenue-generating Node.js monolith and decided to &#8220;modernize&#8221; it. The goal was to move to a distributed microservices architecture using every buzzword that trended on Twitter in 2021. <\/p>\n<p>By Q3 2022, the system was effectively dead. We were spending $450,000 a month on AWS bills for a system that handled 40% less traffic than the old monolith. The &#8220;Modern Stack&#8221; was a house of cards built on top of shifting sand. We saw 99th percentile latency spike from 200ms to 4,500ms. We lost $12M in direct sales during the Black Friday window because the &#8220;Event-Driven Architecture&#8221; resulted in a circular dependency that deadlocked our entire inventory database.<\/p>\n<p>The following report outlines the technical idiocy that led to this failure. I have spent the last 18 months gutting the &#8220;clever&#8221; code and replacing it with things that actually work. If you are looking for a &#8220;javascript best&#8221; practices guide that involves 15 layers of abstraction, close this tab. This is about survival.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"2_The_Microservices_Mirage_and_the_Distributed_Monolith\"><\/span>2. The Microservices Mirage and the Distributed Monolith<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The first mistake was the &#8220;Microservices First&#8221; mandate. We took a perfectly functional business logic layer and sliced it into 42 separate repositories. Each service had its own boilerplate, its own CI\/CD pipeline, and its own set of bugs. <\/p>\n<p>We were told this would &#8220;decouple&#8221; the teams. Instead, it created a distributed monolith where no single service could run without six others being online. We replaced a local function call\u2014which takes nanoseconds\u2014with an HTTP\/2 request that took 50ms, plus DNS lookup, plus TLS handshake, plus the overhead of the &#8220;Service Mesh.&#8221;<\/p>\n<p><strong>The Over-Engineered Mess:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">\/\/ service-inventory\/src\/middleware\/auth-wrapper.ts\n\/\/ This was repeated in 42 services.\nimport { AuthService } from '@company\/internal-sdk'; \n\nexport const validateRequest = async (req, res, next) =&gt; {\n  try {\n    const token = req.headers.authorization;\n    \/\/ Every single internal request triggered an external API call\n    \/\/ to an Auth service that was already struggling.\n    const user = await AuthService.verify(token); \n    req.user = user;\n    next();\n  } catch (e) {\n    res.status(401).json({ error: 'Unauthorised' });\n  }\n};\n<\/code><\/pre>\n<p><strong>The Correction:<\/strong><br \/>\nWe moved back to a modular monolith. We used a simple, shared library for JWT verification that didn&#8217;t require a network hop for every single internal call.<\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">\/\/ core\/auth.js\nconst jwt = require('jsonwebtoken');\nconst PUBLIC_KEY = process.env.JWT_PUBLIC_KEY;\n\n\/\/ Simple, synchronous, fast.\nexports.verifyToken = (token) =&gt; {\n  return jwt.verify(token, PUBLIC_KEY, { algorithms: ['RS256'] });\n};\n<\/code><\/pre>\n<p>We stopped pretending we were Google. We don&#8217;t have 10,000 engineers. We have 50. A monolith is not a &#8220;legacy&#8221; pattern; it is a &#8220;we want to actually ship features&#8221; pattern.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"3_The_Barrel_File_Bottleneck_and_the_Nodejs_Startup_Death_Spiral\"><\/span>3. The Barrel File Bottleneck and the Node.js Startup Death Spiral<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>By Q4 2022, our Lambda functions were timing out before they even started executing code. We were using Node.js v20.11.1, and our &#8220;Cold Start&#8221; times were exceeding 10 seconds. Why? Because some &#8220;Senior&#8221; developer thought it would be &#8220;clean&#8221; to use barrel files (<code>index.ts<\/code>) for everything.<\/p>\n<p>Every time we imported one utility function, Node.js had to crawl the entire file tree, parsing thousands of lines of code that weren&#8217;t even being used. V8 spent more time in the <code>Loading<\/code> phase than the <code>Executing<\/code> phase.<\/p>\n<p><strong>The Terminal Log of Shame:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-bash\">$ node --trace-event-categories v8,node.module_timer index.js\n[2.451s] node:internal\/modules\/cjs\/loader:452: load_request_event_start\n[8.922s] node:internal\/modules\/cjs\/loader:890: load_request_event_end\n# Total startup time: 9.2 seconds. \n# 85% of time spent parsing unused barrel files.\n<\/code><\/pre>\n<p>The &#8220;javascript best&#8221; approach according to the &#8220;clean code&#8221; crowd is to have an <code>index.ts<\/code> in every folder. In reality, this is a performance suicide note.<\/p>\n<p><strong>The Correction:<\/strong><br \/>\nWe banned barrel files. We imported exactly what we needed from the specific file.<\/p>\n<p><strong>Before:<\/strong><br \/>\n<code>import { formatDate } from '@\/utils';<\/code> \/\/ This imports 400 other utilities and 20 heavy libraries.<\/p>\n<p><strong>After:<\/strong><br \/>\n<code>import { formatDate } from '@\/utils\/date-formatter.js';<\/code> \/\/ Imports exactly 15 lines of code.<\/p>\n<p>Startup time dropped from 9 seconds to 400ms. Stop making the V8 engine do work it doesn&#8217;t need to do.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"4_Memory_Leaks_and_the_V8_Garbage_Collector_Nightmare\"><\/span>4. Memory Leaks and the V8 Garbage Collector Nightmare<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In March 2023, the production API started crashing every 4 hours. We looked at the metrics. Memory usage was a literal staircase. We were hitting the 4GB heap limit on our containers and getting OOMKilled.<\/p>\n<p>The culprit? A &#8220;clever&#8221; use of <code>WeakMap<\/code> and <code>Proxy<\/code> objects to build a &#8220;reactive&#8221; caching layer that was supposed to &#8220;automatically&#8221; invalidate data. The developers thought they were being smart. They were actually just preventing the V8 Garbage Collector from doing its job.<\/p>\n<p><strong>The Heap Snapshot Analysis:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-text\">Snapshot 1: 150MB\nSnapshot 2: 850MB (after 1 hour)\nSnapshot 3: 2.4GB (after 2 hours)\n\nTop Retainers:\n(array) @123456 - 45% of heap\n  -&gt; Proxy @78901\n    -&gt; Map @11223\n      -&gt; &quot;massive_api_response_string&quot;\n<\/code><\/pre>\n<p>The V8 engine&#8217;s &#8220;Scavenger&#8221; (Young Generation GC) was running every 2 seconds, consuming 30% of the CPU, trying to find memory it could free. But because of the circular references in the Proxy-based cache, the &#8220;Mark-Sweep-Compact&#8221; (Old Generation GC) couldn&#8217;t reclaim anything.<\/p>\n<p><strong>The &#8220;Clever&#8221; (Broken) Code:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">const cache = new WeakMap();\n\n\/\/ This was supposed to &quot;magically&quot; clean up.\n\/\/ It didn't, because the keys were objects that never went out of scope.\nexport const getCachedData = (keyObject, fetcher) =&gt; {\n  if (cache.has(keyObject)) return cache.get(keyObject);\n\n  const data = fetcher();\n  const proxy = new Proxy(data, {\n    get(target, prop) {\n      console.log(`Accessing ${prop}`);\n      return target[prop];\n    }\n  });\n\n  cache.set(keyObject, proxy);\n  return proxy;\n};\n<\/code><\/pre>\n<p><strong>The Correction:<\/strong><br \/>\nWe deleted the &#8220;reactive&#8221; cache. We used a standard LRU (Least Recently Used) cache with a fixed size.<\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">const { LRUCache } = require('lru-cache');\nconst cache = new LRUCache({ max: 500, ttl: 1000 * 60 * 5 });\n\nexport const getCachedData = async (key, fetcher) =&gt; {\n  const cached = cache.get(key);\n  if (cached) return cached;\n\n  const data = await fetcher();\n  cache.set(key, data);\n  return data;\n};\n<\/code><\/pre>\n<p>The memory staircase vanished. The CPU usage dropped by 40%. Don&#8217;t try to outsmart the V8 engine. It has been optimized by people much smarter than you. If you use a Proxy in a high-throughput path, you are probably making a mistake.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"5_The_any_Pandemic_and_the_TypeScript_Lie\"><\/span>5. The <code>any<\/code> Pandemic and the TypeScript Lie<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We were told TypeScript would make the code &#8220;safe.&#8221; Instead, the team used <code>any<\/code> every time they encountered a slightly complex type. We had 4,200 instances of <code>any<\/code> in the codebase. It was just JavaScript with extra steps and longer compile times.<\/p>\n<p>The worst part was the &#8220;Type Casting&#8221; of API responses. We were casting raw JSON from external services directly into interfaces without any validation.<\/p>\n<p><strong>The Disaster Code:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-typescript\">interface UserProfile {\n  id: string;\n  email: string;\n  settings: {\n    theme: 'light' | 'dark';\n  };\n}\n\nconst loadProfile = async (id: string): Promise&lt;UserProfile&gt; =&gt; {\n  const response = await fetch(`\/api\/users\/${id}`);\n  const data = await response.json();\n  return data as UserProfile; \/\/ The &quot;Lie&quot;\n};\n\n\/\/ Somewhere else in the code...\nconst profile = await loadProfile('123');\nconsole.log(profile.settings.theme); \/\/ CRASH: Cannot read property 'theme' of undefined\n<\/code><\/pre>\n<p>When the external API changed its response format, the entire frontend and backend crashed because we trusted the &#8220;type.&#8221; This cost us $2M in lost orders over a single weekend.<\/p>\n<p><strong>The Correction:<\/strong><br \/>\nWe enforced a strict &#8220;No <code>any<\/code>&#8221; rule in <code>tsconfig.json<\/code>. We introduced Zod for runtime validation. If the data doesn&#8217;t match the schema at the boundary, the system fails gracefully with a logged error, rather than exploding deep in the business logic.<\/p>\n<pre class=\"codehilite\"><code class=\"language-typescript\">import { z } from 'zod';\n\nconst UserProfileSchema = z.object({\n  id: z.string(),\n  email: z.string().email(),\n  settings: z.object({\n    theme: z.enum(['light', 'dark']),\n  }),\n});\n\nconst loadProfile = async (id: string) =&gt; {\n  const response = await fetch(`\/api\/users\/${id}`);\n  const data = await response.json();\n\n  const result = UserProfileSchema.safeParse(data);\n  if (!result.success) {\n    throw new Error('Invalid API response');\n  }\n  return result.data;\n};\n<\/code><\/pre>\n<p>TypeScript is not a replacement for runtime validation. If you aren&#8217;t validating your boundaries, your types are just documentation that can\u2014and will\u2014lie to you.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"6_Redux_Deep_Cloning_and_the_State_Management_Bloat\"><\/span>6. Redux, Deep Cloning, and the State Management Bloat<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In our React frontend, the state management was a dumpster fire. The &#8220;javascript best&#8221; practice at the time was to keep &#8220;everything&#8221; in a single Redux store. We had a global state object that was 12MB of nested JSON.<\/p>\n<p>Every time a user typed a single character into a search box, we were running a reducer that performed a deep clone of that 12MB object using the spread operator.<\/p>\n<p><strong>The Performance Killer:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">\/\/ reducer.js\ncase 'UPDATE_SEARCH_TERM':\n  return {\n    ...state, \/\/ 12MB of data being shallow copied\n    search: {\n      ...state.search,\n      term: action.payload\n    }\n  };\n<\/code><\/pre>\n<p>On a mid-range mobile device, this caused a 150ms &#8220;jank&#8221; on every keystroke. The UI felt like it was stuck in molasses. V8&#8217;s garbage collector was working overtime to clean up the thousands of discarded state objects.<\/p>\n<p><strong>The Correction:<\/strong><br \/>\nWe moved UI-specific state (like search terms) into local component state. For the global state, we stopped the deep-nesting madness. We normalized the data.<\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">\/\/ Instead of a giant tree, use flat objects (entities)\nconst [searchTerm, setSearchTerm] = useState('');\n\n\/\/ Reducer only handles what it needs\ncase 'UPDATE_ENTITY':\n  return {\n    ...state,\n    entities: {\n      ...state.entities,\n      [action.id]: action.data\n    }\n  };\n<\/code><\/pre>\n<p>We also stopped using Redux for server-side data and moved to TanStack Query. This reduced our total &#8220;State Management&#8221; code by 70%.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"7_Dependency_Hell_and_the_2GB_node_modules_Folder\"><\/span>7. Dependency Hell and the 2GB <code>node_modules<\/code> Folder<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>By the end of the migration, our <code>package-lock.json<\/code> was 45,000 lines long. We had 1,400 dependencies. Every time we ran <code>npm install<\/code> (using npm v10.2.4), it was a gamble. <\/p>\n<p>One morning, the build pipeline failed because a sub-dependency of a sub-dependency of a &#8220;pretty-logger&#8221; library had been unpublished. The entire company stopped for 6 hours.<\/p>\n<p><strong>The <code>npm audit<\/code> Reality Check:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-bash\">$ npm audit\n# 124 vulnerabilities found (32 high, 12 critical)\n# Run `npm audit fix` to... [DO NOT DO THIS, IT WILL BREAK EVERYTHING]\n<\/code><\/pre>\n<p>We were using heavy libraries for things that Node.js now does natively. We had <code>request<\/code>, <code>axios<\/code>, and <code>node-fetch<\/code> all in the same project because different &#8220;microservices&#8221; used different templates.<\/p>\n<p><strong>The Correction:<\/strong><br \/>\nWe audited every single dependency. If it could be done with the Node.js standard library, we deleted the dependency.<\/p>\n<ol>\n<li><strong>Deleted <code>moment.js<\/code>:<\/strong> Replaced with <code>Intl.DateTimeFormat<\/code>.<\/li>\n<li><strong>Deleted <code>lodash<\/code>:<\/strong> Replaced with native <code>Array.map<\/code>, <code>filter<\/code>, <code>reduce<\/code>, and <code>Object.entries<\/code>.<\/li>\n<li><strong>Deleted <code>axios<\/code>:<\/strong> Replaced with the native <code>fetch<\/code> API (available in Node 20).<\/li>\n<li><strong>Deleted <code>chalk<\/code>:<\/strong> Replaced with native ANSI escape codes for the few logs that actually needed color.<\/li>\n<\/ol>\n<p>We reduced the <code>node_modules<\/code> size from 2.2GB to 180MB. The build time went from 12 minutes to 2 minutes.<\/p>\n<hr \/>\n<h3><span class=\"ez-toc-section\" id=\"8_The_Cost_of_%E2%80%9CClever%E2%80%9D_Code\"><\/span>8. The Cost of &#8220;Clever&#8221; Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The underlying theme of this $50M disaster was the desire for developers to feel &#8220;clever.&#8221; <\/p>\n<p>They used <code>Reflect.metadata<\/code> for dependency injection in a project that didn&#8217;t need it. They used <code>AsyncLocalStorage<\/code> to pass context through 15 layers of middleware, making debugging a nightmare because the stack traces were incomprehensible. They used <code>InversifyJS<\/code> to solve a problem that simple constructor injection would have solved in three lines of code.<\/p>\n<p><strong>The &#8220;Clever&#8221; DI Mess:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-typescript\">@injectable()\nclass OrderProcessor {\n  constructor(@inject(TYPES.Database) private db: IDatabase) {}\n  \/\/ ...\n}\n<\/code><\/pre>\n<p><strong>The Sane Reality:<\/strong><\/p>\n<pre class=\"codehilite\"><code class=\"language-javascript\">class OrderProcessor {\n  constructor(db) {\n    this.db = db;\n  }\n}\nconst processor = new OrderProcessor(databaseInstance);\n<\/code><\/pre>\n<p>The latter is readable, testable, and doesn&#8217;t require a PhD in Decorators to understand.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Conclusion_What_%E2%80%9Cjavascript_best%E2%80%9D_Actually_Means\"><\/span>Conclusion: What &#8220;javascript best&#8221; Actually Means<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>After 18 months of cleaning up this mess, I can tell you what &#8220;javascript best&#8221; practices actually look like. They aren&#8217;t flashy. They don&#8217;t get you 1,000 likes on LinkedIn.<\/p>\n<ol>\n<li><strong>Prefer Simplicity over Abstraction:<\/strong> If you can solve it with a function, don&#8217;t use a class. If you can solve it with a class, don&#8217;t use a framework.<\/li>\n<li><strong>Validate at the Boundaries:<\/strong> Use Zod or Joi. Trust nothing that comes from an API or a database.<\/li>\n<li><strong>Monitor the V8 Engine:<\/strong> If your memory usage is climbing, you have a leak. Don&#8217;t just increase the RAM in your Dockerfile. Find the leak.<\/li>\n<li><strong>Avoid Hype-Driven Development:<\/strong> Just because a new library is trending doesn&#8217;t mean it&#8217;s ready for production. <\/li>\n<li><strong>Keep the Dependency Tree Lean:<\/strong> Every dependency is a potential security hole and a maintenance burden.<\/li>\n<li><strong>Monoliths are Fine:<\/strong> Unless you are at a scale where you literally cannot store the code in one git repo, you probably don&#8217;t need microservices.<\/li>\n<\/ol>\n<p>We are finally back in the black. The system is stable. The latency is low. But we paid $50M to learn a lesson we should have already known: <strong>Code is a liability, not an asset.<\/strong> The less of it you have, the better off you are.<\/p>\n<p>Now, if you&#8217;ll excuse me, I have to go delete another 5,000 lines of &#8220;clever&#8221; code.<\/p>\n<p><strong>SIGNED,<\/strong><\/p>\n<p><em>A Very Tired Architect<\/em><br \/>\n(Node.js v20.11.1, npm v10.2.4, V8 Engine survivor)<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Related_Articles\"><\/span>Related Articles<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Explore more insights and best practices:<\/p>\n<ul>\n<li><a href=\"https:\/\/itsupportwale.com\/blog\/the-ultimate-kubectl-cheat-sheet-you-ever-need\/\">The Ultimate Kubectl Cheat Sheet You Ever Need<\/a><\/li>\n<li><a href=\"https:\/\/itsupportwale.com\/blog\/is-machine-learning-ai-understanding-the-key-differences\/\">Is Machine Learning Ai Understanding The Key Differences<\/a><\/li>\n<li><a href=\"https:\/\/itsupportwale.com\/blog\/understanding-machine-learning-models-a-complete-guide\/\">Understanding Machine Learning Models A Complete Guide<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>INCIDENT REPORT #882-B: Why our migration to the &#8216;Modern Stack&#8217; nearly bankrupted the company. DATE: October 14, 2023 TO: Executive Steering Committee, Engineering Leads FROM: Lead Architect (Recovery Team) SUBJECT: Post-Mortem of &#8220;Project Phoenix&#8221; (The Great Migration) TOTAL LOSS: $50,420,000 USD (Estimated) TIMELINE: January 2022 \u2013 June 2023 1. Incident Report Summary Project Phoenix was &#8230; <a title=\"JavaScript Best Practices: Write Cleaner, Faster Code\" class=\"read-more\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\" aria-label=\"Read more  on JavaScript Best Practices: Write Cleaner, Faster Code\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4759","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale\" \/>\n<meta property=\"og:description\" content=\"INCIDENT REPORT #882-B: Why our migration to the &#8216;Modern Stack&#8217; nearly bankrupted the company. DATE: October 14, 2023 TO: Executive Steering Committee, Engineering Leads FROM: Lead Architect (Recovery Team) SUBJECT: Post-Mortem of &#8220;Project Phoenix&#8221; (The Great Migration) TOTAL LOSS: $50,420,000 USD (Estimated) TIMELINE: January 2022 \u2013 June 2023 1. Incident Report Summary Project Phoenix was ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\" \/>\n<meta property=\"og:site_name\" content=\"ITSupportWale\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Itsupportwale-298547177495978\" \/>\n<meta property=\"article:published_time\" content=\"2026-04-13T16:20:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2021\/05\/android-chrome-512x512-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Techie\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Techie\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\"},\"author\":{\"name\":\"Techie\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/#\/schema\/person\/8c5a2b3d36396e0a8fd91ec8242fd46d\"},\"headline\":\"JavaScript Best Practices: Write Cleaner, Faster Code\",\"datePublished\":\"2026-04-13T16:20:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\"},\"wordCount\":1610,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\",\"url\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\",\"name\":\"JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale\",\"isPartOf\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/#website\"},\"datePublished\":\"2026-04-13T16:20:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/itsupportwale.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Best Practices: Write Cleaner, Faster Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/#website\",\"url\":\"https:\/\/itsupportwale.com\/blog\/\",\"name\":\"ITSupportWale\",\"description\":\"Tips, Tricks, Fixed-Errors, Tutorials &amp; Guides\",\"publisher\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/itsupportwale.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/#organization\",\"name\":\"itsupportwale\",\"url\":\"https:\/\/itsupportwale.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2023\/09\/cropped-Logo-trans-without-slogan.png\",\"contentUrl\":\"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2023\/09\/cropped-Logo-trans-without-slogan.png\",\"width\":1119,\"height\":144,\"caption\":\"itsupportwale\"},\"image\":{\"@id\":\"https:\/\/itsupportwale.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Itsupportwale-298547177495978\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/itsupportwale.com\/blog\/#\/schema\/person\/8c5a2b3d36396e0a8fd91ec8242fd46d\",\"name\":\"Techie\",\"sameAs\":[\"https:\/\/itsupportwale.com\",\"iswblogadmin\"],\"url\":\"https:\/\/itsupportwale.com\/blog\/author\/iswblogadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale","og_description":"INCIDENT REPORT #882-B: Why our migration to the &#8216;Modern Stack&#8217; nearly bankrupted the company. DATE: October 14, 2023 TO: Executive Steering Committee, Engineering Leads FROM: Lead Architect (Recovery Team) SUBJECT: Post-Mortem of &#8220;Project Phoenix&#8221; (The Great Migration) TOTAL LOSS: $50,420,000 USD (Estimated) TIMELINE: January 2022 \u2013 June 2023 1. Incident Report Summary Project Phoenix was ... Read more","og_url":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/","og_site_name":"ITSupportWale","article_publisher":"https:\/\/www.facebook.com\/Itsupportwale-298547177495978","article_published_time":"2026-04-13T16:20:27+00:00","og_image":[{"width":512,"height":512,"url":"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2021\/05\/android-chrome-512x512-1.png","type":"image\/png"}],"author":"Techie","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Techie","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#article","isPartOf":{"@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/"},"author":{"name":"Techie","@id":"https:\/\/itsupportwale.com\/blog\/#\/schema\/person\/8c5a2b3d36396e0a8fd91ec8242fd46d"},"headline":"JavaScript Best Practices: Write Cleaner, Faster Code","datePublished":"2026-04-13T16:20:27+00:00","mainEntityOfPage":{"@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/"},"wordCount":1610,"commentCount":0,"publisher":{"@id":"https:\/\/itsupportwale.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/","url":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/","name":"JavaScript Best Practices: Write Cleaner, Faster Code - ITSupportWale","isPartOf":{"@id":"https:\/\/itsupportwale.com\/blog\/#website"},"datePublished":"2026-04-13T16:20:27+00:00","breadcrumb":{"@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/itsupportwale.com\/blog\/javascript-best-practices-write-cleaner-faster-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/itsupportwale.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Best Practices: Write Cleaner, Faster Code"}]},{"@type":"WebSite","@id":"https:\/\/itsupportwale.com\/blog\/#website","url":"https:\/\/itsupportwale.com\/blog\/","name":"ITSupportWale","description":"Tips, Tricks, Fixed-Errors, Tutorials &amp; Guides","publisher":{"@id":"https:\/\/itsupportwale.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itsupportwale.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/itsupportwale.com\/blog\/#organization","name":"itsupportwale","url":"https:\/\/itsupportwale.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itsupportwale.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2023\/09\/cropped-Logo-trans-without-slogan.png","contentUrl":"https:\/\/itsupportwale.com\/blog\/wp-content\/uploads\/2023\/09\/cropped-Logo-trans-without-slogan.png","width":1119,"height":144,"caption":"itsupportwale"},"image":{"@id":"https:\/\/itsupportwale.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Itsupportwale-298547177495978"]},{"@type":"Person","@id":"https:\/\/itsupportwale.com\/blog\/#\/schema\/person\/8c5a2b3d36396e0a8fd91ec8242fd46d","name":"Techie","sameAs":["https:\/\/itsupportwale.com","iswblogadmin"],"url":"https:\/\/itsupportwale.com\/blog\/author\/iswblogadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/posts\/4759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/comments?post=4759"}],"version-history":[{"count":0,"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/posts\/4759\/revisions"}],"wp:attachment":[{"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/media?parent=4759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/categories?post=4759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsupportwale.com\/blog\/wp-json\/wp\/v2\/tags?post=4759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}