How fast is Elixir Phoenix than Rails?

Straight and Simple comparison between Elixir Phoenix and Rails Speed.

How fast is Elixir Phoenix is then Rails?

Introduction

I am a Ruby on Rails developer. Since the time I have heard of Elixir Phoenix and started working on it, I have always heard about the performance of Elixir Phoenix. Having been a Rails developer myself for a long time, I have always compared Elixir Phoenix’s speed with Rails. This urge of mine for comparing Elixir Phoenix’s speed with Rails is not new. I have seen multiple blogs, articles, and questions on forums about the same. It was about time that I had to compare them myself and see the difference.

I thought of comparing them by making a very similar application. I decided to do it by creating a simple CRUD application of Topics. The applications had the following actions for a Topic model (module in Elixir Phoenix) for both Elixir Phoenix and Rails.

  1. Create
  2. Read
  3. Update
  4. Delete

I will compare the time taken for the server to render the response and the final time taken for the views to appear on the browser for the index action. The server for both Elixir Phoenix and Rails application will be running on my local machine and their Postgres database will be same. I will also compare the ways to perform similar tasks in Elixir Phoenix and Rails by listing down the commands that are used for them.

Topic Application in Rails

Let’s start by creating the application in Rails. To create a Rails application, I used the following commands.

  1. rails new topic_app: To create a rails app with boilerplate code.
  2. bundle install: To install the gems that are listed in the Gemfile.
  3. rails g model Topic: To create a Topic model.
  4. rake db:migrate: To run all the pending migrations.
  5. rails g controller topics: To generate the file for topic controller

I added the routes for create, index, update, and delete in the config/routes.rb file as shown below.

Rails routes

Above code will create the routes for create, index, update, and delete actions for TopicsController. Below is the link to topics_controller.rb file in the GitHub repository of the application code for Topics Controller.

Click Here to view the full rails code of the application.

It’s time to note down the timing of the index action of the Rails app. Below is the screenshot of the request from my local server.

Rails Index action Local Server Logs

Above screenshot shows that the Rails server took around 32 ms to respond with the index action response. Below screenshot shows the developer console logs from google chrome for the same action.

Chrome Developer Console Shows for the index action

Above screenshot shows that it took 58.49ms to get the index action response and render it on the webpage.

Topic Application in Elixir Phoenix

Let’s create the application in Elixir Phoenix. To create an Elixir Phoenix application, I used the following commands:

  1. mix phx.new topic_new — module Topic: To create a Elixir Phoenix application with boilerplate code.
  2. mix deps.get & mix deps.compile: To install and compile the libraries that are listed in the mix.exs file.
  3. mix ecto.migrate: To run all the pending migrations.

I added the routes for create, index, update, and delete in the router.ex file as shown below.

Elixir Phoenix Routes

Note that we create a module using defmodule in Elixir Phoenix and use other modules using use, alias, etc.

Similar to the Rails application, the CRUD routes for the topics are added using resources.

Below is the link to topic_controller.ex file in the GitHub repository of the application code for Topics Controller.

Click Here to view the full elixir phoenix code of the application.

Again the time has come to check the server response time for the same index action of the Elixir Phoenix application for the same topics in the same database and compare it with the Rails application.

Below is the screenshot of the request from my local server for the index action.

Rails Index action Local Server Logs

Above screenshot shows that it took only 5ms (in comparison to 32 ms in Rails). Let’s see the timings of the index action from the Elixir Phoenix application in the google chrome developer console.

Google Chrome Developer Console Response Timing

Above screenshot shows that the response timings of the index action from Elixir Phoenix application are around 28 ms (in comparison to 58ms in Rails application).

Conclusion

From the above timings, it is clear that Elixir Phoniex application's speed is more than that of Rails application’s speed. This performance difference certainly helps at the production level. But the overall performance of the application also depends on the other factors which include the architecture, queries, etc. in the application.

Why The Difference?

In my opinion, the time difference that we are seeing here is mainly because of the compilation nature of the two languages. Elixir Phoenix runs on Elixir and is compiled before the program is executed. On the contrary, ruby is a runtime language and is not compiled before its execution.

The Difference will be clearly visible for bigger applications

However, there are many other factors such as Erlang VM processes and OTP concepts built on them make Elixir Phoenix’s performance better than ruby. But these do not create much difference here in our case because there our application is being run on a single server without any concurrency to test the performance differences that are caused due to Erlang VM processes.

For the projects that are about to go big and get many users, we will definitely see the difference in the performance because it will be easy to scale the application in case of Elixir Phoenix using BEAM (Erlang VM) processes. We can easily perform multi-core processing with Elixir Phoenix.

The time difference for other actions

Here is the timing comparison of the other CRUD operations in Elixir Phoenix vs Rails application. Note that the server timings may be different from the ones mentioned above. But the difference between them is almost the same.

Comparison of server and browser timings of CRUD actions of similar Elixir Phoenix and Rails application

❤️ Like, Share or Leave A Comment!

If you enjoyed this post, don’t forget to give it a 👏🏼, share it with a friend you think might benefit from it and leave a comment! Stay tuned for more exciting blogs on Flutter, Elixir, React, Angular, Ruby, etc.

Thanks !!

--

--