var cp = 0;
var cs = 0;
var object = {};
class Settable {
set value(v) {
if (cs++ % 7 == 0) v += cs;
this._value = v;
}
};
var setter = new Settable();
var proxy = new Proxy({}, {
set(target, prop, receiver) {
if (cp++ % 7 == 0) prop += cp;
return Reflect.set(target, prop, receiver);
}
});
object.value = 'data';
setter.value = 'data';
proxy.value = 'data';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object param | |
class setter | |
Proxy setter |
Test name | Executions per second |
---|---|
Object param | 10230804.0 Ops/sec |
class setter | 2334002.0 Ops/sec |
Proxy setter | 1254927.5 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of three different approaches for setting a value on an object in JavaScript:
object.value = 'data';
).setter.value = 'data';
).proxy.value = 'data';
).Library and Features
In this benchmark, the following JavaScript features/libraries are used:
There are no special JS features or syntaxes being tested in this benchmark. The focus is solely on comparing the performance of these three approaches.
Benchmarked Options
The three options being compared are:
Pros and Cons of Each Approach
Benchmark Preparation Code
The provided Script Preparation Code defines three variables:
cp
(counter): Used in the Proxy setter method to increment a value when setting the property.cs
(counter): Used in the Class setter method to increment a value when setting the property.object
: An empty object on which the tests will be performed.setter
and proxy
: Instances of the Settable class and a Proxy object, respectively.Benchmark Test Cases
The three test cases are defined as separate objects, each specifying a different benchmark definition:
object.value = 'data';
).setter.value = 'data';
).proxy.value = 'data';
).Benchmark Results
The latest benchmark results show the performance of each test case across different browsers and devices:
Test Name | Browser | Device Platform | Operating System | Executions Per Second |
---|---|---|---|---|
Object param | Opera 78 | Desktop | Windows 7 | 10230804.0 |
Class setter | Opera 78 | Desktop | Windows 7 | 2334002.0 |
Proxy setter | Opera 78 | Desktop | Windows 7 | 1254927.5 |
Alternatives and Considerations
When deciding between these approaches, consider the following:
Keep in mind that this benchmark is focused on the specific use case of setting an object property value. Other factors might influence the performance choice in different scenarios.