Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active October 10, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aschen/c6cfbcfe6dc75d276a581bbd7cb06f97 to your computer and use it in GitHub Desktop.
Save Aschen/c6cfbcfe6dc75d276a581bbd7cb06f97 to your computer and use it in GitHub Desktop.
Proxy Benchmark

Proxy Benchmark

Install npm i benchmark

Run:

$ node proxy.js  
object x 220,987,620 ops/sec ±0.55% (97 runs sampled)
proxy x 2,553,992 ops/sec ±0.47% (92 runs sampled)
proxy Reflect x 1,928,349 ops/sec ±0.91% (94 runs sampled)
proxy with arguments x 1,256,931 ops/sec ±0.35% (98 runs sampled)
Fastest is object
const { Benchmark } = require("benchmark");
var suite = new Benchmark.Suite();
const object = {
name: 'Aschen',
age: 29,
};
const proxy = new Proxy({
name: 'Dana',
age: 31
}, {
get(target, prop, receiver) {
return target[prop];
},
set(target, prop, receiver) {
target[prop] = receiver
}
});
const proxyReflect = new Proxy({
name: 'Dana',
age: 31
}, {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver);
},
set(target, prop, receiver) {
Reflect.set(target, prop, receiver);
}
});
const proxyArguments = new Proxy({
name: 'Dana',
age: 31
}, {
get(target, prop, receiver) {
return Reflect.get(...arguments);
},
set(target, prop, receiver) {
Reflect.set(...arguments);
}
});
let count;
// add tests
suite
.add("object", function () {
object.name = 'Aschen M';
count = object.age + object.age;
})
.add("proxy", function () {
proxy.name = 'Aschen M';
count = proxy.age + proxy.age;
})
.add("proxy Reflect", function () {
proxyReflect.name = 'Aschen M';
count = proxyReflect.age + proxyReflect.age;
})
.add("proxy with arguments", function () {
proxyArguments.name = 'Aschen M';
count = proxyArguments.age + proxyArguments.age;
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
// run async
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment