Hone logo
Hone
Problems

DOM Element Reference Store

In web development, frequently querying the Document Object Model (DOM) for elements can be computationally expensive and lead to less efficient code. A common optimization strategy is to store references to frequently accessed DOM elements, allowing for quicker retrieval without repeatedly traversing the DOM tree. This challenge asks you to build a simple, centralized cache for these references.

Problem Description

Your task is to implement a DOMElementStore (or similar object/module) that acts as a centralized cache for DOM element references. This store should provide an interface for adding, retrieving, checking for existence, and removing references to DOM elements using unique string keys. The primary goal is to minimize direct DOM queries once an element has been identified and stored.

The DOMElementStore should expose the following functionalities:

  • registerElement(key, element):
    • Registers a given element (which should represent a DOM element reference) with a unique key (string).
    • If the key already exists in the store, it should signal an error (e.g., return false or throw an error).
    • It should return true on successful registration.
  • getElement(key):
    • Retrieves the DOM element reference associated with the given key.
    • If the key does not exist in the store, it should return a special "not found" value (e.g., null or undefined).
  • unregisterElement(key):
    • Removes the DOM element reference associated with the given key from the store.
    • If the key does not exist, it should signal an error or indicate failure (e.g., return false).
    • It should return true on successful unregistration.
  • hasElement(key):
    • Returns true if an element with the given key exists in the store, false otherwise.
  • clearStore():
    • Removes all registered DOM element references from the store, effectively resetting it.

Examples

Example 1: Basic Usage

Input:
// Assume 'divElement1' and 'spanElement2' are actual DOM element references
// obtained from a document (e.g., via document.getElementById, document.querySelector)

// Initialize the store
store = new DOMElementStore()

// Register elements
result1 = store.registerElement("myDiv", divElement1)
result2 = store.registerElement("mySpan", spanElement2)

// Check for existence
exists1 = store.hasElement("myDiv")
exists2 = store.hasElement("nonExistent")

// Retrieve elements
retrievedDiv = store.getElement("myDiv")
retrievedSpan = store.getElement("mySpan")

// Verify retrieved element is the same reference
areSame = (retrievedDiv === divElement1)

Output:

result1: true
result2: true
exists1: true
exists2: false
retrievedDiv: <reference to divElement1>
retrievedSpan: <reference to spanElement2>
areSame: true

Explanation: The store successfully registers two elements, correctly identifies their presence and absence, and retrieves the original references.

Example 2: Error Handling and Edge Cases

Input:
// Assume 'pElement1' and 'anotherPElement' are distinct DOM element references

store = new DOMElementStore()

// Register an element
store.registerElement("myParagraph", pElement1)

// Try to register the same key again
duplicateRegister = store.registerElement("myParagraph", anotherPElement)

// Try to retrieve a non-existent key
nonExistentElement = store.getElement("someOtherID")

// Try to unregister a non-existent key
unregisterFailed = store.unregisterElement("nonExistentKey")

// Successfully unregister
unregisterSuccess = store.unregisterElement("myParagraph")

// Try to retrieve after unregistration
retrievedAfterUnregister = store.getElement("myParagraph")

Output:

duplicateRegister: false (or throws an error)
nonExistentElement: null (or undefined)
unregisterFailed: false (or throws an error)
unregisterSuccess: true
retrievedAfterUnregister: null (or undefined)

Explanation: The store correctly handles attempts to register duplicate keys, retrieves non-existent elements, and deals with unregistration of both existing and non-existing keys.

Example 3: Clear Store

Input:
// Assume 'buttonElement1' and 'inputElement2' are DOM element references

store = new DOMElementStore()

// Register elements
store.registerElement("actionButton", buttonElement1)
store.registerElement("dataInput", inputElement2)

// Verify they are present
hasButton = store.hasElement("actionButton")
hasInput = store.hasElement("dataInput")

// Clear the store
store.clearStore()

// Verify they are no longer present
hasButtonAfterClear = store.hasElement("actionButton")
hasInputAfterClear = store.hasElement("dataInput")

Output:

hasButton: true
hasInput: true
hasButtonAfterClear: false
hasInputAfterClear: false

Explanation: The clearStore method successfully empties all registered elements, and subsequent checks confirm their absence.

Constraints

  • The store should be able to hold up to 10,000 unique DOM element references concurrently.
  • Keys must be non-empty strings.
  • The stored "elements" should be treated as opaque references to DOM objects; actual DOM manipulation is outside the scope of this challenge.
  • getElement and hasElement operations should have an average time complexity close to O(1).
  • The solution should not use any external libraries or frameworks for the core storage mechanism.

Notes

  • Consider using a hash map (or dictionary/object in many languages) as the underlying data structure for efficient key-value storage.
  • The challenge focuses on managing references to DOM elements, not on creating or manipulating the DOM itself. For testing purposes, you might simulate DOM elements or use placeholder objects if working in environments without a full DOM environment.
  • Think about how to make the DOMElementStore reusable (e.g., a class, a module with factory functions, or a singleton pattern).
  • The element parameter in registerElement is a placeholder for an actual DOM element reference. Your implementation should be capable of storing and returning these references without modification.
Loading editor...
plaintext