Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungLINQ GroupBy: The Operator Everyone Uses Wrong(23.09.2026 um 09:41 Uhr)
Sichere ProgrammierungIT Heard About the Acquisition Nine Days Before It Closed(23.09.2026 um 09:45 Uhr)
Sichere ProgrammierungThe Story Behind Building NuvyntraLabs(23.09.2026 um 09:45 Uhr)
Sichere ProgrammierungFive dashboards nobody was opening(23.09.2026 um 09:46 Uhr)
Sichere ProgrammierungThe Shift from AI Insights to AI Actions in Finance(23.09.2026 um 09:47 Uhr)
Sichere ProgrammierungGo WebAssembly Meets WebForms Core 2.1(23.09.2026 um 09:49 Uhr)
Sichere ProgrammierungJust One More Round: Scope Creep in the Age of AI Agents(23.09.2026 um 09:50 Uhr)
Sichere ProgrammierungThe Calls That Reach Us Now Are the Ones the Model Could Not Answer(23.09.2026 um 09:50 Uhr)
Sichere ProgrammierungOne Loop Made Four Hundred Round Trips(23.09.2026 um 09:52 Uhr)
Sichere ProgrammierungThe order was committed and nothing else ever heard about it(23.09.2026 um 09:53 Uhr)
Sichere ProgrammierungLINQ GroupBy: The Operator Everyone Uses Wrong(23.09.2026 um 09:41 Uhr)
Sichere ProgrammierungIT Heard About the Acquisition Nine Days Before It Closed(23.09.2026 um 09:45 Uhr)
Sichere ProgrammierungThe Story Behind Building NuvyntraLabs(23.09.2026 um 09:45 Uhr)
Sichere ProgrammierungFive dashboards nobody was opening(23.09.2026 um 09:46 Uhr)
Sichere ProgrammierungThe Shift from AI Insights to AI Actions in Finance(23.09.2026 um 09:47 Uhr)
Sichere ProgrammierungGo WebAssembly Meets WebForms Core 2.1(23.09.2026 um 09:49 Uhr)
Sichere ProgrammierungJust One More Round: Scope Creep in the Age of AI Agents(23.09.2026 um 09:50 Uhr)
Sichere ProgrammierungThe Calls That Reach Us Now Are the Ones the Model Could Not Answer(23.09.2026 um 09:50 Uhr)
Sichere ProgrammierungOne Loop Made Four Hundred Round Trips(23.09.2026 um 09:52 Uhr)
Sichere ProgrammierungThe order was committed and nothing else ever heard about it(23.09.2026 um 09:53 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Javascript Question of the Day #10 [Talk::Overflow]

This post explains a quiz originally shared as a LinkedIn poll. 🔹 The Question let counter = 0; const tracker = { get id() { return ++counter; } }; const cache = new Map(); function memoize(obj, key) { if (!…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

This post explains a quiz originally shared as a LinkedIn poll.






🔹 The Question






let counter = 0;

const tracker = {
get id() {
return ++counter;
}
};

const cache = new Map();

function memoize(obj, key) {
if (!cache.has(obj)) {
cache.set(obj, { [key]: obj[key] });
}
return cache.get(obj)[key];
}

console.log(memoize(tracker, 'id'));
console.log(memoize(tracker, 'id'));
console.log(tracker.id);






Hint: When does the getter execute? Think about when property access happens and what gets stored in the cache.






🔹 Solution



Correct Answer: A) 1, 1, 2



The output will be:




  • 1

  • 1

  • 2






🧠 How this works



This quiz demonstrates a critical production bug pattern: getters with side effects combined with memoization. The core issue is that the getter executes at a specific moment during caching, and subsequent calls return the cached value, not the result of re-executing the getter.



The memoization function creates a cache that stores the result of accessing the property, not a reference to the getter itself. Once cached, the getter never runs again for memoized calls—but direct property access still triggers it.



The key insight: The memoize function evaluates obj[key] exactly once (during cache creation), stores the resulting value, and returns that same value on subsequent calls. The getter's side effect (incrementing counter) only happens during that initial evaluation.






🔍 Line-by-line explanation





  1. Initial state:




   let counter = 0;
const tracker = { get id() { return ++counter; } };








  • counter starts at 0


  • tracker.id is a getter that pre-increments counter and returns the new value





  1. First memoize call:




   console.log(memoize(tracker, 'id'));







  • Check: !cache.has(tracker)true (cache is empty)

  • Execute: cache.set(tracker, { [key]: obj[key] })

  • When creating { id: obj['id'] }, it evaluates tracker.id


  • Getter executes: ++counter → counter becomes 1, returns 1

  • Cache stores: tracker → { id: 1 }

  • Return: cache.get(tracker)['id']1 (from the cached object)

  • Output: 1





  1. Second memoize call:




   console.log(memoize(tracker, 'id'));







  • Check: !cache.has(tracker)false (tracker is already in cache)

  • Skip the cache.set block entirely

  • Return: cache.get(tracker)['id']1 (from the cached object)


  • Getter does NOT execute - we're reading from the cached plain object

  • Output: 1





  1. Direct property access:




   console.log(tracker.id);







  • Direct access to tracker.id bypasses the cache entirely


  • Getter executes: ++counter → counter becomes 2, returns 2

  • Output: 2






🔹 Key Takeaways




  1. Getters execute at property access time: When you access obj[key] where key is a getter, the getter function runs immediately and returns a value.


  2. Memoization captures values, not behavior: Caching { [key]: obj[key] } stores the result of the getter execution, not a reference to the getter itself.


  3. Cached objects are plain objects: The cached object { id: 1 } is a regular object with a regular property—it doesn't inherit the getter from the original object.


  4. Side effects run once during cache creation: Any side effects in the getter (incrementing counters, logging, API calls) only happen during the initial cache population.


  5. Direct access bypasses cache: Accessing tracker.id directly always executes the getter, regardless of what's in the cache.


  6. WeakMap doesn't prevent this issue: Using WeakMap vs Map doesn't change the behavior—the problem is caching the value instead of the getter behavior.


  7. This is a common memoization anti-pattern: Memoizing property access without considering whether the property is a getter with side effects leads to subtle bugs that are hard to debug.


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Javascript Question of the Day #10 [Talk::Overflow]

Thematisch verwandte Begriffe: Javascript, Question, TalkOverflow · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96258 | A vulnerability has been found in onSite internet GmbH Auktion NG Auktio…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick