Skip to content

Commit

Permalink
Switch to tinybench and add a property access benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns committed Mar 7, 2024
1 parent d23fa86 commit a0f6ff6
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 145 deletions.
49 changes: 23 additions & 26 deletions bench/all.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Suite } from "benchmark";
import { withCodSpeed } from "@codspeed/benchmark.js-plugin";
import { Bench } from "tinybench";
import { withCodSpeed } from "@codspeed/tinybench-plugin";
import findRoot from "find-root";
import fs from "fs";
import { LargeRoot } from "../spec/fixtures/LargeRoot";
Expand All @@ -10,28 +10,25 @@ import { BigTestModelSnapshot, TestModelSnapshot } from "../spec/fixtures/TestMo
const root = findRoot(__dirname);
const largeRoot = JSON.parse(fs.readFileSync(root + "/spec/fixtures/large-root-snapshot.json", "utf8"));
const fruitAisle = JSON.parse(fs.readFileSync(root + "/spec/fixtures/fruit-aisle-snapshot.json", "utf8"));
const suite = withCodSpeed(new Suite("instantiating model classes"));

void suite
.add("instantiating a small root", function () {
TestClassModel.createReadOnly(TestModelSnapshot);
})
.add("instantiating a large root", function () {
LargeRoot.createReadOnly(largeRoot);
})
.add("instantiating a large union", function () {
FruitAisle.createReadOnly(fruitAisle);
})
.add("instantiating a diverse root", function () {
TestClassModel.createReadOnly(BigTestModelSnapshot);
})
.on("start", function () {
console.profile();
})
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.on("complete", function (this: Suite) {
console.profileEnd();
})
.run({ async: true });
void (async () => {
const suite = withCodSpeed(new Bench());
suite
.add("instantiating a small root", function () {
TestClassModel.createReadOnly(TestModelSnapshot);
})
.add("instantiating a large root", function () {
LargeRoot.createReadOnly(largeRoot);
})
.add("instantiating a large union", function () {
FruitAisle.createReadOnly(fruitAisle);
})
.add("instantiating a diverse root", function () {
TestClassModel.createReadOnly(BigTestModelSnapshot);
});

await suite.warmup();
await suite.run();

console.table(suite.table());
})();
25 changes: 10 additions & 15 deletions bench/create-large-root.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { Suite } from "benchmark";
import { Bench } from "tinybench";
import findRoot from "find-root";
import fs from "fs";
import { LargeRoot } from "../spec/fixtures/LargeRoot";

const snapshot = JSON.parse(fs.readFileSync(findRoot(__dirname) + "/spec/fixtures/large-root-snapshot.json", "utf8"));
const suite = new Suite("instantiating a very large root");
void (async () => {
const suite = new Bench();

suite
.add("instantiating a large root", function () {
suite.add("instantiating a large root", function () {
LargeRoot.createReadOnly(snapshot);
})
.on("start", function () {
console.profile();
})
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.on("complete", function (this: Suite) {
console.profileEnd();
})
.run({ async: true });
});

await suite.warmup();
await suite.run();
console.table(suite.table());
})();
40 changes: 18 additions & 22 deletions bench/create-many-different-roots.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Suite } from "benchmark";
import { Bench } from "tinybench";
import findRoot from "find-root";
import fs from "fs";
import { LargeRoot } from "../spec/fixtures/LargeRoot";
Expand All @@ -7,25 +7,21 @@ import { BigTestModelSnapshot } from "../spec/fixtures/TestModel";
import { NameExample } from "../spec/fixtures/NameExample";

const snapshot = JSON.parse(fs.readFileSync(findRoot(__dirname) + "/spec/fixtures/large-root-snapshot.json", "utf8"));
const suite = new Suite("instantiating a very large root");

suite
.add("instantiating a large root", function () {
LargeRoot.createReadOnly(snapshot);
})
.add("instantiating a small root", function () {
NameExample.createReadOnly({ key: "test", name: "test" });
})
.add("instantiating a diverse root", function () {
TestClassModel.createReadOnly(BigTestModelSnapshot);
})
.on("start", function () {
console.profile();
})
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.on("complete", function (this: Suite) {
console.profileEnd();
})
.run({ async: true });
void (async () => {
const suite = new Bench();
suite
.add("instantiating a large root", function () {
LargeRoot.createReadOnly(snapshot);
})
.add("instantiating a small root", function () {
NameExample.createReadOnly({ key: "test", name: "test" });
})
.add("instantiating a diverse root", function () {
TestClassModel.createReadOnly(BigTestModelSnapshot);
});

await suite.warmup();
await suite.run();
console.table(suite.table());
})();
26 changes: 11 additions & 15 deletions bench/create-union.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { Suite } from "benchmark";
import { Bench } from "tinybench";
import findRoot from "find-root";
import fs from "fs";
import { FruitAisle } from "../spec/fixtures/FruitAisle";

const root = findRoot(__dirname);
const fruitBasket = JSON.parse(fs.readFileSync(root + "/spec/fixtures/fruit-aisle-snapshot.json", "utf8"));
const suite = new Suite("instantiating model classes");

suite
.add("instantiating a large union", function () {
void (async () => {
const suite = new Bench();

suite.add("instantiating a large union", function () {
FruitAisle.createReadOnly(fruitBasket);
})
.on("start", function () {
console.profile();
})
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.on("complete", function (this: Suite) {
console.profileEnd();
})
.run({ async: true });
});

await suite.warmup();
await suite.run();
console.table(suite.table());
})();
52 changes: 25 additions & 27 deletions bench/cross-framework.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Suite } from "benchmark";
import { Bench } from "tinybench";
import { TestClassModel } from "../spec/fixtures/TestClassModel";
import { TestModel } from "../spec/fixtures/TestModel";
import { TestPlainModel } from "./reference/plain-class";
import { ObservablePlainModel } from "./reference/mobx";

const suite = new Suite("instantiating same object with different paradigms");

const TestModelSnapshot: (typeof TestModel)["InputType"] = {
bool: true,
frozen: { test: "string" },
Expand All @@ -23,27 +21,27 @@ const TestModelSnapshot: (typeof TestModel)["InputType"] = {
},
};

suite
.add("mobx-state-tree", function () {
TestModel.create(TestModelSnapshot);
})
.add("mobx-quick-tree types.model", function () {
TestModel.createReadOnly(TestModelSnapshot);
})
.add("mobx-quick-tree ClassModel", function () {
TestClassModel.createReadOnly(TestModelSnapshot);
})
.add("plain mobx", function () {
new ObservablePlainModel(TestModelSnapshot);
})
.add("plain es6", function () {
new TestPlainModel(TestModelSnapshot);
})
.on("cycle", function (event: any) {
console.log(String(event.target));
})
.on("complete", function (this: Suite) {
console.log("Fastest is " + this.filter("fastest").map("name"));
console.log("Slowest is " + this.filter("slowest").map("name"));
})
.run({ async: true });
void (async () => {
const suite = new Bench();

suite
.add("mobx-state-tree", function () {
TestModel.create(TestModelSnapshot);
})
.add("mobx-quick-tree types.model", function () {
TestModel.createReadOnly(TestModelSnapshot);
})
.add("mobx-quick-tree ClassModel", function () {
TestClassModel.createReadOnly(TestModelSnapshot);
})
.add("plain mobx", function () {
new ObservablePlainModel(TestModelSnapshot);
})
.add("plain es6", function () {
new TestPlainModel(TestModelSnapshot);
});

await suite.warmup();
await suite.run();
console.table(suite.table());
})();
56 changes: 56 additions & 0 deletions bench/property-access-model-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Bench } from "tinybench";
import findRoot from "find-root";
import fs from "fs";
import { FruitAisle } from "../spec/fixtures/FruitAisle";

const root = findRoot(__dirname);
const fruitBasket = JSON.parse(fs.readFileSync(root + "/spec/fixtures/fruit-aisle-snapshot.json", "utf8"));

void (async () => {
const suite = new Bench({ throws: true });

const shared = FruitAisle.createReadOnly(fruitBasket);

const touchProperties = (instance: FruitAisle) => {
instance.binCount;
instance.null;
instance.firstBinCount;
instance.firstBinType;
};

let instance: FruitAisle;

suite
.add(
"accessing unmemoized null property of a class model",
function () {
instance.null;
},
{
beforeEach() {
instance = FruitAisle.createReadOnly(fruitBasket);
},
}
)
.add("accessing memoized null property of a class model", function () {
shared.null;
})
.add(
"accessing unmemoized getter properties of a class model",
function () {
touchProperties(instance);
},
{
beforeEach() {
instance = FruitAisle.createReadOnly(fruitBasket);
},
}
)
.add("accessing memoized getter properties of a class model", function () {
touchProperties(shared);
});

await suite.warmup();
await suite.run();
console.table(suite.table());
})();
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "^2.2.0",
"@codspeed/tinybench-plugin": "^3.1.0",
"@faker-js/faker": "^7.6.0",
"@gadgetinc/eslint-config": "^0.6.1",
"@gadgetinc/prettier-config": "^0.4.0",
"@swc/core": "^1.3.96",
"@swc/jest": "^0.2.29",
"@types/benchmark": "^2.1.5",
"@types/find-root": "^1.1.4",
"@types/jest": "^29.5.8",
"@types/lodash.memoize": "^4.1.9",
"@types/node": "^18.18.9",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"benchmark": "^2.1.4",
"conditional-type-checks": "^1.0.6",
"deep-freeze-es6": "^1.4.1",
"deoptigate": "^0.7.1",
Expand All @@ -57,6 +55,7 @@
"lodash": "^4.17.21",
"microtime": "^3.1.1",
"prettier": "^2.8.8",
"tinybench": "^2.6.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
Expand Down
Loading

0 comments on commit a0f6ff6

Please sign in to comment.