Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lazy getters vs proxy vs property
(version: 1)
Comparing performance of:
lazy1 vs lazy2 vs proxy vs property
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function lazy1(prototype, propertyKey, descriptor) { const originalFunction = descriptor.get; descriptor.get = function() { const val = originalFunction.apply(this, arguments); Object.defineProperty(this, propertyKey, { value: val }); return val; } } function lazy2(prototype, propertyKey, descriptor) { const originalFunction = descriptor.get; const cachedKey = `__lazy__${propertyKey}`; descriptor.get = function() { if (!this.hasOwnProperty(cachedKey)) { this[cachedKey] = originalFunction.apply(this, arguments); } return this[cachedKey]; }; } class A { constructor(){ this.w = 1; } get x() { return 1; } get y() { return 1; } } lazy1(A.prototype, 'x', Object.getOwnPropertyDescriptor(A.prototype, 'x')); lazy2(A.prototype, 'y', Object.getOwnPropertyDescriptor(A.prototype, 'y')); const b = new Proxy({},{get(target, key){return 1;}}); const a = new A();
Tests:
lazy1
let tot = 0; while(tot < 100) tot+=a.x;
lazy2
let tot = 0; while(tot < 100) tot+=a.y;
proxy
let tot = 0; while(tot < 100) tot+=b.z;
property
let tot = 0; while(tot < 100) tot+=a.w;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lazy1
lazy2
proxy
property
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lazy1
10691199.0 Ops/sec
lazy2
10723576.0 Ops/sec
proxy
627352.3 Ops/sec
property
8863970.0 Ops/sec
Related benchmarks:
Prototypal property access vs passing cached value
cache vs hard
Read property reflect vs getOwnPropertyDescriptor
ES6 Proxy vs JS Object get/set vs get/set Function
Cached Getter (class) vs Getter vs Proxy
getOwnPropertyDescriptors vs getOwnPropertyNames
Proxy.get(prop) vs obj[prop]
Another Proxy Test
making proxies vs defining getters properties
Comments
Confirm delete:
Do you really want to delete benchmark?