From 9f640ffb51dc216e78af6ea5fa0eb8bc782e446b Mon Sep 17 00:00:00 2001
From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Date: Fri, 3 Jun 2022 18:49:12 -0700
Subject: impl #1
---
bench/snippets/escapeHTML.js | 86 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)
create mode 100644 bench/snippets/escapeHTML.js
(limited to 'bench/snippets/escapeHTML.js')
diff --git a/bench/snippets/escapeHTML.js b/bench/snippets/escapeHTML.js
new file mode 100644
index 000000000..61bb74c7d
--- /dev/null
+++ b/bench/snippets/escapeHTML.js
@@ -0,0 +1,86 @@
+import { group } from "mitata";
+import { bench, run } from "mitata";
+
+var bunEscapeHTML = Bun.escapeHTML;
+
+const matchHtmlRegExp = /["'&<>]/;
+
+/**
+ * Escapes special characters and HTML entities in a given html string.
+ *
+ * @param {string} string HTML string to escape for later insertion
+ * @return {string}
+ * @public
+ */
+
+function reactEscapeHtml(string) {
+ const str = "" + string;
+ const match = matchHtmlRegExp.exec(str);
+
+ if (!match) {
+ return str;
+ }
+
+ let escape;
+ let html = "";
+ let index;
+ let lastIndex = 0;
+
+ for (index = match.index; index < str.length; index++) {
+ switch (str.charCodeAt(index)) {
+ case 34: // "
+ escape = """;
+ break;
+ case 38: // &
+ escape = "&";
+ break;
+ case 39: // '
+ escape = "'"; // modified from escape-html; used to be '''
+ break;
+ case 60: // <
+ escape = "<";
+ break;
+ case 62: // >
+ escape = ">";
+ break;
+ default:
+ continue;
+ }
+
+ if (lastIndex !== index) {
+ html += str.substring(lastIndex, index);
+ }
+
+ lastIndex = index + 1;
+ html += escape;
+ }
+
+ return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
+}
+
+const long = ("lalala" + "" + "lalala").repeat(9000);
+const short = "lalala" + "" + "lalala";
+const middle =
+ "lalala".repeat(2000) + "" + "lalala".repeat(2000);
+const nothing = "lalala".repeat(9999);
+group(`long (${long.length})`, () => {
+ bench("react's escapeHTML", () => reactEscapeHtml(long));
+ bench("bun's escapeHTML", () => bunEscapeHTML(long));
+});
+
+group(`short (${short.length})`, () => {
+ bench("react's escapeHTML", () => reactEscapeHtml(short));
+ bench("bun's escapeHTML", () => bunEscapeHTML(short));
+});
+
+group(`middle (${middle.length})`, () => {
+ bench("react's escapeHTML", () => reactEscapeHtml(middle));
+ bench("bun's escapeHTML", () => bunEscapeHTML(middle));
+});
+
+group(`nothing (${nothing.length})`, () => {
+ bench("react's escapeHTML", () => reactEscapeHtml(nothing));
+ bench("bun's escapeHTML", () => bunEscapeHTML(nothing));
+});
+
+await run();
--
cgit v1.2.3