From 2be5d611ceafb27740dab776c4200c89df4ed686 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Wed, 25 Sep 2024 19:25:18 +0200 Subject: [PATCH] benchmark: create benchmark for typescript PR-URL: https://github.com/nodejs/node/pull/54904 Reviewed-By: Matteo Collina --- benchmark/fixtures/strip-types-benchmark.js | 21 ++++++++++++ benchmark/fixtures/strip-types-benchmark.ts | 34 +++++++++++++++++++ .../fixtures/transform-types-benchmark.js | 28 +++++++++++++++ .../fixtures/transform-types-benchmark.ts | 30 ++++++++++++++++ benchmark/ts/strip-typescript.js | 27 +++++++++++++++ benchmark/ts/transform-typescript.js | 26 ++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 benchmark/fixtures/strip-types-benchmark.js create mode 100644 benchmark/fixtures/strip-types-benchmark.ts create mode 100644 benchmark/fixtures/transform-types-benchmark.js create mode 100644 benchmark/fixtures/transform-types-benchmark.ts create mode 100644 benchmark/ts/strip-typescript.js create mode 100644 benchmark/ts/transform-typescript.js diff --git a/benchmark/fixtures/strip-types-benchmark.js b/benchmark/fixtures/strip-types-benchmark.js new file mode 100644 index 00000000000000..3244114dea3cf7 --- /dev/null +++ b/benchmark/fixtures/strip-types-benchmark.js @@ -0,0 +1,21 @@ +function processData(input) { + return { + ...input, + b: input.b + 1 + }; +} + +const data = { + a: "test", + b: 42, + c: true, + d: { + e: ["hello", "world"], + f: { + g: 100, + h: ["str", 123, false] + } + } +}; + +export const result = processData(data); diff --git a/benchmark/fixtures/strip-types-benchmark.ts b/benchmark/fixtures/strip-types-benchmark.ts new file mode 100644 index 00000000000000..0f0185c2d7dc52 --- /dev/null +++ b/benchmark/fixtures/strip-types-benchmark.ts @@ -0,0 +1,34 @@ +type ComplexType = { + a: string; + b: number; + c: boolean; + d: { + e: string[]; + f: { + g: number; + h: [string, number, boolean]; + }; + }; +}; + +function processData(input: ComplexType): ComplexType { + return { + ...input, + b: input.b + 1 + }; +} + +const data: ComplexType = { + a: "test", + b: 42, + c: true, + d: { + e: ["hello", "world"], + f: { + g: 100, + h: ["str", 123, false] + } + } +}; + +export const result = processData(data); diff --git a/benchmark/fixtures/transform-types-benchmark.js b/benchmark/fixtures/transform-types-benchmark.js new file mode 100644 index 00000000000000..c0946942179fe3 --- /dev/null +++ b/benchmark/fixtures/transform-types-benchmark.js @@ -0,0 +1,28 @@ +var Color; +(function (Color) { + Color[Color["Red"] = 0] = "Red"; + Color[Color["Green"] = 1] = "Green"; + Color[Color["Blue"] = 2] = "Blue"; +})(Color || (Color = {})); +var Geometry; +(function (Geometry) { + class Circle { + constructor(center, radius) { + this.center = center; + this.radius = radius; + } + area() { + return Math.PI * Math.pow(this.radius, 2); + } + } + Geometry.Circle = Circle; +})(Geometry || (Geometry = {})); +function processShape(color, shape) { + const colorName = Color[color]; + const area = shape.area().toFixed(2); + return `A ${colorName} circle with area ${area}`; +} + +const point = { x: 0, y: 0 }; +const circle = new Geometry.Circle(point, 5); +export const result = processShape(Color.Blue, circle); diff --git a/benchmark/fixtures/transform-types-benchmark.ts b/benchmark/fixtures/transform-types-benchmark.ts new file mode 100644 index 00000000000000..69c13af4ee4d7c --- /dev/null +++ b/benchmark/fixtures/transform-types-benchmark.ts @@ -0,0 +1,30 @@ +enum Color { + Red, + Green, + Blue +} + +namespace Geometry { + export interface Point { + x: number; + y: number; + } + + export class Circle { + constructor(public center: Point, public radius: number) { } + + area(): number { + return Math.PI * this.radius ** 2; + } + } +} + +function processShape(color: Color, shape: Geometry.Circle): string { + const colorName = Color[color]; + const area = shape.area().toFixed(2); + return `A ${colorName} circle with area ${area}`; +} + +const point: Geometry.Point = { x: 0, y: 0 }; +const circle = new Geometry.Circle(point, 5); +export const result = processShape(Color.Blue, circle); diff --git a/benchmark/ts/strip-typescript.js b/benchmark/ts/strip-typescript.js new file mode 100644 index 00000000000000..7a7155c568b613 --- /dev/null +++ b/benchmark/ts/strip-typescript.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common'); +const path = require('path'); +const assert = require('node:assert'); + + +const js = path.resolve(__dirname, '../fixtures/strip-types-benchmark.js'); +const ts = path.resolve(__dirname, '../fixtures/strip-types-benchmark.ts'); + +const bench = common.createBenchmark(main, { + filepath: [ts, js], + n: [1e4], +}, { + flags: ['--experimental-strip-types', '--disable-warning=ExperimentalWarning'], +}); + +async function main({ n, filepath }) { + let output; + bench.start(); + for (let i = 0; i < n; i++) { + const { result } = await import(`${filepath}?${i}`); + output = result; + } + bench.end(n); + assert.ok(output); +} diff --git a/benchmark/ts/transform-typescript.js b/benchmark/ts/transform-typescript.js new file mode 100644 index 00000000000000..59a78eccae3ff1 --- /dev/null +++ b/benchmark/ts/transform-typescript.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../common'); +const path = require('path'); +const assert = require('node:assert'); + +const js = path.resolve(__dirname, '../fixtures/transform-types-benchmark.js'); +const ts = path.resolve(__dirname, '../fixtures/transform-types-benchmark.ts'); + +const bench = common.createBenchmark(main, { + filepath: [js, ts], + n: [1e4], +}, { + flags: ['--experimental-transform-types', '--disable-warning=ExperimentalWarning'], +}); + +async function main({ n, filepath }) { + let output; + bench.start(); + for (let i = 0; i < n; i++) { + const { result } = await import(`${filepath}?${i}`); + output = result; + } + bench.end(n); + assert.ok(output); +}