[HTML][JS] Data stored in memory or DOM
Where Should JSON Data Be Stored in a Web Page? — Memory vs. DOM vs. URL
How did I encounter this?
In this post, I’ll break down the difference between storing data in memory, in the DOM, and in the URL.
Storing in Memory
Storing in memory means keeping the data in a JavaScript variable inside the browser.
let searchResults = []; // Stored in memory
fetch('/api/search?keyword=...') .then(res => res.json()) .then(data => { searchResults = data; });
-
The data is not visible in the HTML or browser address bar.
-
It’s fast and temporary — lives only while the page is open.
It disappears when the page is refreshed or navigated away.
-
Best for managing structured or sensitive data like game metadata.
Storing in the DOM
The DOM (Document Object Model) is what the browser uses to render the page. Data can be stored there by embedding it in HTML elements.
Examples:
<!-- Visible --><div>God of War</div>
<!-- Hidden but stored in DOM --><span data-code="Q12345" hidden></span>
Or via JavaScript:
document.getElementById('gameBox').dataset.gameId = "Q12345";
-
Can be seen in the browser’s Developer Tools.
-
Slower and more exposed than memory.
-
Useful for small values directly linked to HTML elements.
Storing in the URL
Sometimes data is placed in the URL as a query string:
<a href="/add?title=God+of+War&release=2022">Add</a>
-
Visible in browser history, logs, and even bookmarks.
-
Not safe for large or sensitive data.
-
Should be avoided for things like structured JSON.
Comments
Post a Comment