summaryrefslogtreecommitdiff
path: root/cli/bench/deno_common.js
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-03-26 14:13:53 +0100
committerGitHub <noreply@github.com>2021-03-26 09:13:53 -0400
commit6c6f3e87c1a60bc96d006812a670212eeacd1257 (patch)
tree7a0d0ec3b50a7687fb01f374e07709e885f8e944 /cli/bench/deno_common.js
parent505db5da2c024983c35c4a01e4a6019a8a373599 (diff)
Add bench suite of common Deno functions (#9878)
Diffstat (limited to 'cli/bench/deno_common.js')
-rw-r--r--cli/bench/deno_common.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/cli/bench/deno_common.js b/cli/bench/deno_common.js
new file mode 100644
index 000000000..dc6e29f42
--- /dev/null
+++ b/cli/bench/deno_common.js
@@ -0,0 +1,53 @@
+// Run with: deno run -A ./cli/bench/deno_common.js
+function benchSync(name, n, innerLoop) {
+ const t1 = Date.now();
+ for (let i = 0; i < n; i++) {
+ innerLoop(i);
+ }
+ const t2 = Date.now();
+ const dt = (t2 - t1) / 1e3;
+ const r = n / dt;
+ console.log(
+ `${name}:${" ".repeat(20 - name.length)}\t` +
+ `n = ${n}, dt = ${dt.toFixed(3)}s, r = ${r.toFixed(0)}/s`,
+ );
+}
+
+function benchUrlParse() {
+ benchSync("url_parse", 5e4, (i) => {
+ new URL(`http://www.google.com/${i}`);
+ });
+}
+
+function benchNow() {
+ benchSync("now", 5e5, () => {
+ performance.now();
+ });
+}
+
+function benchWriteNull() {
+ // Not too large since we want to measure op-overhead not sys IO
+ const dataChunk = new Uint8Array(100);
+ const file = Deno.openSync("/dev/null", { write: true });
+ benchSync("write_null", 5e5, () => {
+ Deno.writeSync(file.rid, dataChunk);
+ });
+ Deno.close(file.rid);
+}
+
+function benchReadZero() {
+ const buf = new Uint8Array(100);
+ const file = Deno.openSync("/dev/zero");
+ benchSync("read_zero", 5e5, () => {
+ Deno.readSync(file.rid, buf);
+ });
+ Deno.close(file.rid);
+}
+
+function main() {
+ benchUrlParse();
+ benchNow();
+ benchWriteNull();
+ benchReadZero();
+}
+main();