Skip to content

Commit 00d4f80

Browse files
benchmark: create benchmark for typescript
PR-URL: #54904 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 6fb9f56 commit 00d4f80

6 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function processData(input) {
2+
return {
3+
...input,
4+
b: input.b + 1
5+
};
6+
}
7+
8+
const data = {
9+
a: "test",
10+
b: 42,
11+
c: true,
12+
d: {
13+
e: ["hello", "world"],
14+
f: {
15+
g: 100,
16+
h: ["str", 123, false]
17+
}
18+
}
19+
};
20+
21+
export const result = processData(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
type ComplexType = {
2+
a: string;
3+
b: number;
4+
c: boolean;
5+
d: {
6+
e: string[];
7+
f: {
8+
g: number;
9+
h: [string, number, boolean];
10+
};
11+
};
12+
};
13+
14+
function processData(input: ComplexType): ComplexType {
15+
return {
16+
...input,
17+
b: input.b + 1
18+
};
19+
}
20+
21+
const data: ComplexType = {
22+
a: "test",
23+
b: 42,
24+
c: true,
25+
d: {
26+
e: ["hello", "world"],
27+
f: {
28+
g: 100,
29+
h: ["str", 123, false]
30+
}
31+
}
32+
};
33+
34+
export const result = processData(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var Color;
2+
(function (Color) {
3+
Color[Color["Red"] = 0] = "Red";
4+
Color[Color["Green"] = 1] = "Green";
5+
Color[Color["Blue"] = 2] = "Blue";
6+
})(Color || (Color = {}));
7+
var Geometry;
8+
(function (Geometry) {
9+
class Circle {
10+
constructor(center, radius) {
11+
this.center = center;
12+
this.radius = radius;
13+
}
14+
area() {
15+
return Math.PI * Math.pow(this.radius, 2);
16+
}
17+
}
18+
Geometry.Circle = Circle;
19+
})(Geometry || (Geometry = {}));
20+
function processShape(color, shape) {
21+
const colorName = Color[color];
22+
const area = shape.area().toFixed(2);
23+
return `A ${colorName} circle with area ${area}`;
24+
}
25+
26+
const point = { x: 0, y: 0 };
27+
const circle = new Geometry.Circle(point, 5);
28+
export const result = processShape(Color.Blue, circle);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
enum Color {
2+
Red,
3+
Green,
4+
Blue
5+
}
6+
7+
namespace Geometry {
8+
export interface Point {
9+
x: number;
10+
y: number;
11+
}
12+
13+
export class Circle {
14+
constructor(public center: Point, public radius: number) { }
15+
16+
area(): number {
17+
return Math.PI * this.radius ** 2;
18+
}
19+
}
20+
}
21+
22+
function processShape(color: Color, shape: Geometry.Circle): string {
23+
const colorName = Color[color];
24+
const area = shape.area().toFixed(2);
25+
return `A ${colorName} circle with area ${area}`;
26+
}
27+
28+
const point: Geometry.Point = { x: 0, y: 0 };
29+
const circle = new Geometry.Circle(point, 5);
30+
export const result = processShape(Color.Blue, circle);

benchmark/ts/strip-typescript.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const path = require('path');
5+
const assert = require('node:assert');
6+
7+
8+
const js = path.resolve(__dirname, '../fixtures/strip-types-benchmark.js');
9+
const ts = path.resolve(__dirname, '../fixtures/strip-types-benchmark.ts');
10+
11+
const bench = common.createBenchmark(main, {
12+
filepath: [ts, js],
13+
n: [1e4],
14+
}, {
15+
flags: ['--experimental-strip-types', '--disable-warning=ExperimentalWarning'],
16+
});
17+
18+
async function main({ n, filepath }) {
19+
let output;
20+
bench.start();
21+
for (let i = 0; i < n; i++) {
22+
const { result } = await import(`${filepath}?${i}`);
23+
output = result;
24+
}
25+
bench.end(n);
26+
assert.ok(output);
27+
}

benchmark/ts/transform-typescript.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const path = require('path');
5+
const assert = require('node:assert');
6+
7+
const js = path.resolve(__dirname, '../fixtures/transform-types-benchmark.js');
8+
const ts = path.resolve(__dirname, '../fixtures/transform-types-benchmark.ts');
9+
10+
const bench = common.createBenchmark(main, {
11+
filepath: [js, ts],
12+
n: [1e4],
13+
}, {
14+
flags: ['--experimental-transform-types', '--disable-warning=ExperimentalWarning'],
15+
});
16+
17+
async function main({ n, filepath }) {
18+
let output;
19+
bench.start();
20+
for (let i = 0; i < n; i++) {
21+
const { result } = await import(`${filepath}?${i}`);
22+
output = result;
23+
}
24+
bench.end(n);
25+
assert.ok(output);
26+
}

0 commit comments

Comments
 (0)