Coding, Insights, and Digital Discoveries π©π»βπ»
From Stay-at-Home Mom to a Developer After 50
While building a To-Do List app, I encountered a common challenge: how to efficiently display and switch between active and completed tasks. This led me to explore the relationship between memory management and web performance.
At first, I had two possible approaches:
<ul>
and dynamically change its content when switching between active and completed tasks.<ul>
elements, one for active tasks and one for completed tasks, and toggle their visibility.Each approach has its own pros and cons, but to make an informed decision, I had to revisit a crucial concept: How memory works in a web page and how it impacts performance.
The Document Object Model (DOM) consumes memory in several ways:
JavaScript's memory management affects performance through:
In this approach, I keep only one list element (<ul>
) and update its content dynamically when switching tabs.
function toggleList(tabName) {
taskList.innerHTML = "";
taskList.append(...(tabName === "active" ? activeItems : completedItems));
}
β Pros:
β Cons:
In this approach, I keep two <ul>
elements (one for active tasks, one for completed tasks) and show/hide them based on the selected tab.
function toggleList(tabName) {
activeList.style.display = tabName === "active" ? "block" : "none";
completedList.style.display = tabName === "active" ? "none" : "block";
}
β Pros:
β Cons:
A good compromise is to cache the task lists in JavaScript memory and update the DOM only when necessary:
class TaskManager {
constructor() {
this.lists = { active: [], completed: [] };
this.container = document.querySelector('.task-list');
}
toggleList(tabName) {
const fragment = document.createDocumentFragment();
this.lists[tabName].forEach(task => {
const li = document.createElement('li');
li.textContent = task.text;
fragment.appendChild(li);
});
this.container.replaceChildren(fragment);
}
}
This reduces DOM memory usage while keeping quick tab switching.
Modern frameworks like React solve this problem using Virtual DOM:
Monitor these metrics using Chrome DevTools:
Small Lists (less than 100 items)
Medium Lists (100-1000 items)
Large Lists (>1000 items)
These patterns ensure optimal performance while maintaining code maintainability. The key is choosing the right approach based on your specific requirements and scale.
Since this is a To-Do List app with a limited number of items in each list, the two <ul>
approach (toggling visibility) is the better choice. A small number of items wonβt significantly impact memory usage or page speed, and keeping both lists in the DOM allows for faster switching while keeping the code simpler and more maintainable.