# Any links to discussions around Object.deepFreeze?

**URL:** <https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652>\
**Category:** I have questions\
**Created:** [February 20, 2021, 3:55pm UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652 "2021-02-20T15:55:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 20, 2021, 3:55pm UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/1 "2021-02-20T15:55:19Z")

</div>

In the tc39 notes from 2021/01/25 there was discussion around the mutability of JSON modules. It was mentioned that projects could opt-in to immutability with:

> you can simply be the first importer and then you can freeze the object

[source](https://github.com/tc39/notes/blob/34be73a9a3aef9a87f74ef4db11ce8bf63625c09/meetings/2021-01/jan-25.md#:~:text=then%20you%20can%20freeze%20the%20object)

Deeply freezing an object is non trivial (though easier when source object is known to have come from JSON), so is commonly done by importing a library.

**Question:** Does anyone have any links to previous discussions about adding `Object.deepFreeze`

Foreseen issues that have probably already come up:

- Would it handle cycles?
  - Tracking cycles will consume more memory but avoids infinite loops

- Would it freeze prototypes?
  - Should it visit just own properties?
  - Should it freeze `Array.prototype` when obj contains an array?

- Would it be configurable?
  - It could take an argument similar to `JSON.stringify', an array of props to visit

- Preference for using `Record` for deep immutability

EDIT: rephrased post as a question instead of an idea

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [February 20, 2021, 4:09pm UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/2 "2021-02-20T16:09:14Z")

</div>

Handling cycles seems like an obvious thing to do, but I don't think there's any reasonable choice to be made about freezing prototypes. If it _does_, then it could have unintended side effects far beyond the user's intention; if it _doesn't_, then it's not actually deeply freezing the object.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [February 20, 2021, 11:02pm UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/3 "2021-02-20T23:02:37Z")

</div>

Is there much of a use case for deep freezing besides parsing JSON (either with JSON.parse, or with JSON modules)?

I could see cases where it might be convenient to deep freeze a nested object literal, but that gets dangerous if you ever want to add some mutable instance of a class to that nested structure. And tuples/records would remove this use case.

As an aside: The following code is a little slow, but would allow one to copy a data-only object and deep freeze it.

```javascript
JSON.parse(JSON.stringify(data), (_, value) => Object.freeze(value))

```

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [February 21, 2021, 12:23am UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/4 "2021-02-21T00:23:57Z")

</div>

I guess there are scenarios when you may be receiving a nested data structure from a library that you would like to freeze (i.e. maybe a request body while in node).

If we do implement a deepFreeze function, it could pave a standard way to implement other deep operations, such as deep cloning, or deep equality checking.

---

<div class="post-metadata">

**Author:** ![lygstate](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lygstate/32/628_2.png) [@lygstate](https://es.discourse.group/u/lygstate)\
**Post date:** [February 22, 2021, 5:10am UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/5 "2021-02-22T05:10:30Z")

</div>

How about using [GitHub - tc39/proposal-record-tuple: ECMAScript proposal for the Record and Tuple value types. | Stage 2: it will change!](https://github.com/tc39/proposal-record-tuple)?  
We can convert a object to records-tuple.

---

<div class="post-metadata">

**Author:** ![lygstate](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lygstate/32/628_2.png) [@lygstate](https://es.discourse.group/u/lygstate)\
**Post date:** [February 22, 2021, 5:14am UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/6 "2021-02-22T05:14:08Z")

</div>

We can add Object.prototype.toImmutable function, so that we can gain

> **[tc39/proposal-record-tuple](https://github.com/tc39/proposal-record-tuple)**
>
> ECMAScript proposal for the Record and Tuple value types. | Stage 2: it will change!

For Array, it's turn out to be `tuple`,  
For Object, it's turn out to be `record`

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 26, 2021, 12:23pm UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/7 "2021-02-26T12:23:48Z")

</div>

FYI rephrased post as a question, not a proposal.

---

<div class="post-metadata">

**Author:** ![markm](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/markm/32/19_2.png) [@markm](https://es.discourse.group/u/markm)\
**Post date:** [March 16, 2026, 3:21am UTC](https://es.discourse.group/t/any-links-to-discussions-around-object-deepfreeze/652/8 "2026-03-16T03:21:07Z")

</div>

See discussion of `harden` at [https://es.discourse.group/t/object-deepfreeze/](https://es.discourse.group/t/object-deepfreeze/)
