F# vs TypeScript performance - Sorting 1 million elements

Date: 2023-08-02 | create | tech | fsharp | typescript | performance |

Recently I've been benchmarking common functionality in my favorite languages, including sorting large amounts of elements in memory:

After putting in the work to calculate and gather this data, I thought it was only natural to try and compare them.

In this post we'll try to answer the question:

Q: How does sorting performance compare between F# and TypeScript?

We'll look at this from a few different angles, including:

  • Different data sizes
  • Different data types


The answer is it depends - which itself is surprising. I assumed that F# would blow TS out of the water given it's a compiled language that theoretically runs on a faster overall runtime - .NET.

In most cases F# does come out on top, but not always.

Results Overview (median of p90 % comparison Results)

  • Sorting Numbers: F#, faster by ~96%
  • Sorting Strings: TS, faster by ~11%
  • Sorting objects by number property: F#, faster by ~50%
  • Sorting objects by string property: F#, faster by ~35%

Note: An updated version of the benchmark shows TypeScript's number sorting is a bit faster with F# beating it by ~70%.

More detailed results available in the Results section.

Benchmark Setup

Before going further I think it's important to give an idea of how these results were calculated.


The benchmark code is a hand-rolled, naive benchmark implementation. It takes in code to benchmark and times it using the runtime-native Date.now() implementation.

More details (including full, runnable code) available in the respective benchmark posts:


To try and make the benchmarks as fair and reproducible as possible wrt hardware, we're running on free Replit instances:

  • CPU: 0.5 vCPU
  • RAM: 512 MB


As mentioned earlier F# wins in most cases but it's fascinating that it doesn't win in all.

For the comparison, we're using the p90 sort times (in milliseconds) for given (dataSize, dataType) scenarios across 200 iterations.

F# Raw Results:

F# - Sorting Items (P90)

TS Raw Results:

TypeScript - Sorting Items (P90)

F# / TS % Comparison Results:

F# vs TypeScript - Sorting Items

Personally - I expected F# to be about 5-10x faster than TypeScript as it's compiled. What I definitely didn't expect is for TypeScript to beat out F# at any operation, yet it happened.

F# does hit the 10x mark for sorting numbers and even hits ~2x mark for sorting objects by number / string properties. My guess is that this gets more pronounced if we start looking at parallelizable workloads though here we're mostly single thread bound.

The string sorting is so weird that it makes me think there's either something wrong with the benchmark or something weird with the runtime - either .NET is very slow or JS / Node is just surprisingly fast.

Next Steps

While these benchmarks are interesting, not sure how useful this data is.

  • F# is of course faster than TS in most cases
  • You still need to benchmark your own system as that's the best data you'll get
  • Maybe someone should look into why F# string performance is so bad

If you liked this post, you might like these:

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.