-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: create benchmark for typescript
- Loading branch information
1 parent
6cc4d5f
commit f7bda51
Showing
6 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |