Skip to content

Instantly share code, notes, and snippets.

@webNeat
Forked from Aschen/reflect.js
Last active January 31, 2022 18:06
Show Gist options
  • Save webNeat/2a19bd13394de3575b2ff1ffb86707ea to your computer and use it in GitHub Desktop.
Save webNeat/2a19bd13394de3575b2ff1ffb86707ea to your computer and use it in GitHub Desktop.
Reflect.defineProperty vs #field
/**
$ node node/reflect-vs-private.js
Reflect x 7,297,368 ops/sec ±0.66% (92 runs sampled)
Normal x 126,613,750 ops/sec ±0.75% (93 runs sampled)
Private x 14,083,334 ops/sec ±0.99% (87 runs sampled)
Fastest is Normal
*/
const { Benchmark } = require("benchmark");
var suite = new Benchmark.Suite();
class ReflectMe {
constructor() {
Reflect.defineProperty(this, "_name", {
writable: true,
});
}
}
class Normal {
constructor() {
this.name = "aschen";
}
}
class Private {
#name;
constructor() {
this.#name = "aschen";
}
}
let ret;
// add tests
suite
.add("Reflect", function () {
ret = new ReflectMe();
})
.add("Normal", function () {
ret = new Normal();
})
.add("Private", function () {
ret = new Private();
})
// 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