var c = 0;
object = {};
class p {
set value(v){
if (c++%7==0) v += c;
this._value = v;
}
}
proxy = new p();
proxyWithHandler = new Proxy(object, {
set(target, prop, receiver) {
if (c++%7==0) prop += c;
return Reflect.set(target, prop, receiver);
}
})
object.value = 'data';
proxy.value = 'data';
proxyWithHandler.value = 'data';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object setter | |
Proxy setter | |
Proxy with set handler |
Test name | Executions per second |
---|---|
Object setter | 5262696.0 Ops/sec |
Proxy setter | 1703045.5 Ops/sec |
Proxy with set handler | 874933.1 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and discussed.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmarking test case created on MeasureThat.net. The test compares the performance of three different approaches:
Options Comparison
The benchmark compares these three options because they demonstrate different approaches to accessing and modifying attributes in JavaScript:
object.value = 'data';
).set
operation.Pros and Cons of Each Approach:
Library Usage
None of the benchmarked approaches utilize any external libraries. However, they do rely on built-in JavaScript features like Proxies and Reflect (used in the Proxy with Set Handler).
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax beyond what's necessary to test the described concepts.
Alternatives
Some alternative approaches that might be worth exploring for similar scenarios include:
Overall, the benchmark highlights the importance of understanding how different approaches can affect performance in JavaScript, especially when working with objects and attributes.