Skip to content

Add typescript benchmark#19

Merged
RafaelGSS merged 4 commits intonodesource:mainfrom
arthurfiorette:main
May 18, 2024
Merged

Add typescript benchmark#19
RafaelGSS merged 4 commits intonodesource:mainfrom
arthurfiorette:main

Conversation

@arthurfiorette
Copy link
Copy Markdown
Contributor

@arthurfiorette arthurfiorette commented May 16, 2024

closes #15

This PR adds 3 typescript benchmarks.

  • transpile: Simplt transpiles a big TS file without correct type cheking
  • createSourceFile: Just runs the Typescript AST parser
  • getTypeChecker: Runs the type checker in the project

Output sample:

Running Node.js Package Benchmark...
---------------------------------------------------------------
typescript
  transpile:                                                   79.51 (10 samples)
  createSourceFile:                                            1.914K (192 samples)
  getTypeChecker:                                              4.345 (10 samples)
---------------------------------------------------------------

Copy link
Copy Markdown
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Could you please check if dead-code elimination isn't in place?

You can do it by:

  • ./bench-it node baseline
  • Assign ts.* return to a variable and then assert.ok(var)
  • ./bench-it node

and compare if the results are drastically different. I can do it tonight if you aren't available to do so :)

@arthurfiorette
Copy link
Copy Markdown
Contributor Author

If I undestood correctly:

Baseline:

./bench-it.js $(which node) baseline

7e25625544dc7437012c5d5afa08e282b53cefd2
Running Node.js Package Benchmark...
--------------------------------------------
typescript
  transpile:                                85.48 (10 samples)
  createSourceFile:                         2.715K (272 samples)
  getSemanticDiagnostics:                   3.241 (10 samples)

did this changes:

diff --git a/src/typescript-benchmark.js b/src/typescript-benchmark.js
index cb7095a..85d06e5 100644
--- a/src/typescript-benchmark.js
+++ b/src/typescript-benchmark.js
@@ -1,6 +1,7 @@
 const fs = require("node:fs");
 const ts = require("typescript");
 const path = require("node:path");
+const assert = require("node:assert");

 const filePath = path.join(__dirname, "..", "fixtures", "ts-sample.ts");
 const code = fs.readFileSync(filePath, "utf8");
@@ -12,7 +13,7 @@ module.exports = {
                {
                        name: "transpile",
                        fn: () => {
-                               ts.transpile(
+                               const transpiled = ts.transpile(
                                        code,
                                        {
                                                // CJS Settings
@@ -32,18 +33,22 @@ module.exports = {
                                        },
                                        filePath,
                                );
+
+        assert.ok(transpiled)
                        },
                },
                {
                        name: "createSourceFile",
                        fn: () => {
-                               ts.createSourceFile(
+                               const source = ts.createSourceFile(
                                        filePath,
                                        code,
                                        ts.ScriptTarget.ESNext,
                                        false,
                                        ts.ScriptKind.TS,
                                );
+
+        assert.ok(source)
                        },
                },
                {
@@ -74,7 +79,9 @@ module.exports = {
                                        },
                                });

-                               program.getSemanticDiagnostics(program.getSourceFile(filePath));
+                               const diagnostics = program.getSemanticDiagnostics(program.getSourceFile(filePath));
+
+        assert.ok(diagnostics.length === 0);
                        },
                },
        ],
./bench-it.js $(which node)

Running Node.js Package Benchmark...
--------------------------------------------
typescript
  transpile:                                76.11 (10 samples)
  createSourceFile:                         2.61K (261 samples)
  getSemanticDiagnostics:                   3.102 (10 samples)

I don't think dead code elimination is in place... Should be that way, right?

@arthurfiorette
Copy link
Copy Markdown
Contributor Author

arthurfiorette commented May 17, 2024

I've also added another commit improving how the output is printed in TTY mode. Previously it collected all results and then printed it out, now it whenever a new result comes, it gets printed out.

It helps by giving feedback on what run it is currently, how many were already finished and how many are waiting to be ran.

Also added machine info in TTY mode, which gives some basic informations when mentally comparing results across machines/node versions.

However feel free to revert it if you don't like the result.

Copy link
Copy Markdown
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

LGTM.

@RafaelGSS RafaelGSS merged commit b086a74 into nodesource:main May 18, 2024
@RafaelGSS
Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Typescript.

2 participants