{"action":"created","issue":{"url":"https://api.github.com/repos/component/escape-html/issues/17","labels_url":"https://api.github.com/repos/component/escape-html/issues/17/labels{/name}","comments_url":"https://api.github.com/repos/component/escape-html/issues/17/comments","events_url":"https://api.github.com/repos/component/escape-html/issues/17/events","html_url":"https://github.com/component/escape-html/pull/17","id":103900503,"number":17,"title":"Optimize","user":{"login":"TimothyGu","id":1538624,"avatar_url":"https://avatars.githubusercontent.com/u/1538624?v=3","gravatar_id":"","url":"https://api.github.com/users/TimothyGu","html_url":"https://github.com/TimothyGu","followers_url":"https://api.github.com/users/TimothyGu/followers","following_url":"https://api.github.com/users/TimothyGu/following{/other_user}","gists_url":"https://api.github.com/users/TimothyGu/gists{/gist_id}","starred_url":"https://api.github.com/users/TimothyGu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TimothyGu/subscriptions","organizations_url":"https://api.github.com/users/TimothyGu/orgs","repos_url":"https://api.github.com/users/TimothyGu/repos","events_url":"https://api.github.com/users/TimothyGu/events{/privacy}","received_events_url":"https://api.github.com/users/TimothyGu/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/component/escape-html/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"closed","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":4,"created_at":"2015-08-30T04:36:45Z","updated_at":"2015-09-01T02:27:37Z","closed_at":"2015-09-01T02:11:35Z","pull_request":{"url":"https://api.github.com/repos/component/escape-html/pulls/17","html_url":"https://github.com/component/escape-html/pull/17","diff_url":"https://github.com/component/escape-html/pull/17.diff","patch_url":"https://github.com/component/escape-html/pull/17.patch"},"body":"Implementation adapted from Jade's. About 2-8x faster.\r\n\r\nBenchmark:\r\n\r\n```js\r\nvar Benchmark = require('benchmark');\r\nvar suite = new Benchmark.Suite;\r\n\r\nfunction oldEscape(html) {\r\n  return String(html)\r\n    .replace(/&/g, '&amp;')\r\n    .replace(/\"/g, '&quot;')\r\n    .replace(/'/g, '&#39;')\r\n    .replace(/</g, '&lt;')\r\n    .replace(/>/g, '&gt;');\r\n}\r\n\r\nvar matchHtml = /[\"'&<>]/;\r\n\r\nfunction newEscape(html) {\r\n  html = '' + html;\r\n  var regexResult = matchHtml.exec(html);\r\n  if (!regexResult) return html;\r\n\r\n  var result = '';\r\n  var i, lastIndex = 0, escape;\r\n  for (i = regexResult.index; i < html.length; i++) {\r\n    switch (html.charCodeAt(i)) {\r\n      case 34: escape = '&quot;'; break;\r\n      case 39: escape = '&#39;';  break;\r\n      case 38: escape = '&amp;';  break;\r\n      case 60: escape = '&lt;';   break;\r\n      case 62: escape = '&gt;';   break;\r\n      default:                    continue;\r\n    }\r\n    if (lastIndex !== i) result += html.substring(lastIndex, i);\r\n    lastIndex = i + 1;\r\n    result += escape;\r\n  }\r\n  if (lastIndex !== i) return result + html.substring(lastIndex, i);\r\n  else                 return result;\r\n}\r\n\r\n// add tests\r\nsuite.add('no escape – old', function() {\r\n  oldEscape('Hello World!');\r\n}).add('no escape – new', function() {\r\n  newEscape('Hello World!');\r\n}).add('light escape – old', function() {\r\n  oldEscape('Hello & World!');\r\n}).add('light escape – new', function() {\r\n  newEscape('Hello & World!');\r\n}).add('heavy escape – old', function() {\r\n  oldEscape('\\'>\\'\"\"&>h<e>&<y>');\r\n}).add('heavy escape – new', function() {\r\n  newEscape('\\'>\\'\"\"&>h<e>&<y>');\r\n}).on('cycle', function (event) {\r\n  console.log(String(event.target));\r\n}).run()\r\n```\r\n\r\n```\r\n$ node ./j\r\nno escape – old x 1,553,747 ops/sec ±1.55% (93 runs sampled)\r\nno escape – new x 13,234,354 ops/sec ±0.77% (98 runs sampled)\r\nlight escape – old x 1,325,111 ops/sec ±1.41% (98 runs sampled)\r\nlight escape – new x 3,418,685 ops/sec ±0.59% (98 runs sampled)\r\nheavy escape – old x 744,608 ops/sec ±0.66% (96 runs sampled)\r\nheavy escape – new x 2,237,829 ops/sec ±0.58% (92 runs sampled)\r\n```\r\n\r\n```js\r\n$ node -pe process.versions\r\n{ http_parser: '2.5.0',\r\n  node: '3.2.0',\r\n  v8: '4.4.63.26',\r\n  uv: '1.6.1',\r\n  zlib: '1.2.8',\r\n  ares: '1.10.1-DEV',\r\n  modules: '45',\r\n  openssl: '1.0.2d' }\r\n```"},"comment":{"url":"https://api.github.com/repos/component/escape-html/issues/comments/136555359","html_url":"https://github.com/component/escape-html/pull/17#issuecomment-136555359","issue_url":"https://api.github.com/repos/component/escape-html/issues/17","id":136555359,"user":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"created_at":"2015-09-01T02:27:37Z","updated_at":"2015-09-01T02:27:37Z","body":"Looks like that took a hit on the performance ;)"}}