Module: BenchmarkToJs::ViewHelpers

Defined in:
lib/benchmark_to_js/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#benchmark_to_js(name = 'block', &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/benchmark_to_js/view_helpers.rb', line 3

def benchmark_to_js(name = 'block', &block)
  if BenchmarkToJs.configuration.run?
    ret = ''
    time = Benchmark.ms {ret = capture(&block)}

    message = if BenchmarkToJs.configuration.pretty
      color = if time >= BenchmarkToJs.configuration.red_threshold
        'red'
      elsif time >= BenchmarkToJs.configuration.orange_threshold
        'darkorange'
      else
        'green'
      end

      %Q("%c #{escape_javascript(name)} %c ran in %c#{time.round(5)}ms", 'background: #222; color: #bada55', '', 'color:#{color}')
    else
      %Q("'#{escape_javascript(name)}' ran in #{time.round(5)}ms")
    end

    ret + javascript_tag(%Q(console.log(#{message})))
  else
    capture(&block)
  end
end