⚠️ That One-Liner Math.max(...arr) Might Be Slowing You Down We’ve all written this: Math.max(...arr) It’s clean. It’s readable. It feels very JavaScript-y. 😌 But here’s the uncomfortable truth: It’s not always safe in production. What Actually Happens Behind the Scenes? When you use: Math.max(...largeArray) JavaScript doesn’t “loop” the array. It expands every single element into a function argument. If your array has 10 elements → fine. If it has 10,000 → risky. If it has 100,000+ → 💥 you might hit: ❌ Maximum call stack size exceeded ❌ Engine argument limits ❌ Memory spikes ❌ Unexpected crashes Most JS engines have limits on how many arguments a function can accept. And yes — spread turns your array into individual arguments. 🧠 The Safer Alternative Instead of relying on argument expansion: arr.reduce((max, val) => val > max ? val : max, -Infinity) Why this is better: ✔️ No argument explosion ✔️ Handles very large arrays safely ✔️ More predictable performance ✔️ Production-friendly. 🚀 The Real Lesson Modern syntax ≠ Always better. As engineers, especially when building scalable systems, we should ask: What does this do under the hood? How does it behave with large inputs? Will this break at scale? Clean code isn’t just about shorter lines. It’s about understanding trade-offs. If you’ve ever debugged a production issue caused by a “beautiful one-liner,” you know the pain. 😅 What’s another JavaScript shortcut that looks elegant but hides a performance trap? 👇 #JavaScript #WebDevelopment #FrontendDevelopment #Programming #SoftwareEngineering #CleanCode #PerformanceOptimization #TechLeadership #CodingTips #Developers
Math.max(...arr) Performance Pitfall in JavaScript
More Relevant Posts
-
JavaScript’s sort() can silently break your algorithms. I was solving LeetCode #350 (Intersection of Two Arrays II) using the two-pointer approach and hit an unexpected issue. I sorted the arrays like this: nums.sort() It looked fine… until it wasn’t. Example: [1, 2, 10, 5].sort() // → [1, 10, 2, 5] Why? Because JavaScript sorts lexicographically (as strings) by default. So the engine actually compares: "1", "10", "2", "5" Correct numeric sorting requires a comparator: nums.sort((a, b) => a - b) Without this, algorithms that rely on sorted arrays (binary search, two-pointer techniques, etc.) can produce incorrect results. #JavaScript #WebDevelopment #Programming #CodingTips #LeetCode #Algorithms #SoftwareEngineering #FrontendDevelopment
To view or add a comment, sign in
-
-
Day 20: Property Descriptors & The "Sugar" of Classes 🏗️💎 Object-oriented JavaScript isn't just about syntax; it's about control. Today was the final deep-dive into the "Meta" layer of JS objects—understanding why Math.PI is immutable and how to build bulletproof class structures. The "Crack-It Kit" Checklist: Day 20 📑 🔹 Class Hierarchy: Mastering extends and the super() bridge. Understanding why the parent must initialize before the child can exist. 🏛️ 🔹 Static Utilities: Learning to define methods that belong to the "Blueprint" (Class) rather than the "House" (Instance). ⚙️ 🔹 The .bind() Marriage: Locking context permanently. Moving beyond immediate execution to creating reusable, context-safe functions. 🔗 🔹 Under the Hood (Descriptors): Breaking down writable, enumerable, and configurable. Solving the mystery of why built-in JS properties are unchangeable. 💎 🔹 Property Shielding: Using Object.defineProperty to hide data from loops and prevent unauthorized overwrites. 🛡️ 🔹 Object Iteration: Mastering Object.entries() and Factory Functions for cleaner, more modern data handling. 🧪 The foundation is complete. We’ve moved from basic literals to professional meta-programming. 🏗️ #JavaScript #WebDevelopment #CrackItKit #OOP #WebEngineering #CodingJourney #SoftwareArchitecture #TechInterviews #120DayChallenge
To view or add a comment, sign in
-
-
✨ 𝗗𝗮𝘆 𝟱 𝗼𝗳 𝗠𝘆 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗝𝗼𝘂𝗿𝗻𝗲𝘆 🚀 Today I explored 𝗡𝘂𝗺𝗯𝗲𝗿𝘀 𝗮𝗻𝗱 𝘁𝗵𝗲 𝗠𝗮𝘁𝗵 𝗢𝗯𝗷𝗲𝗰𝘁 in JavaScript, and learned some interesting behind-the-scenes details: • 𝗡𝘂𝗺𝗯𝗲𝗿 – integers, floats, NaN, Infinity • 𝗠𝗮𝘁𝗵 𝗢𝗯𝗷𝗲𝗰𝘁 – handy methods like Math.round(), Math.floor(), Math.ceil(), Math.random(), Math.max(), Math.min() • 𝗠𝗮𝘁𝗵.𝗿𝗮𝗻𝗱𝗼𝗺() – generates pseudo-random numbers, which means it’s not truly random. It’s predictable if you know the algorithm and therefore shouldn’t be used for OTPs or secure codes. It’s fascinating to see how much thought goes into even simple math operations in JavaScript. Step by step, my fundamentals are getting stronger! 💪 #JavaScript #100DaysOfCode #WebDevelopment #LearningJourney #FrontendDevelopment #CodingTips
To view or add a comment, sign in
-
-
✨ 𝗗𝗮𝘆 𝟰 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 🚀 Today I learned about 𝗖𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝘀, 𝗟𝗼𝗼𝗽𝘀, 𝗮𝗻𝗱 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 — the core logic that makes programs think and repeat tasks. But what really surprised me was what happens behind the scenes when we compare values. I discovered why floating-point calculations can be unpredictable in JavaScript. 𝗙𝗼𝗿 𝗲𝘅𝗮𝗺𝗽𝗹𝗲: 𝟬.𝟭 + 𝟬.𝟮 !== 𝟬.𝟯 This happens because JavaScript uses binary floating-point representation (IEEE 754), and some decimal numbers can’t be represented exactly in binary. So tiny precision errors appear. It’s fascinating to see that even simple math has deep computer science concepts behind it. Every day I realize: writing code is one thing, understanding it deeply is another. 💪 #JavaScript #100DaysOfCode #WebDevelopment #LearningJourney #ComputerScience
To view or add a comment, sign in
-
-
AI is great at writing code, but it’s surprisingly bad at middle-school math. 💻 As a Frontend Engineer scaling calculator-all.com with Next.js 15 and TypeScript, I recently had a reality check. I was building a complex financial tool and decided to let Cursor and GitHub Copilot take the lead on the core formula. The code looked beautiful. The Tailwind CSS styling was spot on, and Biome didn’t report a single linting error. But then I ran my Vitest suite. 🛑 The calculator was off by nearly $2,400 on a standard 10-year projection. 📈 The AI had "hallucinated" a simplified version of the compound interest formula that looked correct but failed at scale. 💸 It tried to be clever with floating-point math where it should have been precise. I had to step in, go back to the whiteboard, and manually rewrite the logic using proper decimal handling. 🛠️ Modern tools like Bun make my workflow 10x faster, but they don't replace the need for a human to verify the output. 🧠 I almost shipped a tool that would have given my users a very false sense of wealth. 😅 My DMs would have been a disaster by Monday morning. AI is an incredible co-pilot, but we are still the ones responsible for the flight path. 🚀 How often do you double-check the logic AI generates for you? #FrontendEngineer #TypeScript #Nextjs #ReactJS #WebDev #AI #SoftwareEngineering #CleanCode #JavaScript #Programming #WebPerformance #Vitest #TailwindCSS #Coding #Productivity
To view or add a comment, sign in
-
-
It's 5:45 am & I am still looking at my dreams. Today[2/28/2026] is day 23 of learning javascript Today & tommorow i will learn are: 1. Difference between let, const & var 2. How to use the default parameter 3. Template string, Multiline string, Dynamic string 4. Arrow Function Syntax, params 5. Spread Operator, Array Max, Copy Arrays 6. Object & Array destructuring 7. Keys, Values, Entries, Delete, Seal, Freeze 8. Accessing Object Data: Nested Object, Optional Chaining 9. Looping Object 10. Primitive Type, Non Primitive Type 11. Null Vs Undefines 12. Truthy & Falsy Values 13. ==, === , implicit conversion 14. Block Scope, Global Scope, Simple Unders. of Hoisting 15. Closure 16. Callback Function & pass different function 17. Function Arguments, pass by ref. pass by value 18. Map, ForEach 19. Filter, Find, Reduce #letsconnect #programmer #frontenddeveloper #mernstakedeveloper #Coding
To view or add a comment, sign in
-
Blog 06 of my JS Unlocked series is live! 🚀 JavaScript Arrays 101: Everything You Need to Get Started 👇 Storing 5 student names in 5 separate variables? That breaks at scale. Arrays fix that — one variable, any number of values, stored in order. In this one I cover: ✅ What arrays are and why we need them ✅ How indexing works (starts at 0 — always!) ✅ Accessing first, last, and any element ✅ Updating array values ✅ The .length property and its practical uses ✅ for loop vs for...of — when to use which ✅ Hands-on challenge with 5 favorite movies Would love your feedback if you read it 🙏 🔗 https://lnkd.in/dSRR3Rxh Thanks to Hitesh Choudhary Sir, Piyush Garg #JavaScript #WebDevelopment #Hashnode #WebDevCohort2026 #LearningInPublic #Frontend #JS
To view or add a comment, sign in
-
Time Complexity explained from first principles When solving problems, the most important question is not “Does it work?” The real question is: “How well does it scale?” Consider this code: for (let i = 0; i < n; i++) { console.log(i); } If n = 10 → 10 operations If n = 1,000 → 1,000 operations If n = 1,000,000 → 1,000,000 operations This is called O(n) time complexity. Now consider: for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { console.log(i, j); } } If n = 1,000 → 1,000,000 operations This is O(n²) Why this matters: As input grows, inefficient algorithms become unusable. Goal: Always aim to reduce time complexity. #datastructures #algorithms #javascript #engineering
To view or add a comment, sign in
-
Shortest Path Algorithm — Explained Mathematically We often learn shortest path algorithms from a coding perspective. But what if we step back and understand them from a mathematical point of view? In today’s post, I’ve explained the shortest path algorithm in a slightly different way — through mathematical reasoning and structure. It’s a deeper, more conceptual approach that helps you truly understand *why* the algorithm works, not just how to implement it. If you enjoy digging beneath the surface and building strong theoretical foundations behind practical coding problems, this one is for you. 👇 Do you prefer understanding algorithms conceptually first, or jumping straight into code? Follow Muhammad Nouman for more useful content #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #Next #CodingCommunity #Algorithms #DataStructures #SystemDesign #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 915 of #1000DaysOfCode ✨ Shortest Path Algorithm — Explained Mathematically We often learn shortest path algorithms from a coding perspective. But what if we step back and understand them from a mathematical point of view? In today’s post, I’ve explained the shortest path algorithm in a slightly different way — through mathematical reasoning and structure. It’s a deeper, more conceptual approach that helps you truly understand *why* the algorithm works, not just how to implement it. If you enjoy digging beneath the surface and building strong theoretical foundations behind practical coding problems, this one is for you. 👇 Do you prefer understanding algorithms conceptually first, or jumping straight into code? #Day915 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #Next #CodingCommunity #Algorithms #DataStructures #SystemDesign #ProblemSolving
To view or add a comment, sign in
Explore related topics
- Writing Elegant Code for Software Engineers
- Writing Code That Scales Well
- Coding Best Practices to Reduce Developer Mistakes
- Code Planning Tips for Entry-Level Developers
- Writing Functions That Are Easy To Read
- Managing System Scalability and Code Maintainability
- Why Well-Structured Code Improves Project Scalability
- How to Write Clean, Error-Free Code
- How to Improve Your Code Review Process
- How to Refactor Code Thoroughly
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development