Last active
July 23, 2020 22:10
-
-
Save fritzy/e4deac9064052e1bc30c0ecd4ebd91e8 to your computer and use it in GitHub Desktop.
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
let tick = 0; | |
class BasicObject { | |
constructor() { | |
this.x = 0; | |
this.y = 0; | |
this.updated = 0; | |
} | |
} | |
class ProxyObject { | |
constructor() { | |
this.x = 0; | |
this.y = 0; | |
this.updated = 0; | |
return new Proxy(this, { | |
get: function(o, p) { | |
return this[p]; | |
}, | |
set: function(o, p, v) { | |
this[p] = v; | |
this.updated = tick; | |
} | |
}); | |
} | |
} | |
class HandledObject { | |
constructor() { | |
this._x = 0; | |
this._y = 0; | |
this.updated = 0; | |
Object.defineProperty(this, 'x', { | |
get: function() { | |
return this._x; | |
}, | |
set: function(v) { | |
this._x = v; | |
this.updated = tick; | |
} | |
}); | |
Object.defineProperty(this, 'y', { | |
get: function() { | |
return this._y; | |
}, | |
set: function(v) { | |
this._y = v; | |
this.updated = tick; | |
} | |
}); | |
} | |
} | |
class PrototypeObject { | |
constructor() { | |
this._x = 0; | |
this._y = 0; | |
this.updated = 0; | |
} | |
} | |
Object.defineProperty(PrototypeObject.prototype, 'x', { | |
get: function() { | |
return this._x; | |
}, | |
set: function(v) { | |
this._x = v; | |
this.mutated = tick; | |
} | |
}); | |
Object.defineProperty(PrototypeObject.prototype, 'y', { | |
get: function() { | |
return this._y; | |
}, | |
set: function(v) { | |
this._y = v; | |
this.updated = tick; | |
} | |
}); | |
function Benchmark(klass) { | |
const insts = []; | |
let label = `new ${klass.name} ` | |
console.time(label); | |
for (let i = 0; i < 50000; i++) { | |
insts.push(new klass); | |
} | |
console.timeEnd(label); | |
label = `mutate ${klass.name}` | |
console.time(label); | |
for (let i = 0; i < 50000; i++) { | |
tick = i; | |
insts[i].x = 3; | |
insts[i].y = 4; | |
console.assert(insts[i].x === 3); | |
console.assert(insts[i].y === 4); | |
} | |
console.timeEnd(label); | |
} | |
Benchmark(Object); | |
Benchmark(BasicObject); | |
Benchmark(ProxyObject) | |
Benchmark(HandledObject); | |
Benchmark(PrototypeObject); | |
/* | |
Windows Home 10 Build 19041 | |
Processor AMD Ryzen 5 3600 6-Core Processor, 3593 Mhz, 6 Core(s), 12 Logical Processor(s) | |
RAM 16GB DDR4 3200MHZ | |
Node v14.40 WSL2 | |
===================================================== | |
new Object : 8.041ms | |
mutate Object: 36.938ms | |
new BasicObject : 4.887ms | |
mutate BasicObject: 36.533ms | |
new ProxyObject : 20.307ms | |
mutate ProxyObject: 86.779ms | |
new HandledObject : 78.789ms | |
mutate HandledObject: 53.416ms | |
new PrototypeObject : 8.596ms | |
mutate PrototypeObject: 38.53ms | |
Firefox Nightly 80.0a1 (2020-07-22) (64-bit) | |
===================================================== | |
new Object : 11ms - timer ended | |
mutate Object: 15ms - timer ended | |
new BasicObject : 3ms - timer ended | |
mutate BasicObject: 15ms - timer ended | |
new ProxyObject : 32ms - timer ended | |
mutate ProxyObject: 57ms - timer ended | |
new HandledObject : 68ms - timer ended | |
mutate HandledObject: 66ms - timer ended | |
new PrototypeObject : 7ms - timer ended | |
mutate PrototypeObject: 22ms - timer ended | |
Chrome Version 84.0.4147.89 (Official Build) (64-bit) | |
===================================================== | |
new Object : 6.2080078125ms | |
mutate Object: 3.088134765625ms | |
new BasicObject : 9.509033203125ms | |
mutate BasicObject: 3.234130859375ms | |
new ProxyObject : 34.458984375ms | |
mutate ProxyObject: 66.751953125ms | |
new HandledObject : 62.54296875ms | |
mutate HandledObject: 26.198974609375ms | |
new PrototypeObject : 1.956787109375ms | |
mutate PrototypeObject: 6.017822265625ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment