How JavaScript engines actually handle arrays, and there’s a lot of hidden logic going on. ⚙️ The main thing is the difference between Packed (continuous) and Holey (arrays with empty gaps). Here is what I learned: SMI is the best: Arrays with only small integers are the most optimized. One-way downgrade: If you add a float or string to an integer array, the engine downgrades its optimization. Even if you remove that item later, it doesn't go back to being as fast as it was. Holes are costly: When JS hits an empty gap, it has to check all the way up the prototype chain to find a value. It’s one of the most expensive operations in the engine. A simple tip I learned: Avoid using "new Array(3)" because it creates holes immediately. Starting with [ ] keeps things much more optimized. #LearningInPublic #Javascript #WebDev
JavaScript Array Optimization: SMI, Holey Arrays, and Performance
More Relevant Posts
-
JavaScript Problem 🧩 #10 – slice() vs splice() They look similar, but they behave very differently. This is a common source of bugs in JavaScript, especially when working with arrays in real projects. ❌ No loops ❌ No unnecessary logic ✅ Understanding Problem 👇
To view or add a comment, sign in
-
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array: myArray[0] = Date.now; myArray[1] = myFunction; myArray[2] = myCars; In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes. Arrays are a special kind of objects, with numbered indexes. Array.isArray(fruits); return true or false.
To view or add a comment, sign in
-
I practiced destructuring in JavaScript, and it’s a very useful feature. Using destructuring, we can easily extract values from arrays and objects. For arrays, we use square brackets []. For objects, we use curly braces {}. In arrays, values are assigned based on their position, and in objects, they are assigned based on property names. Such a clean and efficient way to work with data. #JavaScript #Destructuring #ES6 #WebDevelopment #LearningJourney
To view or add a comment, sign in
-
Day 6: Closures in JavaScript Closures — one of the most powerful (and confusing) concepts in JavaScript 🤯 A closure is created when a function remembers variables from its outer scope, even after the outer function has finished execution. 🔹 Example function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const fn = outer(); fn(); // 1 fn(); // 2 🔍 What’s happening here? outer() finishes execution But inner() still remembers count This memory + function = closure 🔹 Key Rules ✅ Closures are formed because of lexical scope ✅ Functions retain access to their outer variables ✅ Each function call creates a new closure 🔹 Why are closures important? ✔️ Data hiding & encapsulation ✔️ Used in debouncing, throttling, currying #JavaScript #Closures #ScopeChain #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
JavaScript TDZ — Where are var, let, and const stored? 🧠 When JS runs, it creates an Execution Context ⚙️ with two memory areas: 1️⃣ Global Object Environment 🌍 → var variables go here → attached to the global object (like window in browsers) → auto-initialized with undefined ✅ 2️⃣ Script / Lexical Environment 📦 → let and const go here → block-scoped memory space → memory reserved but not initialized 🚫 (TDZ ⏳) That’s why: var before declaration → undefined let/const before declaration → error ⛔ JS separates them to enforce block scope and reduce bugs 🛡️ #JavaScript #JSCore #ExecutionContext
To view or add a comment, sign in
-
𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟭𝟲 JavaScript is single-threaded. So how can it handle: • Synchronous code • Promises • Timers All at the same time? The answer is task priority and the Event Loop. In this video, I demonstrate exactly how tasks are added and executed based on priority. 𝙒𝙝𝙚𝙣 𝙘𝙤𝙙𝙚 𝙧𝙪𝙣𝙨: -> Code executes first (Call Stack). -> Promise callbacks go to the Microtask Queue. -> setTimeout callbacks go to the Macrotask Queue. 𝙃𝙤𝙬 𝙩𝙝𝙚 𝙀𝙫𝙚𝙣𝙩 𝙇𝙤𝙤𝙥 𝙋𝙞𝙘𝙠𝙨 𝙏𝙖𝙨𝙠𝙨 When the Call Stack becomes empty: -> Run ALL microtasks (Promises first). -> Then run ONE macrotask (setTimeout). -> Repeat the cycle. Microtasks always have higher priority. 𝙏𝙝𝙞𝙨 𝙥𝙧𝙞𝙤𝙧𝙞𝙩𝙮 𝙨𝙮𝙨𝙩𝙚𝙢 𝙞𝙨 𝙬𝙝𝙖𝙩 𝙖𝙡𝙡𝙤𝙬𝙨 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩 𝙩𝙤: • Stay responsive • Execute async logic predictably • Simulate concurrency while staying single-threaded #JavaScript #WebDevelopment #FrontendDevelopment #EventLoop #AsyncJavaScript #SoftwareEngineering #DeveloppementWeb #JavaScriptFR
To view or add a comment, sign in
-
🧠 JavaScript Memory Allocation (What Actually Matters) JavaScript uses automatic memory management handled by the JS engine. 👁️ Stack memory Stores primitive values (number, string, boolean, null, undefined, symbol, bigint) Stores execution contexts and function call frames 👁️ Heap memory Stores objects, arrays, functions Variables in stack hold references to heap objects Memory is allocated during the creation phase and freed by the garbage collector when objects become unreachable.
To view or add a comment, sign in
-
📚 Today I Learned: Scope Chain in JavaScript The scope chain in JavaScript is used to resolve variable values. When a variable is used, JavaScript looks for it in a specific order. 🔹 How it works: 1️⃣ JavaScript first checks the current scope. 2️⃣ If the variable is not found, it checks the outer (parent) scope. 3️⃣ This process continues until it reaches the global scope. 💻 Example: let a = 10; function outer() { let b = 20; function inner() { let c = 30; console.log(a, b, c); } inner(); } outer(); ✅ The inner() function can access c, b, and a because of the scope chain. #javascript #webdevelopment #frontenddeveloper #learninginpublic
To view or add a comment, sign in
-
💡 Sunday Dev Tip: JavaScript Array Methods Stop writing loops. Use array methods instead! ❌ Traditional Loop: let doubled = []; for (let i = 0; i < numbers.length; i++) { doubled.push(numbers[i] * 2); } ✅ Modern Approach: const doubled = numbers.map(n => n * 2); Master These Methods: → .map() - Transform each element → .filter() - Keep elements that match → .reduce() - Calculate single value → .find() - Get first match → .some() / .every() - Test conditions Your code becomes: ✅ More readable ✅ Less error-prone ✅ Easier to maintain ✅ More functional Which array method do you use most? 💬 #JavaScript #CleanCode #WebDevelopment #CodingTips #ES6
To view or add a comment, sign in
-
Because events in JavaScript ""bubble"" up the DOM tree, you can attach a single event listener to the parent container and use event.target to figure out which child was clicked. JavaScript // Attach ONE listener to the parent <ul> document.getElementById('item-list').addEventListener('click', (event) => { // Check if the clicked element was a button if (event.target.tagName === 'BUTTON') { const itemId = event.target.dataset.id; console.log(`Item ${itemId} clicked!`); } }); This uses drastically less memory and automatically handles new items added to the list dynamically—no need to attach new listeners when the DOM updates! #JavaScript #WebPerformance #CodingInterviews #FrontendDev #TechTips"
To view or add a comment, sign in
-
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