Skip to content

Instantly share code, notes, and snippets.

@urbanautomaton
Last active April 22, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbanautomaton/9a8bdf82a3d88999f70559523d2df7ab to your computer and use it in GitHub Desktop.
Save urbanautomaton/9a8bdf82a3d88999f70559523d2df7ab to your computer and use it in GitHub Desktop.
$ ruby -Ilib benchmark.rb
Warming up --------------------------------------
require whitespace 18.000 i/100ms
don't require whitespace
19.000 i/100ms
Calculating -------------------------------------
require whitespace 195.417 (± 1.5%) i/s - 990.000 in 5.067471s
don't require whitespace
192.750 (± 2.6%) i/s - 969.000 in 5.030580s
Comparison:
require whitespace: 195.4 i/s
don't require whitespace: 192.7 i/s - same-ish: difference falls within error
require 'benchmark/ips'
require 'kramdown'
Benchmark.ips do |x|
x.report("require whitespace") do
ENV["REQUIRE_WHITESPACE"] = "true"
Kramdown::Document.new("## 1#{" "*20000}2")
end
x.report("don't require whitespace") do
ENV.delete("REQUIRE_WHITESPACE")
Kramdown::Document.new("## 1#{" "*20000}2")
end
x.compare!
end
diff --git a/lib/kramdown/parser/kramdown/header.rb b/lib/kramdown/parser/kramdown/header.rb
index 700597e..f680b57 100644
--- a/lib/kramdown/parser/kramdown/header.rb
+++ b/lib/kramdown/parser/kramdown/header.rb
@@ -31,7 +31,11 @@ def parse_setext_header
def parse_atx_header
return false unless after_block_boundary?
text, id = parse_header_contents
- text.sub!(/(?<!\\)#+\z/, '') && text.rstrip!
+ if ENV['REQUIRE_WHITESPACE']
+ text.sub!(/[\t ]#+\z/, '') && text.rstrip!
+ else
+ text.sub!(/(?<!\\)#+\z/, '') && text.rstrip!
+ end
return false if text.empty?
add_header(@src["level"].length, text, id)
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment