Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Getter / Setter vs Proxy vs Events
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/124.0.6367.111 Mobile/15E148 Safari/604.1
Browser:
Chrome Mobile iOS 124
Operating system:
iOS 17.4
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
getter / setter
3507616.2 Ops/sec
proxy
3407188.0 Ops/sec
events
1333760.9 Ops/sec
plain object
3387968.8 Ops/sec
events (kinda cheating)
3400655.2 Ops/sec
Script Preparation code:
data1 = {}; handler = { get: (target, prop) => (target[prop]), set: (target, prop, value) => (target[prop] = value), }; proxy = new Proxy(data1, handler); data2 = { get test() { return this._test; }, set test(value) { return this._test = value; } }; const EVENTS = new WeakMap(); function pub(object, event, data) { if (!EVENTS.has(object)) { return; } const callbacks = EVENTS.get(object)[event]; if (callbacks) { for (const callback of callbacks) { callback(data); } } } function sub(object, event, callback) { let dict; if (!EVENTS.has(object)) { EVENTS.set(object, dict={}); } else { dict = EVENTS.get(object); } let evList; if (event in dict) { (evList = dict[event]).push(callback); } else { evList = dict[event] = [callback]; } return function unsub() { evList.splice(evList.indexOf(callback), 1); } } data3 = {}; sub(data3, 'set-test', v => data3.test = v); sub(data3, 'get-test', () => pub(data3, 'getting-test', data3.test)); sub(data3, 'getting-test', v => a += v); var a = 0; data4 = {}; data5 = {}; sub(data5, 'set-test', v => { data5.test = v; pub(data5, 'getting-test', data3.test); }); sub(data5, 'getting-test', v => a += v);
Tests:
getter / setter
data2.test = Math.random(); a += data2.test;
proxy
proxy.test = Math.random(); a += proxy.test;
events
pub(data3, 'set-test', Math.random()); pub(data3, 'get-test');
plain object
data4.test = Math.random(); a += data4.test;
events (kinda cheating)
// note that is almost certainly not accurate since it probably ends after this function is called, not when getting-test triggers pub(data3, 'set-test', Math.random());