aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets
diff options
context:
space:
mode:
Diffstat (limited to 'bench/snippets')
-rw-r--r--bench/snippets/base64-buffer-to-string.mjs14
-rw-r--r--bench/snippets/crypto.mjs12
-rw-r--r--bench/snippets/process-info.mjs33
-rw-r--r--bench/snippets/readfile-not-found.mjs17
-rw-r--r--bench/snippets/util-deprecate.mjs60
5 files changed, 129 insertions, 7 deletions
diff --git a/bench/snippets/base64-buffer-to-string.mjs b/bench/snippets/base64-buffer-to-string.mjs
new file mode 100644
index 000000000..a62b76379
--- /dev/null
+++ b/bench/snippets/base64-buffer-to-string.mjs
@@ -0,0 +1,14 @@
+import { bench, run } from "./runner.mjs";
+import { Buffer } from "node:buffer";
+
+const bigBuffer = Buffer.from("hello world".repeat(10000));
+const converted = bigBuffer.toString("base64");
+bench("Buffer.toString('base64')", () => {
+ return bigBuffer.toString("base64");
+});
+
+// bench("Buffer.from(str, 'base64')", () => {
+// return Buffer.from(converted, "base64");
+// });
+
+await run();
diff --git a/bench/snippets/crypto.mjs b/bench/snippets/crypto.mjs
index b0b992a0f..484a4295d 100644
--- a/bench/snippets/crypto.mjs
+++ b/bench/snippets/crypto.mjs
@@ -1,12 +1,6 @@
// so it can run in environments without node module resolution
import { bench, run } from "../node_modules/mitata/src/cli.mjs";
-
-var crypto = globalThis.crypto;
-
-if (!crypto) {
- crypto = await import("node:crypto");
-}
-
+import crypto from "node:crypto";
var foo = new Uint8Array(65536);
bench("crypto.getRandomValues(65536)", () => {
crypto.getRandomValues(foo);
@@ -22,4 +16,8 @@ bench("crypto.randomUUID()", () => {
return crypto.randomUUID()[2];
});
+bench("crypto.randomInt()", () => {
+ return crypto.randomInt(0, 100);
+});
+
await run();
diff --git a/bench/snippets/process-info.mjs b/bench/snippets/process-info.mjs
new file mode 100644
index 000000000..0366472e5
--- /dev/null
+++ b/bench/snippets/process-info.mjs
@@ -0,0 +1,33 @@
+import { bench, run } from "./runner.mjs";
+import { performance } from "perf_hooks";
+
+bench("process.memoryUsage()", () => {
+ process.memoryUsage();
+});
+
+bench("process.memoryUsage.rss()", () => {
+ process.memoryUsage.rss();
+});
+
+bench("process.cpuUsage()", () => {
+ process.cpuUsage();
+});
+
+const init = process.cpuUsage();
+bench("process.cpuUsage(delta)", () => {
+ process.cpuUsage(init);
+});
+
+bench("performance.now()", () => {
+ performance.now();
+});
+
+bench("process.hrtime()", () => {
+ process.hrtime();
+});
+
+bench("process.hrtime.bigint()", () => {
+ process.hrtime.bigint();
+});
+
+await run();
diff --git a/bench/snippets/readfile-not-found.mjs b/bench/snippets/readfile-not-found.mjs
new file mode 100644
index 000000000..c28100ba4
--- /dev/null
+++ b/bench/snippets/readfile-not-found.mjs
@@ -0,0 +1,17 @@
+import { bench, run } from "./runner.mjs";
+import { readFileSync, existsSync } from "node:fs";
+import { readFile } from "node:fs/promises";
+
+bench(`readFileSync(/tmp/404-not-found)`, () => {
+ try {
+ readFileSync("/tmp/404-not-found");
+ } catch (e) {}
+});
+
+bench(`readFile(/tmp/404-not-found)`, async () => {
+ try {
+ await readFile("/tmp/404-not-found");
+ } catch (e) {}
+});
+
+await run();
diff --git a/bench/snippets/util-deprecate.mjs b/bench/snippets/util-deprecate.mjs
new file mode 100644
index 000000000..364601d79
--- /dev/null
+++ b/bench/snippets/util-deprecate.mjs
@@ -0,0 +1,60 @@
+import { bench, run } from "./runner.mjs";
+function deprecateUsingClosure(fn, msg, code) {
+ if (process.noDeprecation === true) {
+ return fn;
+ }
+
+ var realFn = fn;
+ var wrapper = () => {
+ return fnToWrap.apply(this, arguments);
+ };
+
+ var deprecater = () => {
+ if (process.throwDeprecation) {
+ var err = new Error(msg);
+ if (code) err.code = code;
+ throw err;
+ } else if (process.traceDeprecation) {
+ console.trace(msg);
+ } else {
+ console.error(msg);
+ }
+
+ fnToWrap = realFn;
+ return realFn.apply(this, arguments);
+ };
+ var fnToWrap = deprecater;
+
+ return wrapper;
+}
+
+function deprecateOriginal(fn, msg) {
+ var warned = false;
+ function deprecated() {
+ if (!warned) {
+ if (process.throwDeprecation) {
+ throw new Error(msg);
+ } else if (process.traceDeprecation) {
+ console.trace(msg);
+ } else {
+ console.error(msg);
+ }
+ warned = true;
+ }
+ return fn.apply(this, arguments);
+ }
+ return deprecated;
+}
+
+const deprecatedy = deprecateUsingClosure(() => {}, "This is deprecated", "DEP0001");
+const deprecatedy2 = deprecateOriginal(() => {}, "This is deprecated");
+
+bench("deprecateUsingClosure", () => {
+ deprecatedy(Math.random() + 1);
+});
+
+bench("deprecateOriginal", () => {
+ deprecatedy2(Math.random() + 1);
+});
+
+await run();