> For the complete documentation index, see [llms.txt](https://attr.sygnal.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://attr.sygnal.com/overview/sa5-core/core-technical-notes/element-tracking.md).

# Element Tracking

## Map v. WeakMap

Both \`Map\` and \`WeakMap\` are built-in objects in JavaScript that can store key-value pairs, but there are some key differences between them:<br>

1\. \*\*Key Types\*\*: In a \`Map\`, keys can be of any type: primitives, objects, functions, etc. In a \`WeakMap\`, keys must be objects (not primitive values).

2\. \*\*Garbage Collection\*\*: One of the main differences between \`Map\` and \`WeakMap\` is how they interact with garbage collection. In a \`Map\`, as long as the \`Map\` itself is alive, all of its keys are kept alive too, even if there are no other references to those keys. This could potentially lead to memory leaks if you're not careful. On the other hand, in a \`WeakMap\`, if a key object is not referenced anywhere else, it can be garbage collected, and its entry is automatically removed from the \`WeakMap\`. This makes \`WeakMap\` useful for associating additional data with objects without affecting their lifetime.

3\. \*\*Enumeration\*\*: A \`Map\` is iterable, which means you can loop over its keys and values, and it has a \`size\` property that tells you how many entries it has. A \`WeakMap\` is not iterable and does not have a \`size\` property. This is mainly because of the way \`WeakMap\` interacts with garbage collection -- since keys can be automatically removed at any time, there's no guarantee of how many entries the \`WeakMap\` has at any moment.

4\. \*\*Use Cases\*\*: \`Map\` is a general-purpose key-value store that can be used in a wide variety of scenarios. \`WeakMap\` is more specialized. It's useful when you want to associate data with an object without interfering with garbage collection, such as storing metadata about objects, or keeping private data for an object.

So, in summary, while \`Map\` and \`WeakMap\` can both store key-value pairs, they have different characteristics regarding key types, garbage collection, enumeration, and use cases.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://attr.sygnal.com/overview/sa5-core/core-technical-notes/element-tracking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
