I have a regular scenario, I'm getting some data from a server then spitting it out with the page for later use, however I'm thinking what would be a better way of storing it.
Usually I would do something like
<script>
window.myData = <?= json_encode($myBigData) ?>;
</script>
Now I would have my data stored in the window object which I can access later on from my scripts, however isn't that going to take up double the memory? The data will be stored once as text on the page and once as actual data in the DOM, well probably not twice as much but still would take up more memory, right?
Isn't something like this going to be a better alternative
<script>
function getMyData() {
return <?= json_encode($myBigData) ?>;
}
</script>
This way the data will be stored only as text in the page, then I Will get it once I need it, do some operations with it and JavaScript will GC it once it's done it's business as opposed to the first scenario where it would stay in memory long before I needed it and long after I used it unless I unset it manually.
I want to ask if my logic is correct or that is just some nonsense, is there any actual benefits of doing that?