Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Fix a WeakMap Losing Entries After a Structured Clone

- 17.11.26 - ErcanOPAK

⚡ JavaScript: Fix a WeakMap Losing Entries After a Structured Clone

A WeakMap only ever matches a key by strict object reference, which is exactly what makes it silently break the moment an object crosses a structuredClone() boundary — passing data to a Web Worker through postMessage(), or cloning it directly — because structured cloning produces a brand-new object graph with nested objects that look identical to the originals but are different references entirely. A cache keyed on the pre-clone object finds nothing for the post-clone one, even though every property on it compares equal.

🔎 The Problem

const metadataCache = new WeakMap();
const node = { id: 42, payload: {} };
metadataCache.set(node.payload, { lastAccessed: Date.now() });

worker.postMessage(node); // internally structured-clones `node`
// inside the worker, `node.payload` is a NEW object reference -
// metadataCache.get(clonedNode.payload) returns undefined, even
// though clonedNode.payload deep-equals the original payload

✅ Fix: Key the Cache by a Stable ID, Not by Reference, Across a Clone Boundary

  • Never key a WeakMap on an object you expect to survive a structuredClone(), a postMessage(), or an IndexedDB round-trip — reference identity is exactly what a structured clone does not preserve.
  • Use a stable string or numeric id carried as a plain property on the object instead, and key a regular Map (or re-derive the association) on that id after the clone, since ids survive cloning perfectly while references never do.
  • If the association genuinely must live on the object itself across the boundary, send it explicitly as part of the cloned payload rather than relying on a side-table the clone has no way of knowing about.

🚨 Why This Is Dangerous

  • Debugging a ‘random’ cache-miss bug for hours because every object involved deep-equals its counterpart in logs on both sides of a postMessage boundary, when the actual cause — a WeakMap doing a reference comparison, not a value comparison — never shows up in a console.log at all.

Deep-equal and the-same-reference are different questions, and WeakMap only ever answers the second one.

— JavaScript Tip

Related posts:

JavaScript: Why localStorage Silently Throws in Private Browsing Mode

JavaScript: Why localStorage Silently Throws in Private Browsing Mode

JS Memory Leaks — Forgotten Event Listeners

JS Memory Leaks — Forgotten Event Listeners

JavaScript: Cleaner API Consumption with Object Destructuring

JavaScript: Cleaner API Consumption with Object Destructuring

Views: 6

Post navigation

WordPress: Fix a Shortcode Rendering Twice Inside the Block Editor
C#: Fix a Lazy Initializer Running Twice Under Thread Contention

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com