Skip to content

Commit

Permalink
perf: optimisations
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Apr 8, 2024
1 parent c6f3014 commit faff6d4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

### Changed

- Optimize benchmark statistics computation.

## [0.2.1] - 2024-04-07

### Changed
Expand Down
62 changes: 32 additions & 30 deletions src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,28 @@ export function group(name, cb) {
}
if (cb != null && ![Function, AsyncFunction].includes(cb.constructor))
throw new TypeError(`expected function, got ${cb.constructor.name}`);
if (
Object.prototype.toString.call(name).slice(8, -1) === 'Object' &&
name.name != null &&
'string' !== typeof name.name
)
throw new TypeError(`expected string, got ${name.name.constructor.name}`);
if (
Object.prototype.toString.call(name).slice(8, -1) === 'Object' &&
name.summary != null &&
'boolean' !== typeof name.summary
)
throw new TypeError(
`expected boolean, got ${name.summary.constructor.name}`,
);
if (
Object.prototype.toString.call(name).slice(8, -1) === 'Object' &&
name.before != null &&
![Function, AsyncFunction].includes(name.before.constructor)
)
throw new TypeError(
`expected function, got ${name.before.constructor.name}`,
);
if (
Object.prototype.toString.call(name).slice(8, -1) === 'Object' &&
name.after != null &&
![Function, AsyncFunction].includes(name.after.constructor)
)
throw new TypeError(
`expected function, got ${name.after.constructor.name}`,
);
if (Object.prototype.toString.call(name).slice(8, -1) === 'Object') {
if (name.name != null && 'string' !== typeof name.name)
throw new TypeError(`expected string, got ${name.name.constructor.name}`);
if (name.summary != null && 'boolean' !== typeof name.summary)
throw new TypeError(
`expected boolean, got ${name.summary.constructor.name}`,
);
if (
name.before != null &&
![Function, AsyncFunction].includes(name.before.constructor)
)
throw new TypeError(
`expected function, got ${name.before.constructor.name}`,
);
if (
name.after != null &&
![Function, AsyncFunction].includes(name.after.constructor)
)
throw new TypeError(
`expected function, got ${name.after.constructor.name}`,
);
}

groupName =
('string' === typeof name ? name.trim() : name.name?.trim()) ||
Expand Down Expand Up @@ -139,6 +131,16 @@ export function clear() {
}

export async function run(opts = {}) {
if (Object.prototype.toString.call(opts).slice(8, -1) !== 'Object')
throw new TypeError(`expected object, got ${opts.constructor.name}`);
if (opts.samples != null && 'number' !== typeof opts.samples)
throw new TypeError(
`expected number as 'samples' option, got ${opts.samples.constructor.name}`,
);
if (opts.time != null && 'number' !== typeof opts.time)
throw new TypeError(
`expected number as 'time' option, got ${opts.time.constructor.name}`,
);
if (
opts.json != null &&
'number' !== typeof opts.json &&
Expand Down
29 changes: 10 additions & 19 deletions src/lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,9 @@ export const cpu = await (async () => {
return await {
unknown: () => 'unknown',
browser: () => 'unknown',
node: async () => (await import('node:os')).cpus()[0].model,
deno: async () => {
try {
const os = await import('node:os');
if (os?.cpus?.()?.[0]?.model) return os.cpus()[0].model;
} catch {}

return 'unknown';
},
bun: async () => {
try {
const os = await import('node:os');
if (os?.cpus?.()?.[0]?.model) return os.cpus()[0].model;
} catch {}

return 'unknown';
},
node: async () => (await import('node:os'))?.cpus?.()?.[0]?.model,
deno: async () => (await import('node:os'))?.cpus?.()?.[0]?.model,
bun: async () => (await import('node:os'))?.cpus?.()?.[0]?.model,
}[runtime]();
})();

Expand All @@ -63,6 +49,8 @@ export const noColor = (() => {
export const checkBenchmarkArgs = (fn, opts = {}) => {
if (![Function, AsyncFunction].includes(fn.constructor))
throw new TypeError(`expected function, got ${fn.constructor.name}`);
if (Object.prototype.toString.call(opts).slice(8, -1) !== 'Object')
throw new TypeError(`expected object, got ${opts.constructor.name}`);
if (opts.warmup != null && 'boolean' !== typeof opts.warmup)
throw new TypeError(
`expected boolean, got ${opts.warmup.constructor.name}`,
Expand Down Expand Up @@ -142,6 +130,8 @@ export async function measure(fn, before, after, opts = {}) {
throw new TypeError(`expected function, got ${before.constructor.name}`);
if (![Function, AsyncFunction].includes(after.constructor))
throw new TypeError(`expected function, got ${after.constructor.name}`);
if (Object.prototype.toString.call(opts).slice(8, -1) !== 'Object')
throw new TypeError(`expected object, got ${opts.constructor.name}`);

// biome-ignore lint/style/noParameterAssign: <explanation>
opts = mergeDeepRight(
Expand Down Expand Up @@ -213,7 +203,8 @@ export async function measure(fn, before, after, opts = {}) {
samples.sort((a, b) => a - b);
samples = iqrFilter(samples);

const avg = samples.reduce((a, b) => a + b, 0) / samples.length;
const time = samples.reduce((a, b) => a + b, 0);
const avg = time / samples.length;
const std = Math.sqrt(
samples.reduce((a, b) => a + (b - avg) ** 2, 0) / (samples.length - 1),
);
Expand All @@ -229,7 +220,7 @@ export async function measure(fn, before, after, opts = {}) {
p999: quantile(samples, 0.999),
avg,
std,
iter: samples.length / (samples.reduce((a, b) => a + b, 0) / 1e9),
iter: samples.length / (time / 1e9),
rawSamples: rawSamples.length,
rawAvg,
rawStd,
Expand Down
9 changes: 3 additions & 6 deletions src/reporter/clr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ function init(x, y) {
if (!e || txt == null) return txt;
// biome-ignore lint/style/noParameterAssign: <explanation>
txt = txt.toString();
return (
open +
// biome-ignore lint/complexity/noExtraBooleanCast: <explanation>
(!!~txt.indexOf(close) ? txt.replace(rgx, close + open) : txt) +
close
);
return `${open}${
txt.includes(close) ? txt.replace(rgx, close + open) : txt
}${close}`;
};
}

Expand Down

0 comments on commit faff6d4

Please sign in to comment.