25.2. Store data per DOM element without leaks
Topics
This question is in
25. Memory and cleanupWidgets often need to remember something about each element they enhance: a tooltip's text, a sortable column's direction, the chart object drawn inside a <canvas>. Three common ways to store it all have problems. Writing a property onto the element (el.__chart = chart) mixes your data into the DOM. A data- attribute only holds strings. A global Map from element to data leaks: when the element is removed from the page, the map still holds it, so neither the element nor its data can ever be freed.
Write an ElementData class in element-data.js that stores data per element without any of those problems.
Signature
class ElementData {
set(element, key, value) // returns the store
get(element, key) // the value, or undefined
has(element, key) // true or false
delete(element, key) // true if the key was there
clear(element) // removes all of element's data; true if it had any
}Requirements
setstores a value under a key for one element and returns the store;getreads it.- A missing element or key gives
undefined. Each element has its own data; each store is independent. - Keys can be any value (strings, symbols, numbers, objects) and are compared like
Mapkeys. hasistruefor a storedundefined.deleteremoves one key and returns whether it was there.clearremoves everything for one element and returns whether it had data. When an element's last key is deleted, it has no data left.- The element itself is never changed: no properties, no attributes.
- The element is held weakly: it is a key of a
WeakMap, and is never put into aMap, aSetor an array, so the store alone never keeps it alive. - Data stays as long as your code still holds the element, even after it is removed from the page.
- Every method throws
TypeErrorwhenelementis not a DOMElement.
Examples
const charts = new ElementData();
const canvas = document.querySelector("#sales");
charts.set(canvas, "chart", drawChart(canvas)).set(canvas, "range", "week");
charts.get(canvas, "range"); // "week"
charts.has(canvas, "zoom"); // falsecanvas.remove();
canvas = null;
// No reference to the canvas is left: the garbage collector may free it,
// and the chart stored for it, without any cleanup call.charts.set("#sales", "chart", 1); // TypeError: element must be a DOM elementEnvironment
- Plain JavaScript in a browser page; the tests use the global
ElementDataclass fromelement-data.js. - No test can make the garbage collector run. Instead, the tests check that nothing is written to the element, that a
WeakMapreceives the element as its key, and (by watchingMap.prototype.set,Set.prototype.addand the array methods that add items) that no strong collection ever receives an element. - Out of scope: listing every element that has data (a
WeakMapcannot be listed, on purpose), and data for text nodes.
Hints
- Which built-in collection lets its keys be garbage-collected?
- A
WeakMapcannot tell you how many keys it has. Why must that be so?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it