Skip to content

Instantly share code, notes, and snippets.

@codingfoo
Last active December 11, 2015 20:59
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 codingfoo/4659547 to your computer and use it in GitHub Desktop.
Save codingfoo/4659547 to your computer and use it in GitHub Desktop.
Javascript parsing benchmark
script = open(ARGV[0]).read
require "benchmark"
include Benchmark
require 'rkelly'
require 'parsejs'
require 'violet'
require 'content_urls'
parser = RKelly::Parser.new
n = 100
Benchmark.benchmark(CAPTION, 12, FORMAT, ">% Change:") do |x|
t_gsub = x.report("gsub") do
script.gsub!('window.location','foo.window.location')
script.gsub!('foo.window.location.replace','ot.window.location_replace')
script.gsub!('foo.window.location.href','ot.window.location.ot_href')
script.gsub!('document.location','foo.document_location')
script.gsub!('foo.document_location.replace', 'foo.document_location_replace')
script.gsub!('foo.document_location.href','foo.document_location.ot_href')
script.gsub!('top.location','foo.top_location')
end
t_parser4 = x.report("content_urls") do
res = ContentUrls::JavaScriptParser.rewrite_each_url(script) { |url| url.upcase }
end
t_parser = x.report("rkelly") do
ast = parser.parse( script )
ast.each do |node|
node
# node.value = 'foo.window.location' if node.value == 'window.location'
# node.name = 'foo.window.location' if node.respond_to?(:name) && node.name == 'window.location'
end
ast.to_ecma
end
t_parser2 = x.report("parsejs") do
ast = ParseJS.parse( script )
ParseJS::Stringifier.to_string(ast)
end
t_parser3 = x.report("violet") do
ast = Violet::Parser.parse(script)
ast.to_s
end
[( (t_parser4 - t_gsub) / t_gsub ) * 100] unless t_gsub == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment