-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve overall typescript performance (#435)
Aside from general performance improvements, I added a `profile` option to be able to measure timings and compare them with other tools, like tsc. BREAKING CHANGE: 🧨 Changed default TypeScript compiler options to incremental: true and the default mode to write-tsbuildinfo
- Loading branch information
1 parent
a430979
commit 944e0c9
Showing
13 changed files
with
297 additions
and
34 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
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
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,85 @@ | ||
import { performance } from 'perf_hooks'; | ||
|
||
interface Performance { | ||
enable(): void; | ||
disable(): void; | ||
mark(name: string): void; | ||
markStart(name: string): void; | ||
markEnd(name: string): void; | ||
measure(name: string, startMark?: string, endMark?: string): void; | ||
print(): void; | ||
} | ||
|
||
function createPerformance(): Performance { | ||
let enabled = false; | ||
let timeOrigin: number; | ||
let marks: Map<string, number>; | ||
let measurements: Map<string, number>; | ||
|
||
function enable() { | ||
enabled = true; | ||
marks = new Map(); | ||
measurements = new Map(); | ||
timeOrigin = performance.now(); | ||
} | ||
|
||
function disable() { | ||
enabled = false; | ||
} | ||
|
||
function mark(name: string) { | ||
if (enabled) { | ||
marks.set(name, performance.now()); | ||
} | ||
} | ||
|
||
function measure(name: string, startMark?: string, endMark?: string) { | ||
if (enabled) { | ||
const start = (startMark && marks.get(startMark)) || timeOrigin; | ||
const end = (endMark && marks.get(endMark)) || performance.now(); | ||
|
||
measurements.set(name, (measurements.get(name) || 0) + (end - start)); | ||
} | ||
} | ||
|
||
function markStart(name: string) { | ||
if (enabled) { | ||
mark(`${name} start`); | ||
} | ||
} | ||
|
||
function markEnd(name: string) { | ||
if (enabled) { | ||
mark(`${name} end`); | ||
measure(name, `${name} start`, `${name} end`); | ||
} | ||
} | ||
|
||
function formatName(name: string, width = 0) { | ||
return `${name}:`.padEnd(width); | ||
} | ||
|
||
function formatDuration(duration: number, width = 0) { | ||
return `${(duration / 1000).toFixed(2)} s`.padStart(width); | ||
} | ||
|
||
function print() { | ||
if (enabled) { | ||
let nameWidth = 0; | ||
let durationWidth = 0; | ||
|
||
measurements.forEach((duration, name) => { | ||
nameWidth = Math.max(nameWidth, formatName(name).length); | ||
durationWidth = Math.max(durationWidth, formatDuration(duration).length); | ||
}); | ||
|
||
measurements.forEach((duration, name) => { | ||
console.log(`${formatName(name, nameWidth)} ${formatDuration(duration, durationWidth)}`); | ||
}); | ||
} | ||
} | ||
|
||
return { enable, disable, mark, markStart, markEnd, measure, print }; | ||
} | ||
|
||
export { Performance, createPerformance }; |
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
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
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,48 @@ | ||
import * as ts from 'typescript'; | ||
import { Performance } from '../../profile/Performance'; | ||
|
||
interface TypeScriptPerformance { | ||
enable(): void; | ||
disable(): void; | ||
mark(name: string): void; | ||
measure(name: string, startMark?: string, endMark?: string): void; | ||
} | ||
|
||
function getTypeScriptPerformance(): TypeScriptPerformance | undefined { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (ts as any).performance; | ||
} | ||
|
||
function connectTypeScriptPerformance(performance: Performance): Performance { | ||
const typeScriptPerformance = getTypeScriptPerformance(); | ||
|
||
if (typeScriptPerformance) { | ||
const { mark, measure } = typeScriptPerformance; | ||
const { enable, disable } = performance; | ||
|
||
typeScriptPerformance.mark = (name) => { | ||
mark(name); | ||
performance.mark(name); | ||
}; | ||
typeScriptPerformance.measure = (name, startMark, endMark) => { | ||
measure(name, startMark, endMark); | ||
performance.measure(name, startMark, endMark); | ||
}; | ||
|
||
return { | ||
...performance, | ||
enable() { | ||
enable(); | ||
typeScriptPerformance.enable(); | ||
}, | ||
disable() { | ||
disable(); | ||
typeScriptPerformance.disable(); | ||
}, | ||
}; | ||
} else { | ||
return performance; | ||
} | ||
} | ||
|
||
export { TypeScriptPerformance, connectTypeScriptPerformance }; |
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
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
Oops, something went wrong.