Skip to content

Instantly share code, notes, and snippets.

@chearon
Created May 25, 2018 23:31
Show Gist options
  • Save chearon/d09dceeeca8f8e98e2cff7e4e3ccb10a to your computer and use it in GitHub Desktop.
Save chearon/d09dceeeca8f8e98e2cff7e4e3ccb10a to your computer and use it in GitHub Desktop.
Test frozen props vs unfrozen
const Vue = require('vue');
bigarray = [];
for (let i = 0; i < 30000; ++i) {
bigarray.push({a: Math.random(), b: Math.random(), c: Math.random(), d: Math.random(), e: Math.random(), f: Math.random(), g: Math.random(), h: Math.random(), i: Math.random()});
}
let freeze = false;
/**
* Pretend like this is the store
*/
store = new Vue({
data: {bigarray},
computed: {
filtered() {
const ret = this.bigarray.filter((item, i) => {
item.a; // pretend like we depend
item.i; // on some things
return i % 2 === 0;
});
return freeze ? Object.freeze(ret) : ret;
},
}
});
function touchArray() {
for (const item of bigarray) {
item.a = Math.random();
}
}
function test() {
console.time(`freeze=${freeze}`);
const vm = new Vue({
props: ["list"],
propsData: {list: store.filtered},
computed: {
sorted() {
return this.list.slice().sort((a, b) => a.i - b.i);
}
}
});
vm.sorted;
console.timeEnd(`freeze=${freeze}`);
}
freeze = true;
test(), touchArray();
test(), touchArray();
test(), touchArray();
test(), touchArray();
test(), touchArray();
freeze = false;
test(), touchArray();
test(), touchArray();
test(), touchArray();
test(), touchArray();
test(), touchArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment