-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.ts
167 lines (155 loc) · 4.41 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import fs from 'fs'
import os from 'os'
import { join } from 'path'
import * as babel from '@babel/core'
// @ts-expect-error
import * as envPreset from '@babel/preset-env'
// @ts-expect-error
import * as tsPreset from '@babel/preset-typescript'
import { transformSync as transformSyncNapi, transform as transformNapi } from '@swc-node/core'
import Benchmark, { Suite } from 'benchmark'
import { green } from 'colorette'
import { transformSync as transformSyncEsbuild, transform as transformEsbuild } from 'esbuild'
import ts from 'typescript'
const cpuCount = os.cpus().length - 1
const syncSuite = new Suite('Transform rxjs/AjaxObservable.ts benchmark')
const asyncSuite = new Suite('Transform rxjs/AjaxObservable.ts async benchmark')
const parallelSuite = new Suite('Transform rxjs/AjaxObservable.ts parallel benchmark')
const SOURCE_PATH = join(__dirname, '..', 'node_modules', 'rxjs', 'src', 'internal', 'ajax', 'ajax.ts')
const SOURCE_CODE = fs.readFileSync(SOURCE_PATH, 'utf-8')
async function run() {
let defer: () => void
const task = new Promise<void>((resolve) => {
defer = resolve
})
syncSuite
.add('esbuild', () => {
transformSyncEsbuild(SOURCE_CODE, {
sourcefile: SOURCE_PATH,
loader: 'ts',
sourcemap: true,
minify: false,
target: 'es2015',
})
})
.add('@swc-node/core', () => {
transformSyncNapi(SOURCE_CODE, SOURCE_PATH, {
// SWC target es2016 is es2015 in TypeScript
target: 'es2016',
module: 'commonjs',
sourcemap: true,
})
})
.add('typescript', () => {
ts.transpileModule(SOURCE_CODE, {
fileName: SOURCE_PATH,
compilerOptions: {
target: ts.ScriptTarget.ES2015,
module: ts.ModuleKind.CommonJS,
isolatedModules: true,
sourceMap: true,
},
})
})
.add('babel', () => {
babel.transform(SOURCE_CODE, {
filename: SOURCE_PATH,
presets: [
tsPreset,
[envPreset, { useBuiltIns: false, loose: true, targets: 'Chrome > 52', modules: 'commonjs' }],
],
configFile: false,
babelrc: false,
sourceMaps: true,
})
})
.on('cycle', function (event: Benchmark.Event) {
console.info(String(event.target))
})
.on('complete', function (this: Benchmark.Target & Benchmark.Suite) {
console.info(
`${this.name} bench suite: Fastest is ${green(
this.filter('fastest')
.map((s: Benchmark.Target) => s.name)
.join(''),
)}`,
)
defer()
})
.run()
await task
}
async function runAsync(parallel = 1, suite = asyncSuite) {
let defer: () => void
const task = new Promise<void>((resolve) => {
defer = resolve
})
suite
.add({
name: '@swc-node/core',
fn: (deferred: any) => {
Promise.all(
Array.from({ length: parallel }).map(() => {
return transformNapi(SOURCE_CODE, SOURCE_PATH, {
target: 'es2016',
module: 'commonjs',
sourcemap: true,
})
}),
)
.then(() => {
deferred.resolve()
})
.catch((e) => {
console.error(e)
})
},
defer: true,
async: true,
queued: true,
})
.add({
name: 'esbuild',
fn: (deferred: any) => {
Promise.all(
Array.from({ length: parallel }).map(() =>
transformEsbuild(SOURCE_CODE, {
sourcefile: SOURCE_PATH,
loader: 'ts',
sourcemap: true,
minify: false,
target: 'es2015',
}),
),
)
.then(() => {
deferred.resolve()
})
.catch((e) => {
console.error(e)
})
},
defer: true,
async: true,
queued: true,
})
.on('cycle', function (event: Benchmark.Event) {
event.target.hz = event.target!.hz! * parallel
console.info(String(event.target))
})
.on('complete', function (this: Benchmark.Target & Benchmark.Suite) {
console.info(
`${this.name} bench suite: Fastest is ${green(
this.filter('fastest')
.map((t: Benchmark.Target) => t.name)
.join(''),
)}`,
)
defer()
})
.run()
await task
}
run()
.then(() => runAsync(cpuCount, parallelSuite))
.catch(console.error)