Optimize `Jekyll::Utils.parse_date` (#8425)

Merge pull request 8425
This commit is contained in:
fauno 2021-07-22 14:43:47 -03:00 committed by GitHub
parent 76517175e7
commit 0dee66260f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

25
benchmark/parse-date Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env ruby
require_relative '../lib/jekyll'
require 'benchmark/ips'
date = "2014-08-02 14:43:06 PDT".freeze
time = Time.parse(date)
Benchmark.ips do |x|
x.report('Time.parse') do
Time.parse(date)
end
x.report('localtime') do
Time.parse(date).localtime
end
x.report('localtime parsed') do
time.localtime
end
x.report('Utils.parse_date') do
Jekyll::Utils.parse_date(date)
end
end

View File

@ -128,7 +128,8 @@ module Jekyll
# Returns the parsed date if successful, throws a FatalException
# if not
def parse_date(input, msg = "Input could not be parsed.")
Time.parse(input).localtime
@parse_date_cache ||= {}
@parse_date_cache[input] ||= Time.parse(input).localtime
rescue ArgumentError
raise Errors::InvalidDateError, "Invalid date '#{input}': #{msg}"
end