var object = { value: 'foo' };
var proxy = new Proxy(object, {
get(target, prop) {
return target[prop];
},
set(target, prop, value) {
target[prop] = value;
}
});
const val = object.value;
object.value = 'bar';
const val = proxy.value;
proxy.value = 'bar';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object read/write | |
Proxy read/write |
Test name | Executions per second |
---|---|
Object read/write | 94340448.0 Ops/sec |
Proxy read/write | 2443091.5 Ops/sec |
Let's break down the provided benchmark and its test cases.
What is being tested?
The benchmark compares two approaches:
object
).proxy
) with custom getter and setter functions.Options compared:
object.value
) to read and write values.get
property and a custom setter function for the set
property.Pros and Cons:
Library and Purpose:
The Proxy
object is a built-in JavaScript library that allows you to create custom access controls for objects. The purpose of using Proxy
in this benchmark is to demonstrate its flexibility and utility in controlling property access and modification.
Special JS Feature or Syntax:
This benchmark uses the new Proxy()
constructor, which is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). This syntax allows you to create a proxy object with custom properties and behavior. The getter and setter functions used in this benchmark are also ES6 features.
Other Alternatives:
If you prefer not to use the Proxy
object or want to explore alternative approaches, here are some options:
decorator
or class-decorator
, provide a way to create decorators that can be used to control property access and modification.Keep in mind that these alternatives may introduce additional complexity or overhead compared to using the Proxy
object.