DEV Community

Vincent Chan
Vincent Chan

Posted on • Edited on

Which programming languages run faster in the browser?

Nowadays, we often hear about fancy languages and technologies that claim to have amazing performance. With WASM, we have C++ and Rust, both are performance killers on the native machine.

However, it’s hard to know how much we can actually gain from them. On the other hand, it takes a lot of time to learn or to switch to them. So, it’s important to compare the costs and benefits.

We need a standard benchmark to measure performance between languages. This is what I will do in this article. We will choose some basic but common scenarios to test and compare the results.

Last but not least, no benchmark can 100% reflect the actual scenarios you will encounter. If you have a very sensitive performance scenario, I suggest you write your own benchmark. But for most common scenarios, I hope this article will help.

Playground

We provide a playground where you can run benchmarks on your favorite browsers and OS. The results may vary slightly depending on the environment. If the differences in the timing of different languages in the same test are not large, then we can regard it as the same.

And you can find all the source code here.

Basic language tests

We borrowed some tests from the debian’s benchmark games. It provides some basic tests that can measure the languages. The data structures used in the tests are very similar to those we commonly encounter, so I think this is a test worth referencing.

One thing to notice is that we run each test in a single thread. However, each test is run in a worker. We want to know the performance of each language in a single thread.

BinaryTrees

You can check the detail of this test here. Now we got the result(less is better):

Image description

If we run the test for the first time, we can see that Rust in Wasm got the best score. But if we run again, we’ll find that JavaScript got the best score.

Image description

Don’t be surprised if you run the tests multiple times; the V8 engine usually gets better when it runs the second time (it will optimize the code based on the runtime info). And you will find that the result is unstable. The truth is, these three languages don’t have a large gap in performance. Actually, I don’t think C++ and Rust have any advantage in this competition.

FanncukRedux

You can check the detail of this test here.

Image description

This test has a relatively stable result. JavaScript is the best. We get the same result in multiple tests.

Practical tests

How a language behaves in production depends not only on the language engine itself but also on the ecosystem. The underlying libraries your app uses will determine its performance. In this group, we chose some popular low-level libraries on their platform to test.

Deflate compress

We use the deflate algorithm to compress Hamlet in this test. Deflate is the default algorithm we use for the zip format.

One thing to notice is that in the JS part, we use the CompressionStream from the browser. That doesn't reflect the performance of the JavaScript engine itself. But in the browser environment, it's the best we have.

Image description

From the results, we can see the browser's built-in API has a significant advantage over WASM. C++ has a slight advantage over Rust, but not a large one.

Font parsing

In this test, we choose three libraries from three languages to parse a font format and measure the horizontal advance of a very long text.

Image description

In the results, we can see Rust has a significant advantage. We can see ttf-parser is literally designed for performance.

Conclusion

In conclusion, this benchmark study provides a comprehensive comparison of various programming languages running in the browser. Although some languages have slight advantages, the performance differences are often not substantial. Some languages, like Rust with its font parser, excel in specific areas. If you need that specific advantage, go for it. But most of the time, JavaScript is sufficient.

Top comments (0)