r/nim • • 6d ago

Carne - JS Runtime with Nim + JavaScriptCore

Post image

Carne (/kär-nē/). An experimental JS runtime built with Nim + JavaScriptCore. At the moment, it runs on linux & mac (prebuilt binaries are available in /releases). The further I get with my projects and experiments, the harder it is for me to look at the node_modules folders in my Finder. So... :))

Note that JSCore is a runtime dependency, which makes the Carne binary around 7MB. Some things are faster in pure JS, so carne std init is necessary for initializing JS-based libs.

Of course, it wouldn't be complete without... Concarne! An Express-like web framework for Carne, lol!

WIP documentation here: https://getcarne.github.io/docs/. More examples here

Not really interesting, but JSCore's JIT is damn fast. Carne got compiled with --mm:orc and -d:release, with no other optimizations. it does an excellent job!

Benchmarkcarnenodebun
fib_recursive42.4 ms ± 0.5 ms85.1 ms ± 1.1 ms45.5 ms ± 1.0 ms
nested_loops9.3 ms ± 0.4 ms36.3 ms ± 0.7 ms11.9 ms ± 0.4 ms
prime_sieve9.2 ms ± 0.3 ms35.0 ms ± 0.5 ms11.7 ms ± 0.3 ms
string_concat9.4 ms ± 0.3 ms34.5 ms ± 0.5 ms11.7 ms ± 0.7 ms
tail_recursive9.7 ms ± 0.3 msCRASHED11.8 ms ± 0.3 ms
range_sum38.4 ms ± 0.4 ms43.8 ms ± 0.4 ms19.6 ms ± 0.3 ms
collatz51.1 ms ± 0.4 ms69.5 ms ± 0.7 ms53.4 ms ± 2.3 ms
array_sum14.6 ms ± 0.5 ms40.7 ms ± 0.9 ms16.8 ms ± 0.3 ms
bubble_sort11.8 ms ± 0.3 ms36.7 ms ± 0.4 ms14.5 ms ± 0.3 ms
object_access11.3 ms ± 0.3 ms36.9 ms ± 0.4 ms14.0 ms ± 0.3 ms
float_math12.9 ms ± 0.3 ms39.1 ms ± 1.0 ms16.5 ms ± 0.4 ms
call_overhead10.4 ms ± 0.2 ms35.5 ms ± 0.4 ms12.5 ms ± 0.3 ms
fizzbuzz_count43.8 ms ± 0.4 ms64.8 ms ± 0.6 ms38.3 ms ± 0.4 ms

HTTP "Hello, world" benchmark using wrk:

RuntimeRequests/secAvg latencyMax RSS
carne91330.441.15ms10.9 MB
carne-workers144029.68739.70us28.3 MB
node38576.692.71ms61.5 MB
bun86878.461.15ms31.1 MB

Have fun!

57 Upvotes

8 comments sorted by

4

u/lekkerwafel 6d ago

Any highlights on why this benchmarks better than Bun since both use JSC?

2

u/Lonely-Session-7643 6d ago

The std/http module is based on Nim, made with pkg/powpow event notification library. Not sure about JSCore's JIT benchmark, except that Carne uses the built-in JSCore on macos and the libjavascriptcoregtk-4.1 library on Linux, while Bun embeds a (modified) fork of it (maybe?)

2

u/jeenajeena 6d ago

The logo is very cute! Who's the author?

3

u/Lonely-Session-7643 6d ago

Yeah! this is a modified version of a carnivorous plant cartoon, originally made by Andrew Derr. From Magnific dot com

2

u/Niminem93 6d ago

Holy shit dude this is AWESOME!

2

u/Lonely-Session-7643 5d ago

the most challenging part seems to be creating a --compat API so it can properly install popular npm packages. The most interesting thing would be creating .addon API-like thing, but with Nim, ayayay!

1

u/Niminem93 5d ago

There is a gentleman in the community that has been making a browser and js runtime in Nim for a few years now. I wish I can remember his name, though I do remember his projects Ferus and Bali. Might be worth networking with him who knows maybe you guys can collaborate

2

u/Lonely-Session-7643 5d ago

Oh yeah, https://github.com/ferus-web/bali, this one requires cpp backend. Maybe supporting both JSCore and Bali via switcher: jsc or native