Skip to content

Instantly share code, notes, and snippets.

@warpech
Last active August 21, 2019 14:47
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 warpech/974afc7ca9d3c7f6c79311a2221cf0ad to your computer and use it in GitHub Desktop.
Save warpech/974afc7ca9d3c7f6c79311a2221cf0ad to your computer and use it in GitHub Desktop.
Reflecting changes to a setter #jsbench #jsperf (http://jsbench.github.io/#974afc7ca9d3c7f6c79311a2221cf0ad) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Reflecting changes to a setter #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
class Foo{
constructor() {
this._value = "Albert";
}
set name(newVal){
this._value = newVal;
}
get name(){
return this._value;
}
};
class Baz extends Foo{};
// https://gist.github.com/WebReflection/3373484
// (C) WebReflection - MIT Style License
const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
function getPropertyDescriptor(o, name) {
let proto = o, descriptor;
while (proto && !(
descriptor = getOwnPropertyDescriptor(proto, name))
) proto = proto.__proto__;
return descriptor;
};
function assertEqual(a, b) {
if (a !== b) {
console.error(`These values are expected to be equal: '${a}' !== '${b}'`);
throw new Error("Expectation failed");
}
}
};
suite.add("old way (JSONPatcherProxy master branch)", function () {
// old way (JSONPatcherProxy master branch)
const obj = {"name": "Albert"};
Reflect.set(obj, "name", "Nikola");
assertEqual(obj.name, "Nikola");
});
suite.add("_updateValueByAssignment", function () {
// _updateValueByAssignment
const obj = {"name": "Albert"};
obj["name"] = "Nikola";
assertEqual(obj.name, "Nikola");
});
suite.add("_updateValueByReflection", function () {
// _updateValueByReflection
const obj = {"name": "Albert"};
Reflect.set(obj, "name", "Nikola", obj);
assertEqual(obj.name, "Nikola");
});
suite.add("_updateValueByAuto", function () {
// _updateValueByAuto
const obj = {"name": "Albert"};
const descriptor = getPropertyDescriptor(obj, "name");
if (descriptor && descriptor.set) {
Reflect.set(obj, "name", "Nikola", obj);
}
else {
obj["name"] = "Nikola";
}
assertEqual(obj.name, "Nikola");
});
suite.add("_updateValueByReflection; object with setter", function () {
// _updateValueByReflection; object with setter
const obj = new Baz();
Reflect.set(obj, "name", "Nikola", obj);
assertEqual(obj.name, "Nikola");
});
suite.add("_updateValueByAuto; object with setter", function () {
// _updateValueByAuto; object with setter
const obj = new Baz();
const descriptor = getPropertyDescriptor(obj, "name");
if (descriptor && descriptor.set) {
Reflect.set(obj, "name", "Nikola", obj);
}
else {
obj["name"] = "Nikola";
}
assertEqual(obj.name, "Nikola");
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Reflecting changes to a setter #jsbench #jsperf");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment