|
| 1 | +local bm = require 'benchmark' |
| 2 | +local urlencode = require 'urlencode' |
| 3 | + |
| 4 | +local reporter = bm.bm(6) |
| 5 | + |
| 6 | +-- original code from -> https://gist.github.com/cgwxyz/6053d51e8d7134dd2e30 |
| 7 | +function encodeURI(str) |
| 8 | + if (str) then |
| 9 | + str = string.gsub (str, "\n", "\r\n") |
| 10 | + str = string.gsub (str, "([^%w ])", |
| 11 | + function (c) return string.format ("%%%02X", string.byte(c)) end) |
| 12 | + str = string.gsub (str, " ", "+") |
| 13 | + end |
| 14 | + return str |
| 15 | +end |
| 16 | + |
| 17 | +function decodeURI(s) |
| 18 | + if(s) then |
| 19 | + s = string.gsub(s, '%%(%x%x)', |
| 20 | + function (hex) return string.char(tonumber(hex,16)) end ) |
| 21 | + s = string.gsub(s, "+", " ") |
| 22 | + end |
| 23 | + return s |
| 24 | +end |
| 25 | + |
| 26 | +reporter:report(function() |
| 27 | + for i = 1, 1e6 do |
| 28 | + encodeURI("hello こんにちは สวัสดี") |
| 29 | + end |
| 30 | +end, 'pure lua: encode url') |
| 31 | + |
| 32 | +reporter:report(function() |
| 33 | + for i = 1, 1e6 do |
| 34 | + urlencode.encode_url("hello こんにちは สวัสดี") |
| 35 | + end |
| 36 | +end, 'encodeurl: encode url') |
| 37 | + |
| 38 | +reporter:report(function() |
| 39 | + for i = 1, 1e6 do |
| 40 | + decodeURI("hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E0%B8%AA%E0%B8%A7%E0%B8%B1%E0%B8%AA%E0%B8%94%E0%B8%B5") |
| 41 | + end |
| 42 | +end, 'pure lua: decode url') |
| 43 | + |
| 44 | +reporter:report(function() |
| 45 | + for i = 1, 1e6 do |
| 46 | + urlencode.decode_url("hello+%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF+%E0%B8%AA%E0%B8%A7%E0%B8%B1%E0%B8%AA%E0%B8%94%E0%B8%B5") |
| 47 | + end |
| 48 | +end, 'encodeurl: decode url') |
| 49 | + |
0 commit comments