Revision 687785a00d665f3a134b28fcd2b3d0260e27c510 authored by Parker Moore on 03 August 2020, 20:29:36 UTC, committed by Parker Moore on 03 August 2020, 20:29:36 UTC
1 parent ac7b264
Raw File
regexp-vs-include.rb
#!/usr/bin/env ruby
require 'benchmark/ips'

# For this pull request, which changes Page#dir
# https://github.com/jekyll/jekyll/pull/4403

CONTENT_CONTAINING = <<-HTML.freeze
<!DOCTYPE HTML>
<html lang="en-US">
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <title>Jemoji</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="/css/screen.css">
  </head>
  <body class="wrap">
    <p><img class="emoji" title=":+1:" alt=":+1:" src="https://assets.github.com/images/icons/emoji/unicode/1f44d.png" height="20" width="20" align="absmiddle"></p>

  </body>
</html>
HTML
CONTENT_NOT_CONTAINING = <<-HTML.freeze
<!DOCTYPE HTML>
<html lang="en-US">
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <title>Jemoji</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="/css/screen.css">
  </head>
  <body class="wrap">
    <p><img class="emoji" title=":+1:" alt=":+1:" src="https://assets.github.com/images/icons/emoji/unicode/1f44d.png" height="20" width="20" align="absmiddle"></p>

  </body>
</html>
HTML

Benchmark.ips do |x|
  x.report("no body include?") { CONTENT_NOT_CONTAINING.include?('<body') }
  x.report("no body regexp")   { CONTENT_NOT_CONTAINING =~ /<\s*body/ }
  x.compare!
end

# No trailing slash
Benchmark.ips do |x|
  x.report("with body include?") { CONTENT_CONTAINING.include?('<body') }
  x.report("with body regexp")   { CONTENT_CONTAINING =~ /<\s*body/ }
  x.compare!
end
back to top