Skip to content

Commit

Permalink
perf: use samplesLength instead of multiple use samples.length (#59)
Browse files Browse the repository at this point in the history
* perf: avoid calling `sort()` and multiple use `samples.length`

* fix: typo

* revert: sort() implementation

* revert: keep original

* feat: improve

* fix: sort
  • Loading branch information
Dunqing committed Oct 11, 2023
1 parent 9a53a51 commit f52f027
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,29 @@ export default class Task extends EventTarget {

await this.bench.teardown(this, 'run');

samples.sort((a, b) => a - b);

if (!this.result?.error) {
const min = samples[0]!;
const max = samples[samples.length - 1]!;
samples.sort((a, b) => a - b);

const period = totalTime / this.runs;
const hz = 1000 / period;

const samplesLength = samples.length;
const df = samplesLength - 1;
const min = samples[0]!;
const max = samples[df]!;
// benchmark.js: https://github.com/bestiejs/benchmark.js/blob/42f3b732bac3640eddb3ae5f50e445f3141016fd/benchmark.js#L1912-L1927
const mean = getMean(samples);
const variance = getVariance(samples, mean);
const sd = Math.sqrt(variance);
const sem = sd / Math.sqrt(samples.length);
const df = samples.length - 1;
const sem = sd / Math.sqrt(samplesLength);
const critical = tTable[String(Math.round(df) || 1)] || tTable.infinity!;
const moe = sem * critical;
const rme = (moe / mean) * 100 || 0;
const rme = (moe / mean) * 100;

// mitata: https://github.com/evanwashere/mitata/blob/3730a784c9d83289b5627ddd961e3248088612aa/src/lib.mjs#L12
const p75 = samples[Math.ceil(samples.length * (75 / 100)) - 1]!;
const p99 = samples[Math.ceil(samples.length * (99 / 100)) - 1]!;
const p995 = samples[Math.ceil(samples.length * (99.5 / 100)) - 1]!;
const p999 = samples[Math.ceil(samples.length * (99.9 / 100)) - 1]!;
const p75 = samples[Math.ceil(samplesLength * 0.75) - 1]!;
const p99 = samples[Math.ceil(samplesLength * 0.99) - 1]!;
const p995 = samples[Math.ceil(samplesLength * 0.995) - 1]!;
const p999 = samples[Math.ceil(samplesLength * 0.999) - 1]!;

if (this.bench.signal?.aborted) {
return this;
Expand Down

0 comments on commit f52f027

Please sign in to comment.