Skip to content

Use regular imports instead of require where possible - #59017

Merged
Jake Bailey (jakebailey) merged 3 commits into
microsoft:mainfrom
jakebailey:use-imports
Jun 25, 2024
Merged

Use regular imports instead of require where possible#59017
Jake Bailey (jakebailey) merged 3 commits into
microsoft:mainfrom
jakebailey:use-imports

Conversation

@jakebailey

Copy link
Copy Markdown
Member

This is pulled out of #58419, since I think this is generally better and we can do it now.

Comment threadsrc/tsserver/nodeServer.ts Outdated
@jakebailey

Copy link
Copy Markdown
MemberAuthor

TypeScript Bot (@typescript-bot) perf test this

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 25, 2024

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

CommandStatusResults
perf test this✅ Started👀 Results

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Compiler-Unions - node (v18.15.0, x64)
Errors3030~~~p=1.000 n=6
Symbols62,15362,153~~~p=1.000 n=6
Types50,24250,242~~~p=1.000 n=6
Memory used192,704k (± 0.60%)193,959k (± 0.99%)~192,161k195,778kp=0.128 n=6
Parse Time1.58s (± 0.26%)1.58s (± 0.48%)~1.57s1.59sp=1.000 n=6
Bind Time0.86s (± 1.14%)0.86s (± 0.73%)~0.85s0.87sp=0.733 n=6
Check Time11.21s (± 0.23%)11.19s (± 0.43%)~11.12s11.25sp=0.422 n=6
Emit Time3.27s (± 0.32%)3.27s (± 0.46%)~3.26s3.30sp=0.282 n=6
Total Time16.91s (± 0.13%)16.90s (± 0.28%)~16.83s16.96sp=0.810 n=6
angular-1 - node (v18.15.0, x64)
Errors55~~~p=1.000 n=6
Symbols944,114944,114~~~p=1.000 n=6
Types407,050407,050~~~p=1.000 n=6
Memory used1,218,311k (± 0.00%)1,218,293k (± 0.00%)~1,218,209k1,218,378kp=0.575 n=6
Parse Time7.96s (± 0.93%)7.94s (± 0.31%)~7.91s7.98sp=0.573 n=6
Bind Time2.23s (± 0.23%)2.24s (± 0.24%)+0.01s (+ 0.37%)2.23s2.24sp=0.038 n=6
Check Time35.74s (± 0.29%)35.72s (± 0.34%)~35.50s35.86sp=0.809 n=6
Emit Time16.12s (± 0.42%)16.16s (± 0.32%)~16.12s16.26sp=0.687 n=6
Total Time62.06s (± 0.29%)62.05s (± 0.24%)~61.80s62.22sp=1.000 n=6
mui-docs - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols2,135,0962,135,096~~~p=1.000 n=6
Types927,168927,168~~~p=1.000 n=6
Memory used2,117,354k (± 0.01%)2,117,357k (± 0.01%)~2,117,234k2,117,506kp=0.810 n=6
Parse Time7.91s (± 0.27%)7.92s (± 0.37%)~7.88s7.96sp=0.468 n=6
Bind Time2.76s (± 0.61%)2.77s (± 0.62%)~2.74s2.79sp=0.465 n=6
Check Time83.53s (± 0.32%)83.85s (± 0.19%)~83.69s84.07sp=0.093 n=6
Emit Time0.16s (± 4.65%)0.16s (± 4.65%)~0.15s0.17sp=1.000 n=6
Total Time94.36s (± 0.28%)94.70s (± 0.16%)+0.34s (+ 0.36%)94.51s94.91sp=0.031 n=6
self-build-src - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,230,8691,231,459+590 (+ 0.05%)~~p=0.001 n=6
Types261,077261,178+101 (+ 0.04%)~~p=0.001 n=6
Memory used2,356,017k (± 1.01%)2,347,553k (± 0.03%)~2,346,575k2,348,323kp=0.173 n=6
Parse Time5.98s (± 0.53%)6.00s (± 1.29%)~5.92s6.12sp=0.936 n=6
Bind Time2.26s (± 0.66%)2.27s (± 0.60%)~2.26s2.29sp=0.369 n=6
Check Time39.77s (± 0.54%)40.27s (± 0.24%)+0.50s (+ 1.26%)40.15s40.37sp=0.005 n=6
Emit Time3.21s (± 1.51%)3.24s (± 1.87%)~3.16s3.30sp=0.335 n=6
Total Time51.22s (± 0.42%)51.80s (± 0.30%)+0.58s (+ 1.13%)51.62s51.98sp=0.005 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols1,230,8691,231,459+590 (+ 0.05%)~~p=0.001 n=6
Types261,077261,178+101 (+ 0.04%)~~p=0.001 n=6
Memory used2,421,915k (± 0.02%)2,423,132k (± 0.02%)+1,218k (+ 0.05%)2,422,625k2,423,855kp=0.013 n=6
Parse Time5.16s (± 0.53%)5.22s (± 0.84%)+0.06s (+ 1.26%)5.15s5.28sp=0.030 n=6
Bind Time1.68s (± 0.48%)1.70s (± 1.05%)~1.67s1.72sp=0.085 n=6
Check Time34.16s (± 0.37%)34.51s (± 0.19%)+0.35s (+ 1.03%)34.41s34.60sp=0.005 n=6
Emit Time2.68s (± 2.29%)2.76s (± 1.81%)~2.67s2.81sp=0.066 n=6
Total Time43.72s (± 0.31%)44.21s (± 0.24%)+0.50s (+ 1.13%)44.12s44.41sp=0.005 n=6
self-compiler - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols258,577258,577~~~p=1.000 n=6
Types104,827104,827~~~p=1.000 n=6
Memory used428,182k (± 0.01%)428,194k (± 0.01%)~428,158k428,273kp=0.810 n=6
Parse Time2.75s (± 0.67%)2.74s (± 0.72%)~2.71s2.76sp=0.935 n=6
Bind Time1.11s (± 1.51%)1.11s (± 1.75%)~1.08s1.13sp=1.000 n=6
Check Time15.08s (± 0.29%)15.07s (± 0.37%)~14.97s15.14sp=1.000 n=6
Emit Time1.16s (± 1.33%)1.15s (± 0.95%)~1.13s1.16sp=0.359 n=6
Total Time20.10s (± 0.20%)20.07s (± 0.34%)~19.97s20.17sp=0.574 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors3535~~~p=1.000 n=6
Symbols224,565224,565~~~p=1.000 n=6
Types93,73493,734~~~p=1.000 n=6
Memory used369,470k (± 0.03%)369,426k (± 0.03%)~369,341k369,604kp=0.471 n=6
Parse Time2.31s (± 0.51%)2.31s (± 0.33%)~2.30s2.32sp=0.933 n=6
Bind Time1.34s (± 1.46%)1.33s (± 1.32%)~1.31s1.36sp=0.176 n=6
Check Time13.13s (± 0.22%)13.15s (± 0.28%)~13.11s13.20sp=0.466 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time16.79s (± 0.25%)16.79s (± 0.27%)~16.74s16.86sp=0.873 n=6
vscode - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols2,878,7052,878,705~~~p=1.000 n=6
Types975,213975,213~~~p=1.000 n=6
Memory used3,041,995k (± 0.00%)3,041,998k (± 0.00%)~3,041,926k3,042,059kp=1.000 n=6
Parse Time13.51s (± 0.41%)13.48s (± 0.30%)~13.41s13.52sp=0.684 n=6
Bind Time4.15s (± 0.34%)4.19s (± 1.99%)~4.15s4.36sp=0.245 n=6
Check Time72.82s (± 0.23%)72.98s (± 0.21%)~72.79s73.20sp=0.092 n=6
Emit Time23.91s (± 1.00%)23.86s (± 0.17%)~23.78s23.90sp=0.575 n=6
Total Time114.38s (± 0.24%)114.51s (± 0.19%)~114.22s114.75sp=0.471 n=6
webpack - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols267,117267,117~~~p=1.000 n=6
Types108,775108,775~~~p=1.000 n=6
Memory used411,568k (± 0.03%)411,537k (± 0.01%)~411,464k411,602kp=0.936 n=6
Parse Time3.80s (± 0.56%)3.81s (± 0.61%)~3.78s3.84sp=0.464 n=6
Bind Time1.69s (± 0.61%)1.68s (± 0.45%)~1.67s1.69sp=0.351 n=6
Check Time16.70s (± 0.48%)16.67s (± 0.23%)~16.62s16.72sp=0.686 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time22.19s (± 0.35%)22.17s (± 0.17%)~22.10s22.20sp=1.000 n=6
xstate-main - node (v18.15.0, x64)
Errors00~~~p=1.000 n=6
Symbols525,251525,251~~~p=1.000 n=6
Types178,574178,574~~~p=1.000 n=6
Memory used462,894k (± 0.05%)462,994k (± 0.07%)~462,386k463,267kp=0.298 n=6
Parse Time2.65s (± 0.37%)2.64s (± 0.57%)~2.62s2.66sp=0.360 n=6
Bind Time0.98s (± 0.64%)0.98s (± 0.53%)~0.97s0.98sp=0.386 n=6
Check Time15.16s (± 0.43%)15.16s (± 0.28%)~15.08s15.20sp=0.687 n=6
Emit Time0.00s0.00s~~~p=1.000 n=6
Total Time18.79s (± 0.33%)18.78s (± 0.26%)~18.69s18.83sp=0.630 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

tsserver

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Compiler-UnionsTSServer - node (v18.15.0, x64)
Req 1 - updateOpen2,779ms (± 0.60%)2,755ms (± 1.39%)~2,705ms2,797msp=0.575 n=6
Req 2 - geterr6,934ms (± 9.62%)6,280ms (± 8.74%)~6,022ms7,399msp=0.128 n=6
Req 3 - references370ms (±10.38%)358ms (±11.79%)~318ms397msp=0.808 n=6
Req 4 - navto341ms (± 0.16%)340ms~~~p=0.071 n=6
Req 5 - completionInfo count1,3571,357~~~p=1.000 n=6
Req 5 - completionInfo131ms (± 0.39%)132ms (± 1.17%)~131ms135msp=0.523 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen2,412ms (± 0.72%)2,431ms (± 0.40%)~2,420ms2,445msp=0.054 n=6
Req 2 - geterr3,797ms (± 0.15%)3,797ms (± 0.24%)~3,780ms3,807msp=0.688 n=6
Req 3 - references276ms (± 0.85%)278ms (± 0.20%)~277ms278msp=0.228 n=6
Req 4 - navto229ms (± 2.71%)226ms (± 0.43%)~225ms227msp=0.865 n=6
Req 5 - completionInfo count1,5191,519~~~p=1.000 n=6
Req 5 - completionInfo74ms (± 8.05%)74ms (± 0.74%)~73ms74msp=0.154 n=6
xstate-main-1-tsserver - node (v18.15.0, x64)
Req 1 - updateOpen7,702ms (± 0.47%)6,369ms (± 4.96%)🟩-1,333ms (-17.31%)6,215ms7,013msp=0.005 n=6
Req 2 - geterr1,683ms (± 1.00%)1,594ms (±11.50%)~1,348ms1,734msp=0.689 n=6
Req 3 - references111ms (± 5.00%)105ms (± 0.52%)~104ms105msp=0.152 n=6
Req 4 - navto611ms (± 2.34%)596ms (± 1.42%)-15ms (- 2.48%)581ms603msp=0.029 n=6
Req 5 - completionInfo count3,4133,413~~~p=1.000 n=6
Req 5 - completionInfo1,274ms (± 2.17%)1,245ms (± 0.77%)~1,235ms1,259msp=0.128 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • CompilerTSServer - node (v18.15.0, x64)
  • Compiler-UnionsTSServer - node (v18.15.0, x64)
  • xstate-main-1-tsserver - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

startup

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
tsc-startup - node (v18.15.0, x64)
Execution time187.95ms (± 0.17%)187.93ms (± 0.19%)~186.20ms191.43msp=0.214 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time284.80ms (± 0.27%)284.84ms (± 0.26%)~278.07ms287.68msp=0.673 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time279.23ms (± 0.29%)279.09ms (± 0.29%)-0.14ms (- 0.05%)272.66ms286.93msp=0.000 n=600
typescript-startup - node (v18.15.0, x64)
Execution time338.88ms (± 0.30%)338.67ms (± 0.29%)-0.21ms (- 0.06%)330.51ms348.20msp=0.000 n=600
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • tsc-startup - node (v18.15.0, x64)
  • tsserver-startup - node (v18.15.0, x64)
  • tsserverlibrary-startup - node (v18.15.0, x64)
  • typescript-startup - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@jakebailey
Jake Bailey (jakebailey) merged commit ef339af into microsoft:mainJun 25, 2024
@jakebailey
Jake Bailey (jakebailey) deleted the use-imports branch June 25, 2024 21:00
Comment on lines +1 to +5
import childProcess from "child_process";
import fs from "fs";
import net from "net";
import os from "os";
import readline from "readline";

@DanielRosenwasserDaniel Rosenwasser (DanielRosenwasser)Jul 3, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any risk now that these are unconditional imports?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this is the tsserver project which only runs in node.

let cancellationToken: ts.server.ServerCancellationToken;
try {
const factory = require("./cancellationToken");
const factory = require("./cancellationToken.js");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I came across this line running dependency-cruiser on the typescript codebase. After building, it's the only require/import that can't be resolved. I notice that this file, nodeServer, does not have a cancellationToken sibling file - is it possible that this is always throwing (seems like it could be, since the catch block silently catches the error and uses ts.server.nullCancellationToken)

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should exist as a sibling. https://unpkg.com/browse/typescript@5.6.3/lib/cancellationToken.js

Are you running this tool on the built code, or the source?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(realistically, it's not clear to me why this is a separate file at all anyway)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. I'm running it on the source, so I guess it's missing some part of the build process that compiles src/tsserver/nodeServer.ts into lib/tsserver.js, and src/cancellationToken/cancellationToken.ts into lib/cancellationToken.js, is that right? (Something in Hereby?)

Would it not work as require("../cancellationToken/cancellationToken.js") (which I think is what would be correct for the source file structure)

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that would not work. Our input source structure has nothing to do with our output source structure; it's bundled and placed in a different directory.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - thanks very much!

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#60250 you've inspired me

@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Uncommitted BugPR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@jakebailey@typescript-bot@DanielRosenwasser@sheetalkamat@mmkal@sandersn