#Typescript faster than built javascript?

1 messages · Page 1 of 1 (latest)

stuck elm
#

I have a Node.js data processing CLI script. Just files in and files out with no external APIs or database connections. I spent the weekend converting it to Bun. I love the dev experience. And it is faster. But I can't figure out why the uncompiled Typescript is running faster than the built JS. Here is my build command:

"build": "rm -rf ./dist/* && bun build --entrypoints ./src/index.ts ./src/index-batch.ts --outdir ./dist --target bun",

I also tried the --compile option and it ran 2x slower than Typescript.

Here is how I run the benchmarks. There are 8 CLI args but using a couple as examples. Batch processing uses Bun.spawn() to run index.ts (or index.js if compiled).

hyperfine 'bun src/index.ts --a a --b b'
hyperfine 'bun dist/index.js --a a --b b'
hyperfine 'bun src/index-batch.ts -a a --b b'
hyperfine 'bun dist/index-batch.js -a a --b b'

Any ideas on how this can be? My understanding is that Bun ignores Typescript signatures or strips them out and that is slightly slower because of it. Either way, I am looking for the fastest execution because I need to run millions of these jobs regularly.

Thanks!

quasi heath
#

compiled being that slow is surprising though

stuck elm
#

I'm using --target bun. Is there another target I should be using?

late lava
#

this is very strange

#

can you try bun build --compile --minify ./src/index.ts

#

specifically with --minify

#

if the entry point is CommonJS that might explain it

stuck elm
#

Yes, I used --compile --minify before and the batch run was ~2x slower. But I can run it now for completeness

#

The entrypoint is a Typescript file with the imports looking like this:

import _ from 'lodash'
import { writeFileSync, mkdirSync } from 'fs'

stuck elm
#

Similar results as my attempt before. Here are my build commands:

    "build-native": "bun build --compile --minify ./src/index.ts --outfile ./dist/index",
    "build-native-batch": "bun build --compile --minify ./src/index-batch.ts --outfile ./dist/index-batch",
late lava
#

my guess is

#

there are large commonjs modules

#

and then those get eagerly-loaded

#

due to ESM

#

when using bun build

#

minified shouldn't be slower than the other one though

stuck elm
#

Ok, this is helpful. This is a 63MB import:

import defaultsLookup from '../../../assets/defaults-3.1.0/defaults-lookup.json'

I've got several assets imports like this but that is by far the biggest.

Do you have suggestions on how I would handle large imports though?

#

btw, thank you so much for working so hard on this project. Already I can tell it is such a nicer experience than the Node ecosystem

late lava
#

oh that 100% explains it

#

we transpile the json to js

#

parsing JS is slower than parsing JSON

quasi heath
#

why is it transpiled then?

late lava
#

it probably shouldn't be

#

but it does allow for tree-shaking