Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Scoped / Getter / Setter vs Proxy vs Events
(version: 0)
Comparing performance of:
plain object vs test scope vs scope 2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let pp = () => { let a = 1 } //scope = (() => { function S() { this.get = {} }; function E() { this.jj = () => { let a = 1 } this.set = (v) => { this.get = v this.jj() } } S.prototype = new E() scope = new S() function SP() { this.get = {} this.jj = () => { let a = 1 } this.set = (v) => { this.get = v this.jj() } }; scopeA = new SP() // const set = (value) => {data0.test = value; pp()} // return data0 //})(); data1 = {}; handler = { get: (target, prop) => (target[prop]), set: (target, prop, value) => { target[prop] = value; pp() }, }; proxy = new Proxy(data1, handler); data2 = { get test() { return this._test; }, set test(value) { this._test = value; pp() } }; 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:
plain object
data4.test = Math.random(); a += data4.test;
test scope
scope.set(Math.random()) a+= scope.get
scope 2
scopeA.set(Math.random()) a+= scopeA.get
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
plain object
test scope
scope 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations. **Benchmark Overview** The benchmark is designed to compare the performance of three approaches: using a plain object, using a scope with getter and setter methods, and using a proxy. The test cases measure the execution time of simple assignments to a property named "test" on each object. **Options Compared** 1. **Plain Object**: A simple JavaScript object without any additional features or optimizations. 2. **Scope with Getter and Setter**: An object that uses a scope with getter and setter methods, which are called when accessing or modifying the "test" property. 3. **Proxy**: An object that uses a proxy to intercept assignments to its properties. **Pros and Cons** 1. **Plain Object**: * Pros: Simple and straightforward implementation, no additional overhead. * Cons: No optimization for frequent assignments or lookups. 2. **Scope with Getter and Setter**: * Pros: Can provide some performance benefits by avoiding direct property access, which can be slower due to the way JavaScript engines execute code. * Cons: May introduce unnecessary function calls and potential overhead for infrequent assignments or lookups. 3. **Proxy**: * Pros: Can provide significant performance benefits by intercepting assignments and minimizing direct property access, which can be slower. * Cons: Requires more complex implementation and may introduce additional overhead due to the proxy object itself. **Library Usage** None of the test cases use any external libraries. However, it's worth noting that JavaScript engines like V8 (used by Chrome) have some built-in optimizations for accessing properties on objects, which might affect the results of this benchmark. **Special JS Features/Syntax** The test cases do not explicitly use any special JavaScript features or syntax beyond standard ES5/ES6 syntax. However, it's worth noting that some modern JavaScript engines may optimize certain code paths differently due to their internal optimizations and caching mechanisms. **Other Considerations** * The benchmark uses a single value (`Math.random()`) for the initial assignment to the "test" property. In a real-world scenario, this value would likely be a more complex expression or even an object reference. * The test cases are designed to measure the performance of assigning a value to a property named "test". Other properties or methods on the objects might affect the results differently. * The benchmark is run on multiple browsers and devices, which can introduce variations in execution times due to differences in engine optimizations and caching mechanisms. **Alternative Approaches** Other approaches that could be used for this benchmark include: 1. Using a custom-built virtual machine (e.g., SpiderMonkey) or interpreter to measure the performance of each approach. 2. Implementing the three approaches using different languages, such as C++ or Rust, to compare their performance at the language level. 3. Using a more comprehensive testing framework that covers additional use cases and edge scenarios. Keep in mind that these alternative approaches might not be feasible for a web-based benchmark like MeasureThat.net, which relies on browser engines and JavaScript interpreters.
Related benchmarks:
Proxy vs Object vs Object.setPrototypeOf
ES6 Proxy vs JS Object get/set vs get/set Function
Proxy.get(prop) vs obj[prop]
proxy vs getter/setter vs prototype getter/setter
Comments
Confirm delete:
Do you really want to delete benchmark?