Skip to curriculum
Jump to the editor

25.2. Store data per DOM element without leaks

Medium
Topics

This question is in

25. Memory and cleanup
15–25 min

Widgets 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

JAVASCRIPT
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

  1. set stores a value under a key for one element and returns the store; get reads it.
  2. A missing element or key gives undefined. Each element has its own data; each store is independent.
  3. Keys can be any value (strings, symbols, numbers, objects) and are compared like Map keys.
  4. has is true for a stored undefined.
  5. delete removes one key and returns whether it was there. clear removes everything for one element and returns whether it had data. When an element's last key is deleted, it has no data left.
  6. The element itself is never changed: no properties, no attributes.
  7. The element is held weakly: it is a key of a WeakMap, and is never put into a Map, a Set or an array, so the store alone never keeps it alive.
  8. Data stays as long as your code still holds the element, even after it is removed from the page.
  9. Every method throws TypeError when element is not a DOM Element.

Examples

JAVASCRIPT
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");    // false
JAVASCRIPT
canvas.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.
JAVASCRIPT
charts.set("#sales", "chart", 1);   // TypeError: element must be a DOM element

Environment

  • Plain JavaScript in a browser page; the tests use the global ElementData class from element-data.js.
  • No test can make the garbage collector run. Instead, the tests check that nothing is written to the element, that a WeakMap receives the element as its key, and (by watching Map.prototype.set, Set.prototype.add and the array methods that add items) that no strong collection ever receives an element.
  • Out of scope: listing every element that has data (a WeakMap cannot be listed, on purpose), and data for text nodes.

Hints

  1. Which built-in collection lets its keys be garbage-collected?
  2. A WeakMap cannot 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
Back to the question
Loading the editor…