Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use samplesLength instead of multiple use samples.length #59

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export default class Task extends EventTarget {
async run() {
this.dispatchEvent(createBenchEvent('start', this));
let totalTime = 0; // ms
let min = Infinity;
let max = -Infinity;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to get the min and max, we need to return them actually, and when you do, add it to the documentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And aren't they in the wrong order? min should be -Infinity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to get the min and max, we need to return them actually, and when you do, add it to the documentation.

I do not understand what you're saying. There were min and max in the first place just a different way to get them.

And aren't they in the wrong order? min should be -Infinity.

I think you may have misunderstood. If min is -Infinity it means that there is no value smaller than this min

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I get it now.

Copy link
Member

@Aslemammad Aslemammad Oct 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand what you're saying. There were min and max in the first place just a different way to get them.

Yes, we used to get them by samples[samples.length] for instance, but since we do not sort now, we need to return them along other results like mean, variance, ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand what you're saying. There were min and max in the first place just a different way to get them.

Yes, we used to get them by samples[samples.length] for instance, but since we do not sort now, we need to return them along other results like mean, variance, ...

Oh, I got it. Looks like we still need to call sort() otherwise the result will be incorrect

const samples: number[] = [];

await this.bench.setup(this, 'run');
Expand Down Expand Up @@ -90,9 +92,11 @@ export default class Task extends EventTarget {
taskTime = this.bench.now() - taskStart;
}

samples.push(taskTime);
this.runs += 1;
totalTime += taskTime;
samples.push(taskTime);
if (taskTime > max) max = taskTime;
if (taskTime < min) min = taskTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do these lines do exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the max and min values, This way we don't have to call sort() again.


if (this.opts.afterEach != null) {
await this.opts.afterEach.call(this);
Expand All @@ -112,29 +116,26 @@ 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]!;
const period = totalTime / this.runs;
const hz = 1000 / period;

// benchmark.js: https://github.com/bestiejs/benchmark.js/blob/42f3b732bac3640eddb3ae5f50e445f3141016fd/benchmark.js#L1912-L1927
const simplesLength = samples.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

samplesLength

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(simplesLength);
const df = simplesLength - 1;
const critical = tTable[String(Math.round(df) || 1)] || tTable.infinity!;
const moe = sem * critical;
const rme = (moe / mean) * 100 || 0;

// 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(simplesLength * (75 / 100)) - 1]!;
const p99 = samples[Math.ceil(simplesLength * (99 / 100)) - 1]!;
const p995 = samples[Math.ceil(simplesLength * (99.5 / 100)) - 1]!;
const p999 = samples[Math.ceil(simplesLength * (99.9 / 100)) - 1]!;

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