Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 43403a0

Browse files
author
Wenjie Xia
committed
chore(compiler): update benchmark script
1 parent 2341f96 commit 43403a0

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

compiler/benchmark.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { compile, CompileOptions } from 'https://deno.land/x/[email protected]/tsc/compile.ts'
22
import { colors, path, Sha1, walk } from '../deps.ts'
3+
import { hashShort } from '../shared/constants.ts'
34
import { initWasm, transpileSync } from './mod.ts'
45

5-
const hashShort = 9
6-
const reHttp = /^https?:\/\//i
7-
86
function tsc(source: string, opts: any) {
97
const compileOptions: CompileOptions = {
108
mode: opts.isDev ? 'development' : 'production',
119
target: 'es2020',
1210
reactRefresh: opts.isDev,
13-
rewriteImportPath: (path: string) => path.replace(reHttp, '/-/'),
11+
rewriteImportPath: (path: string) => path.replace('https://', '/-/'),
1412
signUseDeno: (id: string) => {
15-
const sig = 'useDeno.' + (new Sha1()).update(id).update("0.2.25").update(Date.now().toString()).hex().slice(0, hashShort)
13+
const sig = 'useDeno.' + (new Sha1()).update(id).update('0.2.25').update(Date.now().toString()).hex().slice(0, hashShort)
1614
return sig
1715
}
1816
}
@@ -43,10 +41,10 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
4341

4442
for (const { code, filename } of sourceFiles) {
4543
const t = performance.now()
46-
for (let i = 0; i < 5; i++) {
44+
for (let i = 0; i < 2; i++) {
4745
tsc(code, { filename, isDev })
4846
}
49-
const d = (performance.now() - t) / 5
47+
const d = (performance.now() - t) / 2
5048
if (d1.min === 0 || d < d1.min) {
5149
d1.min = d
5250
}
@@ -57,10 +55,14 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
5755
}
5856
for (const { code, filename } of sourceFiles) {
5957
const t = performance.now()
60-
for (let i = 0; i < 5; i++) {
61-
transpileSync(code, { url: filename, swcOptions: {}, isDev })
58+
for (let i = 0; i < 2; i++) {
59+
transpileSync(code, {
60+
url: filename,
61+
swcOptions: { target: 'es2020' },
62+
isDev
63+
})
6264
}
63-
const d = (performance.now() - t) / 5
65+
const d = (performance.now() - t) / 2
6466
if (d2.min === 0 || d < d2.min) {
6567
d2.min = d
6668
}
@@ -75,23 +77,25 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
7577
console.log(`swc is ${coloredDiff(d1.d / d2.d)} ${d1.d > d2.d ? 'faster' : 'slower'} than tsc`)
7678
}
7779

78-
if (import.meta.main) {
79-
(async () => {
80-
const p = Deno.run({
81-
cmd: ['deno', 'info'],
82-
stdout: 'piped',
83-
stderr: 'null'
84-
})
85-
await initWasm((new TextDecoder).decode(await p.output()).split('"')[1])
86-
p.close()
80+
async function main() {
81+
const p = Deno.run({
82+
cmd: ['deno', 'info'],
83+
stdout: 'piped',
84+
stderr: 'null'
85+
})
86+
await initWasm((new TextDecoder).decode(await p.output()).split('"')[1])
87+
p.close()
8788

88-
const sourceFiles: Array<{ code: string, filename: string }> = []
89-
const walkOptions = { includeDirs: false, exts: ['.tsx'], skip: [/[\._]test\.tsx?$/i] }
90-
for await (const { path: filename } of walk(path.resolve('..'), walkOptions)) {
91-
sourceFiles.push({ code: await Deno.readTextFile(filename), filename })
92-
}
89+
const sourceFiles: Array<{ code: string, filename: string }> = []
90+
const walkOptions = { includeDirs: false, exts: ['ts', '.tsx'], skip: [/[\._](test|d)\.tsx?$/i, /\/compiler\//] }
91+
for await (const { path: filename } of walk(path.resolve('..'), walkOptions)) {
92+
sourceFiles.push({ code: await Deno.readTextFile(filename), filename })
93+
}
9394

94-
await benchmark(sourceFiles, true)
95-
await benchmark(sourceFiles, false)
96-
})()
95+
await benchmark(sourceFiles, true)
96+
await benchmark(sourceFiles, false)
97+
}
98+
99+
if (import.meta.main) {
100+
main()
97101
}

0 commit comments

Comments
 (0)