I need to give users result as a JSON Object and I cache the result.
In some cases users change this result object and the modified version is available to successive users. To avoid this I'm currently cloning result but this leads to heavy overhead as its very large JSON object.
So I want to make JSON read only for giving result and avoid clone process. How to make a JSON Object non editable ?
You can use Object.freeze() for that:
var obj = {a: 'a', b: 'b'};
var freezeObj = Object.freeze(obj);
freezeObj.a = 'c';
console.log("%j", freezeObj); // prints {"a":"a","b":"b"}
console.log("%j", obj); // prints {"a":"a","b":"b"}
console.log(obj === freezeObj); // prints true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With