0

I would like to cache data from remote database locally to use it in my javascript application. I do not need any excesses such as SQL requests to local data, so I decide just to store database records in array of objects in the following way:

    <script type="text/javascript">
 var json0 = '{"1" : {"fname": "fname1", "sname": "sname1"}, "2" : {"fname": "fname2", "sname": "sname2"}}';
 var json1 = '{"2" : {"data": "11/05/2014", "time": "11:30:18", "person": "data0[1]"}, "6" : {"data": "24/06/2014", "time": "16:11:05", "person": "data0[2]"}, "8" : {"data": "11/10/2014", "time": "12:15:27", "person": "data0[1]"}}';
 var data0 = JSON.parse(json0);
 var data1 = JSON.parse(json1, function (k, v) {
    try {
      return  eval(v); 
    } catch (e) {
        return v;
    }         
});
 console.log(data1);
</script>

The question is about the memory usage. If the objects in array data1 will store the reference to the objects from data0 in the property "person" or they will store the copy of these objects?

  • Have a look at localStorage (which is available to most modern browsers) : https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage –  Oct 17 '14 at 09:36

1 Answers1

1

as reducing version of your example shows, data0 will be stored by reference.

var tt = {f1: 'aa', f2: 'bb'}
var rr = eval('tt');

console.log(rr.f1); // 'aa'
rr.f1 = 'cc';
console.log(tt.f1); // 'cc'

But using eval(..) is bad way: Why is using the JavaScript eval function a bad idea? . Use local storage instead as jeff said.

Community
  • 1
  • 1
weeklyTea
  • 316
  • 2
  • 10