看了一个家伙写的《Is Rails Slow?》的ppt,起了测试模板速度的念头。ppt里测试的是haml比erb快,那么我就测试一下slim和erb之间的速度把。我用的gem版本如下:
gemfile
source 'https://rubygems.org'
ruby '2.1.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'slim-rails', '2.1.4'
代码如下:
#users_controller.rb
class UsersController < ApplicationController
layout :false
def index
@users = User.all.to_a
end
def show
@user = User.find_by_id(params[:id])
end
def index_slim
@users = User.all.to_a
end
def show_slim
@user = User.find_by_id(params[:id])
end
end
routes.rb
resources :users, only: [:index, :show] do
member do
get :show_slim
end
collection do
get :index_slim
end
end
index.html.erb
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Phone</td>
<td>Address</td>
<td>Function</td>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.phone %></td>
<td><%= user.address %></td>
<td><%#= link_to "#{user.name}", user_path(user.id) %></td>
</tr>
<% end -%>
</table>
</body>
</html>
index_slim.html.slim
html
head
title
body
table
tr
td Name
td Phone
td Address
td Function
- @users.each do |user|
tr
td = user.name
td = user.phone
td = user.address
td
/= link_to "#{user.name}", show_slim_user_path(user.id)
================快乐的分割线====================================
下面是测试结果:
注释掉了URL helpers的情况下:
slim的速度:
1~3ms之间浮动
Rendered users/index_slim.html.slim (3.0ms) Rendered users/index_slim.html.slim (1.1ms) Rendered users/index_slim.html.slim (3.0ms) Rendered users/index_slim.html.slim (2.1ms) Rendered users/index_slim.html.slim (1.1ms)erb的速度:
2~6ms之间浮动
Rendered users/index.html.erb (2.4ms) Rendered users/index.html.erb (3.7ms) Rendered users/index.html.erb (3.2ms) Rendered users/index.html.erb (5.7ms) Rendered users/index.html.erb (3.0ms)
加上URL helpers的情况下:
slim的速度:
Rendered users/index_slim.html.slim (9.1ms) Rendered users/index_slim.html.slim (11.1ms) Rendered users/index_slim.html.slim (6.9ms) Rendered users/index_slim.html.slim (8.9ms) Rendered users/index_slim.html.slim (6.9ms) Rendered users/index_slim.html.slim (7.4ms)erb的速度:
Rendered users/index.html.erb (14.1ms) Rendered users/index.html.erb (10.2ms) Rendered users/index.html.erb (13.2ms) Rendered users/index.html.erb (11.4ms) Rendered users/index.html.erb (12.3ms) Rendered users/index.html.erb (11.0ms)==============================================================
结论:Slim在有没有URL helper的情况下都会比erb模板的生成html速度快