Home > Article > Web Front-end > What is the use of vue data freezing?
In vue, the data freezing "Object.freeze()" method is used to freeze the object, and it is prohibited to modify the properties of the object (since arrays are also objects in nature, this method can be used on arrays). After an object is frozen, existing attributes cannot be deleted, the enumerability, configurability, and writability of the object's existing attributes cannot be modified, and the values of existing attributes cannot be modified; in addition, after freezing an object, the object's prototype It cannot be modified either.
The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.
When introducing data binding and response in Vue's documentation, it is specifically noted that objects that pass the Object.freeze() method cannot respond to updates. Therefore, I specifically checked the specific meaning of the Object.freeze() method.
The Object.freeze() method is used to freeze the object, and it is prohibited to modify the properties of the object (because array is also an object in nature
, so the method can be used on arrays). In Mozilla MDN, it is introduced as follows:
You can freeze an object. A frozen object can no longer be modified; if an object is frozen, new attributes cannot be added to the object,
cannot delete existing attributes, and cannot modify the enumerability of existing attributes of the object. Configurability, writability, and the inability to modify the values of existing properties. In addition, the prototype of an object cannot be modified after freezing it
The return value of this method is the parameter itself.
It is important to note the following two points
1. Object.freeze() is different from const variable declarations and does not assume the functions of const.
const is completely different from Object.freeze()
The following code is correct:
2. Object.freeze() is "shallow freezing", and the following code is valid:
General usage
Obviously, a The prop attribute has not been changed, even if it is reassigned.
"Deep Freeze"
To fully freeze an object with nested properties, you can write your own library or use an existing Library to freeze objects, such as Deepfreeze or immutable-js
// 深冻结函数. function deepFreeze(obj) { // 取回定义在obj上的属性名 var propNames = Object.getOwnPropertyNames(obj); // 在冻结自身之前冻结属性 propNames.forEach(function(name) { var prop = obj[name]; // 如果prop是个对象,冻结它 if (typeof prop == 'object' && prop !== null) deepFreeze(prop); }); // 冻结自身(no-op if already frozen) return Object.freeze(obj); }
is actually a simple recursive method. But it involves a very important but rarely used knowledge point when writing business logic Object.getOwnPropertyNames(obj)
. We all know that there are prototype chain properties in JS Object, and all non-prototype chain properties can be obtained through this method.
Object.freeze()
Improve performanceIn addition to component optimization, we can also start with the dependency transformation of vue. During initialization, Vue will perform getter and setter transformations on data. In modern browsers, this process is actually quite fast, but there is still room for optimization.
Object.freeze()
You can freeze an object. After freezing, you cannot add new attributes to the object, modify the values of its existing attributes, delete existing attributes, and cannot Modify the enumerability, configurability, and writability of existing attributes of the object. This method returns the frozen object.
When you pass a normal JavaScript object to the data
option of the Vue instance, Vue will iterate through all the properties of the object and use Object.defineProperty to define these Properties are all converted to getters/setters. These getters/setters are invisible to the user, but internally they allow Vue to track dependencies and notify changes when properties are accessed and modified.
但 Vue 在遇到像 Object.freeze()
这样被设置为不可配置之后的对象属性时,不会为对象加上 setter getter 等数据劫持的方法。参考 Vue 源码
Vue observer 源码
在基于 Vue 的一个 big table benchmark 里,可以看到在渲染一个一个 1000 x 10 的表格的时候,开启Object.freeze()
前后重新渲染的对比。
big table benchmark
开启优化之前
开启优化之后
在这个例子里,使用了 Object.freeze()
比不使用快了 4 倍
Object.freeze()
的性能会更好不使用Object.freeze()
的CPU开销
使用 Object.freeze()
的CPU开销
对比可以看出,使用了 Object.freeze()
之后,减少了 observer 的开销。
Object.freeze()
应用场景由于 Object.freeze()
会把对象冻结,所以比较适合展示类的场景,如果你的数据属性需要改变,可以重新替换成一个新的 Object.freeze()
的对象。
修改 React props React生成的对象是不能修改props的, 但实践中遇到需要修改props的情况. 如果直接修改, js代码将报错, 原因是props对象被冻结了, 可以用Object.isFrozen()来检测, 其结果是true. 说明该对象的属性是只读的.
那么, 有方法将props对象解冻, 从而进行修改吗?
事实上, 在javascript中, 对象冻结后, 没有办法再解冻, 只能通过克隆一个具有相同属性的新对象, 通过修改新对象的属性来达到目的.
可以这样:
ES6: Object.assign({}, frozenObject); lodash: _.assign({}, frozenObject);
来看实际代码:
function modifyProps(component) { let condictioin = this.props.condictioin, newComponent = Object.assign({}, component), newProps = Object.assign({}, component.props) if (condictioin) { if (condictioin.add) newProps.add = true if (condictioin.del) newProps.del = true } newComponent.props = newProps return newComponent }
锁定对象的方法
no new properties or methods can be added to the project 对象不可扩展, 即不可以新增属性或方法, 但可以修改/删除
same as prevent extension, plus prevents existing properties and methods from being deleted 在上面的基础上,对象属性不可删除, 但可以修改
same as seal, plus prevent existing properties and methods from being modified 在上面的基础上,对象所有属性只读, 不可修改
以上三个方法分别可用Object.isExtensible(), Object.isSealed(), Object.isFrozen()来检测
当一个 Vue 实例被创建时,它向 Vue 的响应式系统中加入了其 data 对象中能找到的所有的属性。当这些属性的值发生改变时,视图将会产生“响应”,即匹配更新为新的值。但是如果使用 Object.freeze(),这会阻止修改现有的属性,也意味着响应系统无法再追踪变化。
具体使用办法举例:
<template> <div> <p>freeze后会改变吗 {{obj.foo}} </p> <!-- 两个都不能修改??为什么?第二个理论上应该是可以修改的--> <button @click="change">点我确认</button> </div> </template> <script> var obj = { foo: '不会变' } Object.freeze(obj) export default { name: 'index', data () { return { obj: obj } }, methods: { change () { this.obj.foo = '改变' } } } </script>
运行后:
从报错可以看出只读属性foo不能进行修改,Object.freeze()冻结的是值,你仍然可以将变量的引用替换掉,将上述代码更改为:
<button @click="change">点我确认</button> change () { this.obj = { foo: '会改变' } }
Object.freeze()是ES5新增的特性,可以冻结一个对象,冻结指的是不能向这个对象添加新的属性,不能修改其已有属性的值,不能删除已有属性,以及不能修改该对象已有属性的可枚举性、可配置性、可写性。防止对象被修改。 如果你有一个巨大的数组或Object,并且确信数据不会修改,使用Object.freeze()可以让性能大幅提升。
Object.freeze()是ES5新增的特性,可以冻结一个对象,防止对象被修改。
vue 1.0.18+对其提供了支持,对于data或vuex里使用freeze冻结了的对象,vue不会做getter和setter的转换。
如果你有一个巨大的数组或Object,并且确信数据不会修改,使用Object.freeze()可以让性能大幅提升。在我的实际开发中,这种提升大约有5~10倍,倍数随着数据量递增。
并且,Object.freeze()冻结的是值,你仍然可以将变量的引用替换掉。举个例子:
<p v-for="item in list">{{ item.value }}</p>
new Vue({ data: { // vue不会对list里的object做getter、setter绑定 list: Object.freeze([ { value: 1 }, { value: 2 } ]) }, created () { // 界面不会有响应 this.list[0].value = 100; // 下面两种做法,界面都会响应 this.list = [ { value: 100 }, { value: 200 } ]; this.list = Object.freeze([ { value: 100 }, { value: 200 } ]); } })
vue的文档没有写上这个特性,但这是个非常实用的做法,对于纯展示的大数据,都可以使用Object.freeze提升性能。
The above is the detailed content of What is the use of vue data freezing?. For more information, please follow other related articles on the PHP Chinese website!