-
-
Save webNeat/2a19bd13394de3575b2ff1ffb86707ea to your computer and use it in GitHub Desktop.
Reflect.defineProperty vs #field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
$ 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