# Crystal, Rust, .NET, Swift, and JS relative performance

**URL:** <https://forum.crystal-lang.org/t/crystal-rust-net-swift-and-js-relative-performance/6874>\
**Category:** News\
**Created:** [May 28, 2024, 4:59am UTC](https://forum.crystal-lang.org/t/crystal-rust-net-swift-and-js-relative-performance/6874 "2024-05-28T04:59:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [May 28, 2024, 4:59am UTC](https://forum.crystal-lang.org/t/crystal-rust-net-swift-and-js-relative-performance/6874/1 "2024-05-28T04:59:13Z")

</div>

For the past few days I’ve been seeing some benchmarks floating around Twitter and the fediverse.

- [Miguel de Icaza compared Swift and .NET](https://mastodon.social/@Migueldeicaza/112502002209480046)
- [Twitter user `@iSeiryu` compared Rust, C#, and JS](https://twitter.com/iSeiryu/status/1793830738153889902)

There were some others, but these two were the ones I saw the most.

Saving you some clicks: other than JS, they’re all within a few percentage points of each other. In all cases, the benchmark was a web app that both receives and responds with equivalent JSON payloads. I suppose the idea was to benchmark something more substantial than a “hello world” benchmark. I still don’t think that’s enough work to benchmark, but we’ll get to that in a minute.

My understanding is that C# and .NET are used interchangeably. I’ve never worked in that ecosystem, though, so feel free to check me on that.

I ran some benchmarks of my own to see how well Crystal fares because I’m always interested in the relative performance. Now, since these two folks have effectively shown that Swift, .NET, and Rust all perform pretty closely on this particular benchmark, comparing to one of them also compares to the others.

On my machine, the Rust implementation (copy/pasted from the gist linked in the tweet) handled 97k RPS across 1M total requests:

```auto
Summary:
  Total:	10.3051 secs
  Slowest:	0.1218 secs
  Fastest:	0.0000 secs
  Average:	0.0003 secs
  Requests/sec:	97038.9722

```

This is great. It’s incredibly fast. This gives a baseline for comparison to the Crystal implementation … which does 98k RPS for the same total number of requests (with `-Dpreview_mt` — important because the other implementations were also using multiple CPU cores):

```auto
Summary:
  Total:	10.1531 secs
  Slowest:	0.2014 secs
  Fastest:	0.0000 secs
  Average:	0.0003 secs
  Requests/sec:	98492.1149

```

This means that Crystal is as fast as Rust, .NET, and Swift at parsing and emitting these particular JSON payloads. But this isn’t a realistic workload. Parsing and emitting JSON is _part_ a realistic workload, but to dial up the realism a bit, you should at least talk to some kind of data store.

I augmented the Rust benchmark to make a single Redis query while handling the request. This can simulate persisting the data being sent from the client, sticking the data into a queue to be processed asynchronously, or really anything beyond simply transforming one JSON payload into another — there are more efficient ways to do that than sending an HTTP call.

The code is [here](https://gist.github.com/jgaskins/2ea2657ec9ffacb90cdc09e31c6342da).

With _only_ that change between them, here are the results from the Rust implementation:

```auto
Summary:
  Total:	24.4925 secs
  Slowest:	0.0704 secs
  Fastest:	0.0001 secs
  Average:	0.0006 secs
  Requests/sec:	40828.8615

```

And the Crystal implementation:

```auto
Summary:
  Total:	16.8755 secs
  Slowest:	0.0659 secs
  Fastest:	0.0000 secs
  Average:	0.0004 secs
  Requests/sec:	59257.6096

```

Crystal was on par with Rust when only parsing/emitting HTTP and JSON, but as soon as I added a single data store query in there, Crystal pulled ahead by almost 50%. Even the latency distribution was similar.

Rust:

```txt
Latency distribution:
  10% in 0.0004 secs
  25% in 0.0005 secs
  50% in 0.0006 secs
  75% in 0.0007 secs
  90% in 0.0008 secs
  95% in 0.0009 secs
  99% in 0.0013 secs

```

Crystal:

```txt
Latency distribution:
  10% in 0.0002 secs
  25% in 0.0002 secs
  50% in 0.0003 secs
  75% in 0.0005 secs
  90% in 0.0006 secs
  95% in 0.0007 secs
  99% in 0.0015 secs

```

Previous notable performance benchmarks I’ve done between Crystal and Rust:

- This very same Redis client [outperformed the Rust Redis crate](https://dev.to/jgaskins/performance-comparison-rust-vs-crystal-with-redis-1a17) several years ago, too
  - Originally, we thought this might’ve been due to the Rust implementation [not having been optimized for pipelined queries](https://dev.to/marwes/comment/113b0), but it holds in this case even for a single query

- One I did for a [performance optimization in the `will/crystal-pg` shard](https://github.com/will/crystal-pg/pull/228) a few years ago showing that, with that optimization, Crystal and Rust perform equivalently on the CPU in talking to Postgres
