Created
April 4, 2018 08:03
-
-
Save WebReflection/6c8b36c4ad6fc64cfb22a16d59a66c48 to your computer and use it in GitHub Desktop.
Benchmark for prototypal VS classic state management
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 bench = { | |
stress: 10e2, | |
times: 5, | |
classical(copy, overwrite) { | |
this._prototype = false; | |
this._bootstrap(copy, overwrite); | |
}, | |
prototypal(copy, overwrite) { | |
this._prototype = true; | |
this._bootstrap(copy, overwrite); | |
}, | |
add(start, many) { | |
const create = this._prototype ? Object.create : this.copy; | |
const length = start * many + many; | |
const random = Math.random; | |
for (let i = start * many; i < length; i++) { | |
this.state = create(this.state); | |
this.state[i] = random(); | |
} | |
}, | |
copy(state) { | |
const copy = {}; | |
for (const k in state) { | |
copy[k] = state[k]; | |
} | |
return copy; | |
}, | |
mark(i) { | |
if (i === this.times) return; | |
console.time('create new states'); | |
this.add(this._overwrite ? 0 : i, this.stress); | |
console.timeEnd('create new states'); | |
setTimeout( | |
() => { | |
if (this._copy) this.state = this.copy(this.state); | |
console.time('retrieve a random top 10 state'); | |
this.last = this.state[Math.floor(Math.random() * 10)]; | |
console.timeEnd('retrieve a random top 10 state'); | |
console.log(this.last); | |
this.mark(i + 1); | |
}, | |
100 | |
); | |
}, | |
_bootstrap(copy, overwrite) { | |
this._copy = !!copy; | |
this._overwrite = !!overwrite; | |
this.state = {}; | |
setTimeout(() => this.mark(0)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment