From a9e4ff2029a769737982fcf7e0b7d76d35cf35e4 Mon Sep 17 00:00:00 2001 From: evan Date: Tue, 12 Jul 2022 04:40:12 -0400 Subject: cleanup benchmarks folder (#587) * cleanup benchmarks * run prettier --- bench/snippets/_fixture.txt | 1610 ++++++++++++++++++++++++++++ bench/snippets/buffer-read.js | 81 ++ bench/snippets/buffer.js | 136 +++ bench/snippets/callbacks-overhead.mjs | 78 ++ bench/snippets/concat.js | 66 ++ bench/snippets/escapeHTML.js | 122 +++ bench/snippets/exists.js | 6 + bench/snippets/jsx-entity-decoding.jsx | 29 + bench/snippets/noop.js | 17 + bench/snippets/performance-now-overhead.js | 17 + bench/snippets/react-dom-render.bun.js | 68 ++ bench/snippets/realpath.mjs | 4 + bench/snippets/render.js | 4 + bench/snippets/transpiler.mjs | 58 + 14 files changed, 2296 insertions(+) create mode 100644 bench/snippets/_fixture.txt create mode 100644 bench/snippets/buffer-read.js create mode 100644 bench/snippets/buffer.js create mode 100644 bench/snippets/callbacks-overhead.mjs create mode 100644 bench/snippets/concat.js create mode 100644 bench/snippets/escapeHTML.js create mode 100644 bench/snippets/exists.js create mode 100644 bench/snippets/jsx-entity-decoding.jsx create mode 100644 bench/snippets/noop.js create mode 100644 bench/snippets/performance-now-overhead.js create mode 100644 bench/snippets/react-dom-render.bun.js create mode 100644 bench/snippets/realpath.mjs create mode 100644 bench/snippets/render.js create mode 100644 bench/snippets/transpiler.mjs (limited to 'bench/snippets') diff --git a/bench/snippets/_fixture.txt b/bench/snippets/_fixture.txt new file mode 100644 index 000000000..d3c9bff5e --- /dev/null +++ b/bench/snippets/_fixture.txt @@ -0,0 +1,1610 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + v_escape/lib.rs at master · botika/v_escape · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + + +
+
+
+ + + + + + + + + + + + + + + +
+
+ + + + + + + Permalink + + + + + +
+ +
+
+ + + master + + + + +
+ + + +
+
+
+ +
+ + + + Go to file + + +
+ + + + + + + +
+ + + +
+ +
+
+
 
+
+ +
+
 
+ Cannot retrieve contributors at this time +
+
+ + + + + + +
+ +
+
+ + 168 lines (163 sloc) + + 10.7 KB +
+ +
+ +
+ Raw + Blame +
+ + +
+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
use v_htmlescape::HTMLEscape;
+
#[test]
fn test_escape() {
let empty = "";
let escapes = "<>&\"'/";
let escaped = "&lt;&gt;&amp;&quot;&#x27;&#x2f;";
let string_long: &str = &"foobar".repeat(1024);
+
// https://gitlab.com/r-iendo/v_escape/issues/2
let issue = "<".repeat(31);
assert_eq!(
HTMLEscape::from(issue.as_ref()).to_string(),
"&lt;".repeat(31)
);
+
assert_eq!(HTMLEscape::from(empty).to_string(), empty);
assert_eq!(HTMLEscape::from("").to_string(), "");
assert_eq!(HTMLEscape::from("<&>").to_string(), "&lt;&amp;&gt;");
assert_eq!(HTMLEscape::from("bar&").to_string(), "bar&amp;");
assert_eq!(HTMLEscape::from("<foo").to_string(), "&lt;foo");
assert_eq!(HTMLEscape::from("bar&h").to_string(), "bar&amp;h");
assert_eq!(
HTMLEscape::from("// my <html> is \"unsafe\" & should be 'escaped'").to_string(),
"&#x2f;&#x2f; my &lt;html&gt; is &quot;unsafe&quot; &amp; \
should be &#x27;escaped&#x27;"
);
assert_eq!(
HTMLEscape::from("<".repeat(16).as_ref()).to_string(),
"&lt;".repeat(16)
);
assert_eq!(
HTMLEscape::from("<".repeat(32).as_ref()).to_string(),
"&lt;".repeat(32)
);
assert_eq!(
HTMLEscape::from("<".repeat(64).as_ref()).to_string(),
"&lt;".repeat(64)
);
assert_eq!(
HTMLEscape::from("<".repeat(128).as_ref()).to_string(),
"&lt;".repeat(128)
);
assert_eq!(
HTMLEscape::from("<".repeat(1024).as_ref()).to_string(),
"&lt;".repeat(1024)
);
assert_eq!(
HTMLEscape::from("<".repeat(129).as_ref()).to_string(),
"&lt;".repeat(129)
);
assert_eq!(
HTMLEscape::from("<".repeat(128 * 2 - 1).as_ref()).to_string(),
"&lt;".repeat(128 * 2 - 1)
);
assert_eq!(
HTMLEscape::from("<".repeat(128 * 8 - 1).as_ref()).to_string(),
"&lt;".repeat(128 * 8 - 1)
);
assert_eq!(HTMLEscape::from(string_long).to_string(), string_long);
assert_eq!(
HTMLEscape::from([string_long, "<"].join("").as_ref()).to_string(),
[string_long, "&lt;"].join("")
);
assert_eq!(
HTMLEscape::from(["<", string_long].join("").as_ref()).to_string(),
["&lt;", string_long].join("")
);
assert_eq!(
HTMLEscape::from(escapes.repeat(1024).as_ref()).to_string(),
escaped.repeat(1024)
);
assert_eq!(
HTMLEscape::from(
[string_long, &escapes.repeat(13)]
.join("")
.repeat(1024)
.as_ref()
)
.to_string(),
[string_long, &escaped.repeat(13)].join("").repeat(1024)
);
assert_eq!(
HTMLEscape::from([string_long, "<", string_long].join("").as_ref()).to_string(),
[string_long, "&lt;", string_long].join("")
);
assert_eq!(
HTMLEscape::from(
[string_long, "<", string_long, escapes, string_long,]
.join("")
.as_ref()
)
.to_string(),
[string_long, "&lt;", string_long, escaped, string_long,].join("")
);
+
let string_long = r#"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat tellus sit
amet ornare fermentum. Etiam nec erat ante. In at metus a orci mollis scelerisque.
Sed eget ultrices turpis, at sollicitudin erat. Integer hendrerit nec magna quis
venenatis. Vivamus non dolor hendrerit, vulputate velit sed, varius nunc. Quisque
in pharetra mi. Sed ullamcorper nibh malesuada commodo porttitor. Ut scelerisque
sodales felis quis dignissim. Morbi aliquam finibus justo, sit amet consectetur
mauris efficitur sit amet. Donec posuere turpis felis, eu lacinia magna accumsan
quis. Fusce egestas lacus vel fermentum tincidunt. Phasellus a nulla eget lectus
placerat commodo at eget nisl. Fusce cursus dui quis purus accumsan auctor.
Donec iaculis felis quis metus consectetur porttitor.
<p>
Etiam nibh mi, <b>accumsan</b> quis purus sed, posuere fermentum lorem. In pulvinar porta
maximus. Fusce tincidunt lacinia tellus sit amet tincidunt. Aliquam lacus est, pulvinar
non metus a, <b>facilisis</b> ultrices quam. Nulla feugiat leo in cursus eleifend. Suspendisse
eget nisi ac justo sagittis interdum id a ipsum. Nulla mauris justo, scelerisque ac
rutrum vitae, consequat vel ex.
</p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p>
<p>
Sed sollicitudin <b>sem</b> mauris, at rutrum nibh egestas vel. Ut eu nisi tellus. Praesent dignissim
orci elementum, mattis turpis eget, maximus ante. Suspendisse luctus eu felis a tempor. Morbi
ac risus vitae sem molestie ullamcorper. Curabitur ligula augue, sollicitudin quis maximus vel,
facilisis sed nibh. Aenean auctor magna sem, id rutrum metus convallis quis. Nullam non arcu
dictum, lobortis erat quis, rhoncus est. Suspendisse venenatis, mi sed venenatis vehicula,
tortor dolor egestas lectus, et efficitur turpis odio non augue. Integer velit sapien, dictum
non egestas vitae, hendrerit sed quam. Phasellus a nunc eu erat varius imperdiet. Etiam id
sollicitudin turpis, vitae molestie orci. Quisque ornare magna quis metus rhoncus commodo.
Phasellus non mauris velit.
</p>
<p>
Etiam dictum tellus ipsum, nec varius quam ornare vel. Cras vehicula diam nec sollicitudin
ultricies. Pellentesque rhoncus sagittis nisl id facilisis. Nunc viverra convallis risus ut
luctus. Aliquam vestibulum <b>efficitur massa</b>, id tempus nisi posuere a. Aliquam scelerisque
elit justo. Nullam a ante felis. Cras vitae lorem eu nisi feugiat hendrerit. Maecenas vitae
suscipit leo, lacinia dignissim lacus. Sed eget volutpat mi. In eu bibendum neque. Pellentesque
finibus velit a fermentum rhoncus. Maecenas leo purus, eleifend eu lacus a, condimentum sagittis
justo.
</p>"#;
let string_long_escaped = "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris consequat tellus sit\n amet ornare fermentum. Etiam nec erat ante. In at metus a orci mollis scelerisque.\n Sed eget ultrices turpis, at sollicitudin erat. Integer hendrerit nec magna quis\n venenatis. Vivamus non dolor hendrerit, vulputate velit sed, varius nunc. Quisque\n in pharetra mi. Sed ullamcorper nibh malesuada commodo porttitor. Ut scelerisque\n sodales felis quis dignissim. Morbi aliquam finibus justo, sit amet consectetur\n mauris efficitur sit amet. Donec posuere turpis felis, eu lacinia magna accumsan\n quis. Fusce egestas lacus vel fermentum tincidunt. Phasellus a nulla eget lectus\n placerat commodo at eget nisl. Fusce cursus dui quis purus accumsan auctor.\n Donec iaculis felis quis metus consectetur porttitor.\n&lt;p&gt;\n Etiam nibh mi, &lt;b&gt;accumsan&lt;&#x2f;b&gt; quis purus sed, posuere fermentum lorem. In pulvinar porta\n maximus. Fusce tincidunt lacinia tellus sit amet tincidunt. Aliquam lacus est, pulvinar\n non metus a, &lt;b&gt;facilisis&lt;&#x2f;b&gt; ultrices quam. Nulla feugiat leo in cursus eleifend. Suspendisse\n eget nisi ac justo sagittis interdum id a ipsum. Nulla mauris justo, scelerisque ac\n rutrum vitae, consequat vel ex.\n&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;&lt;&#x2f;p&gt;\n&lt;p&gt;\n Sed sollicitudin &lt;b&gt;sem&lt;&#x2f;b&gt; mauris, at rutrum nibh egestas vel. Ut eu nisi tellus. Praesent dignissim\n orci elementum, mattis turpis eget, maximus ante. Suspendisse luctus eu felis a tempor. Morbi\n ac risus vitae sem molestie ullamcorper. Curabitur ligula augue, sollicitudin quis maximus vel,\n facilisis sed nibh. Aenean auctor magna sem, id rutrum metus convallis quis. Nullam non arcu\n dictum, lobortis erat quis, rhoncus est. Suspendisse venenatis, mi sed venenatis vehicula,\n tortor dolor egestas lectus, et efficitur turpis odio non augue. Integer velit sapien, dictum\n non egestas vitae, hendrerit sed quam. Phasellus a nunc eu erat varius imperdiet. Etiam id\n sollicitudin turpis, vitae molestie orci. Quisque ornare magna quis metus rhoncus commodo.\n Phasellus non mauris velit.\n&lt;&#x2f;p&gt;\n&lt;p&gt;\n Etiam dictum tellus ipsum, nec varius quam ornare vel. Cras vehicula diam nec sollicitudin\n ultricies. Pellentesque rhoncus sagittis nisl id facilisis. Nunc viverra convallis risus ut\n luctus. Aliquam vestibulum &lt;b&gt;efficitur massa&lt;&#x2f;b&gt;, id tempus nisi posuere a. Aliquam scelerisque\n elit justo. Nullam a ante felis. Cras vitae lorem eu nisi feugiat hendrerit. Maecenas vitae\n suscipit leo, lacinia dignissim lacus. Sed eget volutpat mi. In eu bibendum neque. Pellentesque\n finibus velit a fermentum rhoncus. Maecenas leo purus, eleifend eu lacus a, condimentum sagittis\n justo.\n&lt;&#x2f;p&gt;";
let string_short = "Lorem ipsum dolor sit amet,<foo>bar&foo\"bar\\foo/bar";
let string_short_escaped =
"Lorem ipsum dolor sit amet,&lt;foo&gt;bar&amp;foo&quot;bar\\foo&#x2f;bar";
let no_escape = "Lorem ipsum dolor sit amet,";
let no_escape_long = r#"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin scelerisque eu urna in aliquet.
Phasellus ac nulla a urna sagittis consequat id quis est. Nullam eu ex eget erat accumsan dictum
ac lobortis urna. Etiam fermentum ut quam at dignissim. Curabitur vestibulum luctus tellus, sit
amet lobortis augue tempor faucibus. Nullam sed felis eget odio elementum euismod in sit amet massa.
Vestibulum sagittis purus sit amet eros auctor, sit amet pharetra purus dapibus. Donec ornare metus
vel dictum porta. Etiam ut nisl nisi. Nullam rutrum porttitor mi. Donec aliquam ac ipsum eget
hendrerit. Cras faucibus, eros ut pharetra imperdiet, est tellus aliquet felis, eget convallis
lacus ipsum eget quam. Vivamus orci lorem, maximus ac mi eget, bibendum vulputate massa. In
vestibulum dui hendrerit, vestibulum lacus sit amet, posuere erat. Vivamus euismod massa diam,
vulputate euismod lectus vestibulum nec. Donec sit amet massa magna. Nunc ipsum nulla, euismod
quis lacus at, gravida maximus elit. Duis tristique, nisl nullam.
"#;
+
assert_eq!(HTMLEscape::from(no_escape).to_string(), no_escape);
assert_eq!(HTMLEscape::from(no_escape_long).to_string(), no_escape_long);
assert_eq!(
HTMLEscape::from(string_short).to_string(),
string_short_escaped
);
assert_eq!(
HTMLEscape::from(string_short.repeat(1024).as_ref()).to_string(),
string_short_escaped.repeat(1024)
);
assert_eq!(
HTMLEscape::from(string_long).to_string(),
string_long_escaped
);
}
+ + + +
+ +
+ + + + +
+ + +
+ + +
+
+ + + + +
+
+ +
+
+ +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bench/snippets/buffer-read.js b/bench/snippets/buffer-read.js new file mode 100644 index 000000000..5da645153 --- /dev/null +++ b/bench/snippets/buffer-read.js @@ -0,0 +1,81 @@ +// import { Buffer } from "buffer"; +var buf = new Buffer(1024); +// var buf = new Uint8Array(1024); +var view = new DataView(buf.buffer); +var INTERVAL = 9999999; +var time = (name, fn) => { + console.log(name, fn()); + // for (let i = 0; i < INTERVAL; i++) fn(); + + // console.time(name.padEnd("DataView.readBigUInt64 (LE)".length)); + // for (let i = 0; i < INTERVAL; i++) fn(); + // console.timeEnd(name.padEnd("DataView.readBigUInt64 (LE)".length)); + // }; + + // console.log( + // `Run ${new Intl.NumberFormat().format(INTERVAL)} times with a warmup:`, + // "\n" +}; +var array = new Uint8Array(1024); +new Uint8Array(buf.buffer).fill(255); +time("Buffer[] ", () => buf[0]); +time("Uint8Array[]", () => array[0]); +console.log(""); + +time("Buffer.getBigInt64BE ", () => buf.readBigInt64BE(0)); +time("DataView.getBigInt64 (BE)", () => + view.getBigInt64(0, false).toString(10) +); +console.log(""); + +time("Buffer.readBigInt64LE ", () => buf.readBigInt64LE(0)); +time("DataView.readBigInt64 (LE)", () => view.getBigInt64(0, true)); +console.log(""); +time("Buffer.getBigUInt64BE ", () => buf.readBigUInt64BE(0)); +time("DataView.getBigUInt64 (BE)", () => view.getBigUint64(0, false)); +console.log(""); +time("Buffer.readBigUInt64LE ", () => buf.readBigUInt64LE(0)); +time("DataView.readBigUInt64 (LE)", () => view.getBigUint64(0, true)); +console.log(""); +time("Buffer.getDoubleBE ", () => buf.readDoubleBE(0)); +time("DataView.getDouble (BE)", () => view.getFloat64(0, false)); +console.log(""); +time("Buffer.readDoubleLE ", () => buf.readDoubleLE(0)); +time("DataView.readDouble (LE)", () => view.getFloat64(0, true)); +console.log(""); +time("Buffer.getFloatBE ", () => buf.readFloatBE(0)); +time("DataView.getFloat (BE)", () => view.getFloat32(0, false)); +console.log(""); +time("Buffer.readFloatLE ", () => buf.readFloatLE(0)); +time("DataView.readFloat (LE)", () => view.getFloat32(0, true)); +console.log(""); +time("Buffer.getInt16BE ", () => buf.readInt16BE(0)); +time("DataView.getInt16 (BE)", () => view.getInt16(0, false)); +console.log(""); +time("Buffer.readInt16LE ", () => buf.readInt16LE(0)); +time("DataView.readInt16 (LE)", () => view.getInt16(0, true)); +console.log(""); +time("Buffer.getInt32BE ", () => buf.readInt32BE(0)); +time("DataView.getInt32 (BE)", () => view.getInt32(0, false)); +console.log(""); +time("Buffer.readInt32LE ", () => buf.readInt32LE(0)); +time("DataView.readInt32 (LE)", () => view.getInt32(0, true)); +console.log(""); +time("Buffer.readInt8 ", () => buf.readInt8(0)); +time("DataView.readInt (t8)", () => view.getInt8(0)); +console.log(""); +time("Buffer.getUInt16BE ", () => buf.readUInt16BE(0)); +time("DataView.getUInt16 (BE)", () => view.getUint16(0, false)); +console.log(""); +time("Buffer.readUInt16LE ", () => buf.readUInt16LE(0)); +time("DataView.readUInt16 (LE)", () => view.getUint16(0, true)); +console.log(""); +time("Buffer.getUInt32BE ", () => buf.readUInt32BE(0)); +time("DataView.getUInt32 (BE)", () => view.getUint32(0, false)); +console.log(""); +time("Buffer.readUInt32LE ", () => buf.readUInt32LE(0)); +time("DataView.getUInt32 (LE)", () => view.getUint32(0, true)); +console.log(""); +time("Buffer.readUInt8 ", () => buf.readUInt8(0)); +time("DataView.getUInt (t8)", () => view.getUint8(0)); +console.log(""); diff --git a/bench/snippets/buffer.js b/bench/snippets/buffer.js new file mode 100644 index 000000000..9ababbf35 --- /dev/null +++ b/bench/snippets/buffer.js @@ -0,0 +1,136 @@ +// import { Buffer } from "buffer"; +var buf = new Buffer(1024); +var view = new DataView(buf.buffer); +var INTERVAL = 9999999; +var time = (name, fn) => { + for (let i = 0; i < INTERVAL; i++) fn(); + + console.time(name.padEnd('Buffer.write(string, "latin1")'.length)); + for (let i = 0; i < INTERVAL; i++) fn(); + console.timeEnd(name.padEnd('Buffer.write(string, "latin1")'.length)); +}; + +console.log( + `Run ${new Intl.NumberFormat().format(INTERVAL)} times with a warmup:`, + "\n" +); + +const stringToWrite = "hellooooohellooooo"; + +time('Buffer.write(string, "utf8")', () => buf.write(stringToWrite, "utf8")); +time('Buffer.write(string, "ascii")', () => buf.write(stringToWrite, "ascii")); +time('Buffer.write(string, "latin1")', () => + buf.write(stringToWrite, "latin1") +); +time("Buffer.readBigInt64BE ", () => buf.readBigInt64BE(0)); +// time("DataView.getBigInt64 (BE)", () => view.getBigInt64(0, false)); +// console.log(""); +time("Buffer.readBigInt64LE ", () => buf.readBigInt64LE(0)); +// time("DataView.readBigInt64 (LE)", () => view.getBigInt64(0, true)); +// console.log(""); +time("Buffer.readBigUInt64BE ", () => buf.readBigUInt64BE(0)); +// time("DataView.getBigUInt64 (BE)", () => view.getBigUint64(0, false)); +// console.log(""); +time("Buffer.readBigUInt64LE ", () => buf.readBigUInt64LE(0)); +// time("DataView.readBigUInt64 (LE)", () => view.getBigUint64(0, true)); +// console.log(""); +time("Buffer.readDoubleBE ", () => buf.readDoubleBE(0)); +// time("DataView.getDouble (BE)", () => view.getFloat64(0, false)); +// console.log(""); +time("Buffer.readDoubleLE ", () => buf.readDoubleLE(0)); +// time("DataView.readDouble (LE)", () => view.getFloat64(0, true)); +// console.log(""); +time("Buffer.readFloatBE ", () => buf.readFloatBE(0)); +// time("DataView.getFloat (BE)", () => view.getFloat32(0, false)); +// console.log(""); +time("Buffer.readFloatLE ", () => buf.readFloatLE(0)); +// time("DataView.readFloat (LE)", () => view.getFloat32(0, true)); +// console.log(""); +time("Buffer.readInt16BE ", () => buf.readInt16BE(0)); +// time("DataView.getInt16 (BE)", () => view.getInt16(0, false)); +// console.log(""); +time("Buffer.readInt16LE ", () => buf.readInt16LE(0)); +// time("DataView.readInt16 (LE)", () => view.getInt16(0, true)); +// console.log(""); +time("Buffer.readInt32BE ", () => buf.readInt32BE(0)); +// time("DataView.getInt32 (BE)", () => view.getInt32(0, false)); +// console.log(""); +time("Buffer.readInt32LE ", () => buf.readInt32LE(0)); +// time("DataView.readInt32 (LE)", () => view.getInt32(0, true)); +// console.log(""); +time("Buffer.readInt8 ", () => buf.readInt8(0)); +// time("DataView.readInt (t8)", () => view.getInt8(0)); +// console.log(""); +time("Buffer.readUInt16BE ", () => buf.readUInt16BE(0)); +// time("DataView.getUInt16 (BE)", () => view.getUint16(0, false)); +// console.log(""); +time("Buffer.readUInt16LE ", () => buf.readUInt16LE(0)); +// time("DataView.readUInt16 (LE)", () => view.getUint16(0, true)); +// console.log(""); +time("Buffer.readUInt32BE ", () => buf.readUInt32BE(0)); +// time("DataView.getUInt32 (BE)", () => view.getUint32(0, false)); +// console.log(""); +time("Buffer.readUInt32LE ", () => buf.readUInt32LE(0)); +// time("DataView.getUInt32 (LE)", () => view.getUint32(0, true)); +// console.log(""); +time("Buffer.readUInt8 ", () => buf.readUInt8(0)); +// time("DataView.getUInt (t8)", () => view.getUint8(0)); +// console.log(""); + +time("Buffer.writeBigInt64BE", () => buf.writeBigInt64BE(BigInt(0), 0)); +// time("DataView.getBigInt64 (BE)", () => view.getBigInt64(0, false)); +// console.log(""); +time("Buffer.writeBigInt64LE", () => buf.writeBigInt64LE(BigInt(0), 0)); +// time("DataView.readBigInt64 (LE)", () => view.getBigInt64(0, true)); +// console.log(""); +time("Buffer.writeBigUInt64BE", () => buf.writeBigUInt64BE(BigInt(0), 0)); +// time("DataView.getBigUInt64 (BE)", () => view.getBigUint64(0, false)); +// console.log(""); +time("Buffer.writeBigUInt64LE", () => buf.writeBigUInt64LE(BigInt(0), 0)); +// time("DataView.readBigUInt64 (LE)", () => view.getBigUint64(0, true)); +// console.log(""); +time("Buffer.writeDoubleBE ", () => buf.writeDoubleBE(0, 0)); +// time("DataView.getDouble (BE)", () => view.getFloat64(0, false)); +// console.log(""); +time("Buffer.writeDoubleLE ", () => buf.writeDoubleLE(0, 0)); +// time("DataView.readDouble (LE)", () => view.getFloat64(0, true)); +// console.log(""); +time("Buffer.writeFloatBE ", () => buf.writeFloatBE(0, 0)); +// time("DataView.getFloat (BE)", () => view.getFloat32(0, false)); +// console.log(""); +time("Buffer.writeFloatLE ", () => buf.writeFloatLE(0, 0)); +// time("DataView.readFloat (LE)", () => view.getFloat32(0, true)); +// console.log(""); +time("Buffer.writeInt16BE ", () => buf.writeInt16BE(0, 0)); +// time("DataView.getInt16 (BE)", () => view.getInt16(0, false)); +// console.log(""); +time("Buffer.writeInt16LE ", () => buf.writeInt16LE(0, 0)); +// time("DataView.readInt16 (LE)", () => view.getInt16(0, true)); +// console.log(""); +time("Buffer.writeInt32BE ", () => buf.writeInt32BE(0, 0)); +// time("DataView.getInt32 (BE)", () => view.getInt32(0, false)); +// console.log(""); +time("Buffer.writeInt32LE ", () => buf.writeInt32LE(0, 0)); +// time("DataView.readInt32 (LE)", () => view.getInt32(0, true)); +// console.log(""); +time("Buffer.writeInt8 ", () => buf.writeInt8(0, 0)); +// time("DataView.readInt (t8)", () => view.getInt8(0)); +// console.log(""); +time("Buffer.writeUInt16BE ", () => buf.writeUInt16BE(0, 0)); +// time("DataView.getUInt16 (BE)", () => view.getUint16(0, false)); +// console.log(""); +time("Buffer.writeUInt16LE ", () => buf.writeUInt16LE(0, 0)); +// time("DataView.readUInt16 (LE)", () => view.getUint16(0, true)); +// console.log(""); +time("Buffer.writeUInt32BE ", () => buf.writeUInt32BE(0, 0)); +// time("DataView.getUInt32 (BE)", () => view.getUint32(0, false)); +// console.log(""); +time("Buffer.writeUInt32LE ", () => buf.writeUInt32LE(0, 0)); +// time("DataView.getUInt32 (LE)", () => view.getUint32(0, true)); +// console.log(""); +time("Buffer.writeUInt8 ", () => buf.writeUInt8(0, 0)); +// time("DataView.getUInt (t8)", () => view.getUint8(0)); +// console.log(""); + +buf.writeUInt8(10, 10); +console.assert(buf.readUInt8(10, 10) === 10); diff --git a/bench/snippets/callbacks-overhead.mjs b/bench/snippets/callbacks-overhead.mjs new file mode 100644 index 000000000..65171145c --- /dev/null +++ b/bench/snippets/callbacks-overhead.mjs @@ -0,0 +1,78 @@ +const iterations = 1_000; + +export var report = { + async: 0, + callback: 0, + sync: 0, + then: 0, +}; + +const tests = { + callback(n, cb) { + if (n === iterations) return cb(); + tests.callback(1 + n, () => cb()); + }, + + sync(n) { + if (n === iterations) return; + + tests.sync(1 + n); + }, + + async async(n) { + if (n === iterations) return; + + await tests.async(1 + n); + }, + + then(n) { + if (n === iterations) return; + return Promise.resolve(1 + n).then(tests.then); + }, +}; + +async function test(log) { + { + const a = performance.now(); + await tests.async(0); + if (log) + console.log( + `async/await: ${(report.async = (performance.now() - a).toFixed(4))}ms` + ); + } + + { + const a = performance.now(); + tests.callback(0, function () { + if (log) + console.log( + `callback: ${(report.callback = (performance.now() - a).toFixed( + 4 + ))}ms` + ); + }); + } + + { + const a = performance.now(); + await tests.then(0); + if (log) + console.log( + `then: ${(report.then = (performance.now() - a).toFixed(4))}ms` + ); + } + + { + const a = performance.now(); + tests.sync(0); + if (log) + console.log( + `sync: ${(report.sync = (performance.now() - a).toFixed(4))}ms` + ); + } +} + +let warmup = 10; +while (warmup--) await test(); + +await test(true); diff --git a/bench/snippets/concat.js b/bench/snippets/concat.js new file mode 100644 index 000000000..46c6e0f7d --- /dev/null +++ b/bench/snippets/concat.js @@ -0,0 +1,66 @@ +import { bench, group, run } from "mitata"; +import { readFileSync } from "fs"; +import { allocUnsafe } from "bun"; + +function polyfill(chunks) { + var size = 0; + for (const chunk of chunks) { + size += chunk.byteLength; + } + var buffer = new ArrayBuffer(size); + var view = new Uint8Array(buffer); + var offset = 0; + for (const chunk of chunks) { + view.set(chunk, offset); + offset += chunk.byteLength; + } + return buffer; +} + +function polyfillUninitialized(chunks) { + var size = 0; + for (const chunk of chunks) { + size += chunk.byteLength; + } + var view = allocUnsafe(size); + + var offset = 0; + for (const chunk of chunks) { + view.set(chunk, offset); + offset += chunk.byteLength; + } + return view.buffer; +} + +const chunkGroups = [ + [Uint8Array.from([123]), Uint8Array.from([456]), Uint8Array.from([789])], + Array.from(readFileSync(import.meta.path)).map((a) => Uint8Array.from([a])), + [readFileSync(import.meta.path)], + Array.from({ length: 42 }, () => readFileSync(import.meta.path)), + Array.from({ length: 2 }, () => + new TextEncoder().encode(readFileSync(import.meta.path, "utf8").repeat(100)) + ), +]; + +for (const chunks of chunkGroups) { + group( + `${chunks.reduce( + (prev, curr, i, a) => prev + curr.byteLength, + 0 + )} bytes for ${chunks.length} chunks`, + () => { + bench("Bun.concatArrayBuffers", () => { + Bun.concatArrayBuffers(chunks); + }); + bench("Uint8Array.set", () => { + polyfill(chunks); + }); + + bench("Uint8Array.set (uninitialized memory)", () => { + polyfillUninitialized(chunks); + }); + } + ); +} + +await run(); diff --git a/bench/snippets/escapeHTML.js b/bench/snippets/escapeHTML.js new file mode 100644 index 000000000..f186b1d2f --- /dev/null +++ b/bench/snippets/escapeHTML.js @@ -0,0 +1,122 @@ +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; + +const FIXTURE = require("fs") + .readFileSync(import.meta.dir + "/_fixture.txt", "utf8") + .split("") + .map((a) => { + if (a.charCodeAt(0) > 127) { + return "a"; + } + return a; + }) + .join(""); + +const FIXTURE_WITH_UNICODE = require("fs").readFileSync( + import.meta.dir + "/_fixture.txt", + "utf8" +); + +// from react-dom: +const matchHtmlRegExp = /["'&<>]/; + +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; +} + +// for (let input of [ +// "", +// `long string, nothing to escape... `.repeat(9999), +// `long utf16 string, no esc 🤔🤔🤔🤔🤔` + "tex".repeat(4000), +// `smol`, +// // `medium string with `, + +// FIXTURE, +// // "[unicode]" + FIXTURE_WITH_UNICODE, +// ]) { +// group( +// { +// summary: true, +// name: +// `"` + +// input.substring(0, Math.min(input.length, 32)) + +// `"` + +// ` (${input.length} chars)`, +// }, +// () => { +// bench(`ReactDOM.escapeHTML`, () => reactEscapeHtml(input)); +// bench(`html-entities.encode`, () => htmlEntityEncode(input)); +// bench(`he.escape`, () => heEscape(input)); +// bench(`Bun.escapeHTML`, () => bunEscapeHTML(input)); +// } +// ); +// } + +for (let input of [ + `long string, nothing to escape... `.repeat(9999999 * 3), + FIXTURE.repeat(8000), + // "[unicode]" + FIXTURE_WITH_UNICODE, +]) { + group( + { + summary: true, + name: + `"` + + input.substring(0, Math.min(input.length, 32)) + + `"` + + ` (${new Intl.NumberFormat().format(input.length / 100_000_000_0)} GB)`, + }, + () => { + // bench(`ReactDOM.escapeHTML`, () => reactEscapeHtml(input)); + // bench(`html-entities.encode`, () => htmlEntityEncode(input)); + // bench(`he.escape`, () => heEscape(input)); + bench(`Bun.escapeHTML`, () => bunEscapeHTML(input)); + } + ); +} +await run(); diff --git a/bench/snippets/exists.js b/bench/snippets/exists.js new file mode 100644 index 000000000..1d1d6cfe0 --- /dev/null +++ b/bench/snippets/exists.js @@ -0,0 +1,6 @@ +const { existsSync } = require("fs"); +const cwd = process.cwd(); + +const count = parseInt(process.env.ITERATIONS || "1", 10) || 1; + +for (let i = 0; i < count; i++) existsSync(cwd); diff --git a/bench/snippets/jsx-entity-decoding.jsx b/bench/snippets/jsx-entity-decoding.jsx new file mode 100644 index 000000000..29834d781 --- /dev/null +++ b/bench/snippets/jsx-entity-decoding.jsx @@ -0,0 +1,29 @@ +import React from "react"; + +export default ( + <> + ‍‍‍‍‍‍&zwjϑ≅≅≅≅≅≅≅≅ + üüüüüüü£åååååååå + óóóóóóóΠμμμμμμμμ + ♣♣♣♣♣♣&clubs⊕¢¢¢¢¢¢¢¢ + ÏÏÏÏÏÏÏ‌þþþþþþþþ + ………………&hellip°<<<<<<<< + λλλλλλ&lambdaρ⇓⇓⇓⇓⇓⇓⇓⇓ + ⇔⇔⇔⇔⇔⇔&hArrΤ≠≠≠≠≠≠≠≠ + ûûûûûûû•⊥⊥⊥⊥⊥⊥⊥⊥ + ôôôôôôô¶óóóóóóóó + ⊂⊂⊂⊂⊂⊂&subáíííííííí + ¸¸¸¸¸¸¸«§§§§§§§§ + ¨¨¨¨¨¨¨ς"""""""" + ÔÔÔÔÔÔÔ∫¥¥¥¥¥¥¥¥ + ςςςςςς&sigmafΕ«««««««« + ♦♦♦♦♦♦&diamsÓªªªªªªªª + ∞∞∞∞∞∞&infin⇔⋅⋅⋅⋅⋅⋅⋅⋅ + òòòòòòòý×××××××× + ⌈⌈⌈⌈⌈⌈&lceilωßßßßßßßß + ≥≥≥≥≥≥&geßüüüüüüüü + ρρρρρρ&rhoš∝∝∝∝∝∝∝∝ + ◊◊◊◊◊◊&lozχ↵↵↵↵↵↵↵↵ + ÈÈÈÈÈÈÈ ÅÅÅÅÅÅÅÅ + +); diff --git a/bench/snippets/noop.js b/bench/snippets/noop.js new file mode 100644 index 000000000..789e6abbb --- /dev/null +++ b/bench/snippets/noop.js @@ -0,0 +1,17 @@ +import { bench, run } from "mitata"; + +var noop = globalThis[Symbol.for("Bun.lazy")]("noop"); + +bench("function", function () { + noop.function(); +}); + +bench("setter", function () { + noop.getterSetter = 1; +}); + +bench("getter", function () { + noop.getterSetter; +}); + +run(); diff --git a/bench/snippets/performance-now-overhead.js b/bench/snippets/performance-now-overhead.js new file mode 100644 index 000000000..12115f203 --- /dev/null +++ b/bench/snippets/performance-now-overhead.js @@ -0,0 +1,17 @@ +import { group } from "mitata"; +import { bench, run } from "mitata"; +bench("performance.now x 1000", () => { + for (let i = 0; i < 1000; i++) { + performance.now(); + } +}); + +if ("Bun" in globalThis) { + var nanoseconds = Bun.nanoseconds; + bench("Bun.nanoseconds x 1000", () => { + for (let i = 0; i < 1000; i++) { + nanoseconds(); + } + }); +} +await run(); diff --git a/bench/snippets/react-dom-render.bun.js b/bench/snippets/react-dom-render.bun.js new file mode 100644 index 000000000..c3d1b3505 --- /dev/null +++ b/bench/snippets/react-dom-render.bun.js @@ -0,0 +1,68 @@ +import { bench, group, run } from "mitata"; +import { renderToReadableStream } from "react-dom/cjs/react-dom-server.browser.production.min"; +import { renderToReadableStream as renderToReadableStreamBun } from "../test/bun.js/reactdom-bun"; + +const App = () => ( +
+

Hello, world!

+

+ This is a React component This is a React component This is a React + component This is a React component. +

+

+ This is a React component This is a React component This is a React + component This is a React component. +

+

+ This is a React component This is a React component This is a React + component This is a React component. +

+

+ This is a React component This is a React component This is a React + component This is a React component. +

+

+ This is a React component This is a React component This is a React + component This is a React component. +

+
+); + +group("new Response(stream).text()", () => { + bench( + "react-dom/server.browser", + async () => await new Response(await renderToReadableStream()).text() + ); + bench( + "react-dom/server.bun", + async () => + await new Response(await renderToReadableStreamBun()).text() + ); +}); + +group("new Response(stream).arrayBuffer()", () => { + bench( + "react-dom/server.browser", + async () => + await new Response(await renderToReadableStream()).arrayBuffer() + ); + bench( + "react-dom/server.bun", + async () => + await new Response(await renderToReadableStreamBun()).arrayBuffer() + ); +}); + +group("new Response(stream).blob()", () => { + bench( + "react-dom/server.browser", + async () => await new Response(await renderToReadableStream()).blob() + ); + bench( + "react-dom/server.bun", + async () => + await new Response(await renderToReadableStreamBun()).blob() + ); +}); + +await run(); diff --git a/bench/snippets/realpath.mjs b/bench/snippets/realpath.mjs new file mode 100644 index 000000000..9c3793d80 --- /dev/null +++ b/bench/snippets/realpath.mjs @@ -0,0 +1,4 @@ +import { realpathSync } from "node:fs"; +const count = parseInt(process.env.ITERATIONS || "1", 10) || 1; +const arg = process.argv[process.argv.length - 1]; +for (let i = 0; i < count; i++) realpathSync(arg); diff --git a/bench/snippets/render.js b/bench/snippets/render.js new file mode 100644 index 000000000..9ef70bc27 --- /dev/null +++ b/bench/snippets/render.js @@ -0,0 +1,4 @@ +import decoding from "./jsx-entity-decoding"; +import ReactDOMServer from "react-dom/server.browser"; + +console.log(ReactDOMServer.renderToString(decoding)); diff --git a/bench/snippets/transpiler.mjs b/bench/snippets/transpiler.mjs new file mode 100644 index 000000000..6872d88b7 --- /dev/null +++ b/bench/snippets/transpiler.mjs @@ -0,0 +1,58 @@ +import { readFileSync } from "fs"; + +var transformSync; +var transform; +var opts; +if (process.isBun) { + const transpiler = new Bun.Transpiler({ loader: "jsx" }); + transformSync = transpiler.transformSync.bind(transpiler); + transform = transpiler.transform.bind(transpiler); + opts = "jsx"; +} else if (process.env["esbuild"]) { + try { + const esbuild = await import("esbuild"); + transformSync = esbuild.transformSync; + transform = esbuild.transform; + opts = { loader: "jsx" }; + } catch (exception) { + throw exception; + } +} else if (process.env["swc"]) { + try { + const swc = await import("@swc/core"); + transformSync = swc.transformSync; + transform = swc.transform; + opts = { + sourceMaps: false, + inlineSourcesContent: false, + jsc: { + target: "es2022", + parser: { + jsx: true, + }, + }, + }; + } catch (exception) { + throw exception; + } +} else if (process.env["babel"]) { + try { + const swc = await import("@babel/core"); + transformSync = swc.transformSync; + transform = swc.transform; + opts = { + sourceMaps: false, + presets: [(await import("@babel/preset-react")).default], + }; + } catch (exception) { + throw exception; + } +} + +const code = readFileSync("src/test/fixtures/simple.jsx", "utf8"); + +if (process.env.ASYNC) { + console.log(await transform(code, opts)); +} else { + console.log(transformSync(code, opts)); +} -- cgit v1.2.3