Skip to content

Commit

Permalink
feat: benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
shuta13 committed Apr 18, 2024
1 parent 177c8dc commit 0602c9d
Show file tree
Hide file tree
Showing 10 changed files with 5,698 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ dist
# End of https://www.toptal.com/developers/gitignore/api/node,go

**/__mocks__/src/

benchmarks/src/**
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,23 @@ The syntax of text/template can also be used for headings.
- Node.js: v20.11.1
- pnpm: 8.1.1

We measured the time it took to generate a simple React component with each package.
We measured the time it took to generate a simple React component with each package. The benchmark project is available [here](./benchmarks)

```tsx
import React from "react";

export const Page = (children: { children: React.ReactNode }) => <div>{children}</div>;
export const {{ name }}: React.FC = (children: { children: React.ReactNode }) => <div>{children}</div>;
```

### Result

| Package | Version | Time (ms) |
| --------------- | ------- | --------- |
| plop(turbo gen) | | |
| scaffdog | | |
| hygen | | |
| **moldable** | | |
| Package | Version | Time (ms) |
| ------------ | ------- | --------- |
| plop | 6.2.11 | 0.73 |
| scaffdog | 3.0.0 | 0.16 |
| **moldable** | latest | 0.05 |

It is the fastest. This is because it is written in Go and executed by node after compiling it into a binary.
Moldable is the fastest. This is because it is written in Go and executed by node after compiling it into a binary.

## Third Party License

Expand Down
20 changes: 20 additions & 0 deletions benchmarks/.moldable/benchmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: "benchmark"
description: "This is a benchmark generator"
prompts:
- type: "base"
name: "name"
message: "Please enter any text."
actions:
- type: "add"
path: "src/moldable/{{.name}}.tsx"
template: "benchmark.tsx"
---

# benchmark.tsx

```tsx
import React from "react";

export const {{.name}}: React.FC = (children: { children: React.ReactNode }) => <div>{children}</div>;`,
```
17 changes: 17 additions & 0 deletions benchmarks/.scaffdog/benchmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: "benchmark"
root: "."
output: "src/scaffdog"
ignore: []
questions:
name: "Please enter any text."
---

# `{{ inputs.name }}.tsx`

```tsx
import React from "react";

export const {{ inputs.name }}: React.FC = (children: { children: React.ReactNode }) => <div>{children}</div>;
```

3 changes: 3 additions & 0 deletions benchmarks/.scaffdog/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
files: ['*'],
};
75 changes: 75 additions & 0 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";

const { spawn } = require("child_process");
const { rimraf } = require("rimraf");
const { mkdirp } = require("mkdirp");

const FILE_NAME = "for-benchmark";

const LF = String.fromCharCode(0x0a); // \n
const DOWN = String.fromCharCode(0x1b, 0x5b, 0x42); // ↓
const ENTER = String.fromCharCode(0x0d); // enter

async function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function readStream(stream) {
let result = "";
for await (const line of stream) {
result += line;
}
return result;
}

async function clear() {
await rimraf("./src");
await mkdirp("./src");
}

(async () => {
const type = process.argv[2];

if (type === "plop") {
const plop = spawn("./node_modules/.bin/plop");
await wait(1000);
plop.stdin.write(FILE_NAME);
await wait(1000);
const plopMeasureStart = performance.now();
plop.stdin.write(LF);
const plopMeasureEnd = performance.now();
console.log(`plop: ${plopMeasureEnd - plopMeasureStart}ms`);
}

if (type === "scaffdog") {
const scaffdog = spawn("./node_modules/.bin/scaffdog", ["generate"]);
await wait(1000);
scaffdog.stdin.write(LF);
await wait(1000);
scaffdog.stdin.write(DOWN);
await wait(1000);
scaffdog.stdin.write(ENTER);
await wait(1000);
scaffdog.stdin.write(FILE_NAME);
await wait(1000);
const scaffdogMeasureStart = performance.now();
scaffdog.stdin.write(LF);
const scaffdogMeasureEnd = performance.now();
console.log(`scaffdog: ${scaffdogMeasureEnd - scaffdogMeasureStart}ms`);
}

if (type === "moldable") {
const moldable = spawn("./node_modules/.bin/moldable");
await wait(1000);
moldable.stdin.write(LF);
await wait(1000);
moldable.stdin.write(FILE_NAME);
await wait(1000);
const moldableMeasureStart = performance.now();
moldable.stdin.write(LF);
const moldableMeasureEnd = performance.now();
console.log(`moldable: ${moldableMeasureEnd - moldableMeasureStart}ms`);
}

await clear();
})();
Loading

0 comments on commit 0602c9d

Please sign in to comment.