Skip to content

Commit

Permalink
remove parsing body
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Aug 20, 2024
1 parent 524f545 commit 5541f27
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 233 deletions.
120 changes: 60 additions & 60 deletions benchmarks/_util/runner.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
//@ts-check
// @ts-check

"use strict";
'use strict'

class Info {
/**@type {string} */
#name;
/**@type {bigint} */
#current;
/**@type {bigint} */
#finish;
/**@type {(...args: any[]) => any} */
#callback;
/**@type {boolean} */
#finalized = false;
/** @type {string} */
#name
/** @type {bigint} */
#current
/** @type {bigint} */
#finish
/** @type {(...args: any[]) => any} */
#callback
/** @type {boolean} */
#finalized = false

/**
* @param {string} name
* @param {(...args: any[]) => any} callback
*/
constructor(name, callback) {
this.#name = name;
this.#callback = callback;
constructor (name, callback) {
this.#name = name
this.#callback = callback
}

get name() {
return this.#name;
get name () {
return this.#name
}

start() {
start () {
if (this.#finalized) {
throw new TypeError("called after finished.");
throw new TypeError('called after finished.')
}
this.#current = process.hrtime.bigint();
this.#current = process.hrtime.bigint()
}

end() {
end () {
if (this.#finalized) {
throw new TypeError("called after finished.");
throw new TypeError('called after finished.')
}
this.#finish = process.hrtime.bigint();
this.#finalized = true;
this.#callback();
this.#finish = process.hrtime.bigint()
this.#finalized = true
this.#callback()
}

diff() {
return Number(this.#finish - this.#current);
diff () {
return Number(this.#finish - this.#current)
}
}

Expand All @@ -58,71 +58,71 @@ class Info {
* @param {{}} [options]
* @returns {Promise<{ name: string; average: number; samples: number; fn: BenchMarkHandler; min: number; max: number }[]>}
*/
async function bench(experiments, options = {}) {
const names = Object.keys(experiments);
async function bench (experiments, options = {}) {
const names = Object.keys(experiments)

/**@type {{ name: string; average: number; samples: number; fn: BenchMarkHandler; min: number; max: number }[]} */
const results = [];
/** @type {{ name: string; average: number; samples: number; fn: BenchMarkHandler; min: number; max: number }[]} */
const results = []

async function waitMaybePromiseLike(p) {
async function waitMaybePromiseLike (p) {
if (
(typeof p === "object" || typeof p === "function") &&
(typeof p === 'object' || typeof p === 'function') &&
p !== null &&
typeof p.then === "function"
typeof p.then === 'function'
) {
await p;
await p
}
}

for (let i = 0; i < names.length; ++i) {
const name = names[i];
const fn = experiments[name];
const samples = [];
const name = names[i]
const fn = experiments[name]
const samples = []

let timing = 0;
let timing = 0

for (let j = 0; j < 128 || timing < 800_000_000; ++j) {
let resolve = (value) => {},
reject = (reason) => {},
promise = new Promise(
(done, fail) => ((resolve = done), (reject = fail))
);
let resolve = (value) => {}
let reject = (reason) => {}
const promise = new Promise(
(done, fail) => ((resolve = done), (reject = fail))

Check failure on line 88 in benchmarks/_util/runner.js

View workflow job for this annotation

GitHub Actions / Lint

Promise constructor parameters must be named to match "^_?resolve$"

Check failure on line 88 in benchmarks/_util/runner.js

View workflow job for this annotation

GitHub Actions / Lint

Promise constructor parameters must be named to match "^_?reject$"

Check failure on line 88 in benchmarks/_util/runner.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected use of comma operator
)

const info = new Info(name, resolve);
const info = new Info(name, resolve)

try {
const p = fn(info);
const p = fn(info)

await waitMaybePromiseLike(p);
await waitMaybePromiseLike(p)
} catch (err) {
reject(err);
reject(err)
}

await promise;
await promise

samples.push({ time: info.diff() });
samples.push({ time: info.diff() })

timing += info.diff();
timing += info.diff()
}

const average =
samples.map((v) => v.time).reduce((a, b) => a + b, 0) / samples.length;
samples.map((v) => v.time).reduce((a, b) => a + b, 0) / samples.length

results.push({
name: names[i],
average: average,
average,
samples: samples.length,
fn: fn,
fn,
min: samples.reduce((a, acc) => Math.min(a, acc.time), samples[0].time),
max: samples.reduce((a, acc) => Math.max(a, acc.time), samples[0].time),
});
max: samples.reduce((a, acc) => Math.max(a, acc.time), samples[0].time)
})
}

return results;
return results
}

function print() {
function print () {

Check failure on line 124 in benchmarks/_util/runner.js

View workflow job for this annotation

GitHub Actions / Lint

'print' is defined but never used

}

module.exports = { bench };
module.exports = { bench }
Loading

0 comments on commit 5541f27

Please sign in to comment.