Created
May 25, 2018 23:31
-
-
Save chearon/d09dceeeca8f8e98e2cff7e4e3ccb10a to your computer and use it in GitHub Desktop.
Test frozen props vs unfrozen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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