data1 = {};
handler = {
get: (target, prop) => (target[prop]),
set: (target, prop, value) => console.error("IMMUTABLE!!!"),
};
proxy = new Proxy(data1, handler);
data2 = {
get test() {
return this._test;
},
set test(value) {
return this._test = value;
}
};
const EVENTS = new WeakMap();
function pub(object, event, data) {
if (!EVENTS.has(object)) { return; }
const callbacks = EVENTS.get(object)[event];
if (callbacks) {
for (const callback of callbacks) {
callback(data);
}
}
}
function sub(object, event, callback) {
let dict;
if (!EVENTS.has(object)) {
EVENTS.set(object, dict={});
} else {
dict = EVENTS.get(object);
}
let evList;
if (event in dict) {
(evList = dict[event]).push(callback);
} else {
evList = dict[event] = [callback];
}
return function unsub() {
evList.splice(evList.indexOf(callback), 1);
}
}
data3 = {};
sub(data3, 'set-test', v => data3.test = v);
sub(data3, 'get-test', () => pub(data3, 'getting-test', data3.test));
sub(data3, 'getting-test', v => a += v);
var a = 0;
data4 = {};
data5 = {};
sub(data5, 'set-test', v => { data5.test = v; pub(data5, 'getting-test', data3.test); });
sub(data5, 'getting-test', v => a += v);
data2.test = Math.random();
a += data2.test;
proxy.test = Math.random();
a += proxy.test;
pub(data3, 'set-test', Math.random());
pub(data3, 'get-test');
data4.test = Math.random();
a += data4.test;
// note that is almost certainly not accurate since it probably ends after this function is called, not when getting-test triggers
pub(data3, 'set-test', Math.random());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getter / setter | |
proxy | |
events | |
plain object | |
events (kinda cheating) |
Test name | Executions per second |
---|---|
getter / setter | 1188862.4 Ops/sec |
proxy | 132470.1 Ops/sec |
events | 689065.4 Ops/sec |
plain object | 1660932.4 Ops/sec |
events (kinda cheating) | 1803649.2 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark with five test cases, each testing different approaches for updating values in an object.
Test Cases Explanation
test
property of an object (data2
). It checks how fast the value is updated.proxy
). It checks how fast the value is updated when accessed through the proxy.pub
function). It checks how fast the value is updated when triggered by an event.data4
). It checks how fast the value is updated.pub
function, but with a modified implementation that bypasses some optimizations.Library and Syntax
The provided JSON uses the following libraries and syntax:
Proxy
: built-in JavaScript Proxy API, used to create a proxy object (proxy
).WeakMap
: built-in JavaScript WeakMap data structure, used to store event listeners (EVENTS
).pub
function: custom function that broadcasts events to registered listeners.sub
function: custom function that registers a listener for an event.The JSON also uses some special syntax:
let dict = EVENTS.get(object)
(WeakMap lookup)let evList = dict[event]
(array indexing)Other Considerations
When interpreting the benchmark results, consider the following factors:
When considering alternative approaches or optimizing these tests, keep in mind that:
It's essential to consider these factors when deciding which approach to use in your own JavaScript applications.