Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created April 4, 2018 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/6c8b36c4ad6fc64cfb22a16d59a66c48 to your computer and use it in GitHub Desktop.
Save WebReflection/6c8b36c4ad6fc64cfb22a16d59a66c48 to your computer and use it in GitHub Desktop.
Benchmark for prototypal VS classic state management
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