Skip to content

Instantly share code, notes, and snippets.

@marekpiechut
Created August 18, 2017 11:02
Show Gist options
  • Save marekpiechut/84069ac0680f047bc52b9c1fd5695c4a to your computer and use it in GitHub Desktop.
Save marekpiechut/84069ac0680f047bc52b9c1fd5695c4a to your computer and use it in GitHub Desktop.
Benchmark - prototypes
window.BENCH = ((target) => {
const MyType = function(startVal) {
this.val = startVal
this.name = "Magic Object"
this.magic = 32
}
MyType.prototype.add = function(b) {
this.val += this.magic + b
return this
}
MyType.prototype.sub = function(b) {
this.val += this.magic - b
return this
}
MyType.prototype.mul = function(b) {
this.val = this.val * b
return this
}
MyType.prototype.mod = function(b) {
this.val = this.val % b
return this
}
MyType.prototype.getVal = function() {
return this.val
}
let idx = 0
target.proto = {
start: (amount) => {
const objects = []
for(let i = 0; i < amount; i++) {
objects.push(new MyType(i))
}
BENCH.objects = objects
setInterval(() => {
const o = objects[idx]
o.add(12)
o.sub(4)
o.mul(7)
o.mod(16)
idx++
document.getElementById('target-div').innerHTML = o.getVal()
}, 10)
},
stop: () => {
if(BENCH.proto.timeoutId) {
clearInterval(BENCH.proto.timeoutId)
}
}
}
return target
})(window.BENCH || {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment