diff options
Diffstat (limited to 'bench')
-rw-r--r-- | bench/snippets/escapeHTML.js | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/bench/snippets/escapeHTML.js b/bench/snippets/escapeHTML.js index 63e68861d..b6330b630 100644 --- a/bench/snippets/escapeHTML.js +++ b/bench/snippets/escapeHTML.js @@ -1,27 +1,10 @@ import { group } from "mitata"; import { bench, run } from "mitata"; +import { encode as htmlEntityEncode } from "html-entities"; +import { escape as heEscape } from "he"; var bunEscapeHTML_ = globalThis.escapeHTML || Bun.escapeHTML; -var bunEscapeHTML = function (str) { - if (str.length === 1) { - switch (str.charCodeAt(0)) { - case 34: // " - return """; - case 38: // & - return "&"; - case 39: // ' - return "'"; // modified from escape-html; used to be ''' - case 60: // < - return "<"; - case 62: // > - return ">"; - default: - return str; - } - } - - return bunEscapeHTML_(str); -}; +var bunEscapeHTML = bunEscapeHTML_; const matchHtmlRegExp = /["'&<>]/; @@ -44,6 +27,11 @@ const FIXTURE = require("fs") }) .join(""); +const FIXTURE_WITH_UNICODE = require("fs").readFileSync( + import.meta.dir + "/_fixture.txt", + "utf8" +); + function reactEscapeHtml(string) { const str = "" + string; const match = matchHtmlRegExp.exec(str); @@ -90,25 +78,29 @@ function reactEscapeHtml(string) { } for (let input of [ - // " ", - // "<script>alert('xss')</script>", - // "hello world", - // "hello world<script>alert('xss')</script>", - // "<", - // ">", - // `short value`, - `nothing to escape `.repeat(99999), + "<script>alert('xss')</script>", + `long string, nothing to escape... `.repeat(9999), + `long utf16 string, no esc 🤔🤔🤔🤔🤔` + "tex".repeat(4000), + `smol`, + // `medium string with <script>alert('xss')</script>`, + FIXTURE, + // "[unicode]" + FIXTURE_WITH_UNICODE, ]) { group( { summary: true, - name: `"` + input.substring(0, Math.min(input.length, 32)) + `"`, + name: + `"` + + input.substring(0, Math.min(input.length, 32)) + + `"` + + ` (${input.length} chars)`, }, () => { - bench(`react's escapeHTML`, () => reactEscapeHtml(input)); - - bench(`bun's escapeHTML`, () => bunEscapeHTML(input)); + bench(`ReactDOM.escapeHTML`, () => reactEscapeHtml(input)); + bench(`html-entities.encode`, () => htmlEntityEncode(input)); + bench(`he.escape`, () => heEscape(input)); + bench(`Bun.escapeHTML`, () => bunEscapeHTML(input)); } ); } |