hoxworth/json-schema — pull request #156 by pd (2014-10-27 19:22:40+00:00)
Title: Store static regexps in constants for re-use

Since they're static, there's no need to re-compile it every time.

Microbenchmark:

~~~ruby
require 'benchmark'

STR = '2010-01-01T12:00:00Z'
RGXSTR = '^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:?\d\d)?)?$'
RGX = Regexp.new(RGXSTR)

N = 10_000

Benchmark.bmbm do |x|
x.report('re-compiled') do
N.times { Regexp.new(RGXSTR).match(STR) }
end

x.report('precompiled') do
N.times { RGX.match(STR) }
end
end

__END__
~ [chruby:2.1.1] » ruby bench.rb
Rehearsal -----------------------------------------------
re-compiled 0.770000 0.000000 0.770000 ( 0.779001)
precompiled 0.010000 0.000000 0.010000 ( 0.011617)
-------------------------------------- total: 0.780000sec

user system total real
re-compiled 0.760000 0.000000 0.760000 ( 0.766040)
precompiled 0.010000 0.000000 0.010000 ( 0.008398)
~~~

I've got an app that is using draft3 + a lot of `format: "date"` and this made a decent dent in the runtime for tests validating the API.