var object = { value: 'foo' };
var proxy = new Proxy(object, {
get(target, prop) {
return target[prop];
},
set(target, prop, value) {
target[prop] = value;
}
});
function getData(prop) {
return object[prop];
}
function setData(prop, val) {
object[prop] = val;
}
const val = object.value;
object.value = 'bar';
const val = proxy.value;
proxy.value = 'bar';
const val = getData('value');
setData('value', 'bar');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object read/write | |
Proxy read/write | |
get/set function |
Test name | Executions per second |
---|---|
Object read/write | 66050972.0 Ops/sec |
Proxy read/write | 2075251.1 Ops/sec |
get/set function | 64830928.0 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches. In this analysis, we'll break down the provided benchmark definition JSON and individual test cases to understand what's being tested.
Benchmark Definition
The benchmark definition represents a group of tests that compare three approaches:
get
and set
methods on JavaScript objects.Proxy
constructor to create a proxy object with custom get
and set
handlers.getData
and setData
, which mimic the behavior of get and set operations.Options Compared
The benchmark compares the performance of these three approaches in different scenarios:
Proxy
.Pros and Cons
Here's a brief summary of each approach:
Library and Special JS Features
The benchmark uses JavaScript objects and the Proxy
constructor, which are built-in features. There are no external libraries or special JS features used in this benchmark.
Other Alternatives
If you're looking for alternatives to these approaches, consider the following:
In summary, the benchmark provides a simple and concise way to compare the performance of different approaches for getting and setting properties in JavaScript. By understanding the pros and cons of each approach, developers can make informed decisions about which method best suits their needs.