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(ext/web): optimize performance.measure() #25774

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
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
perf(ext/web): optimize performance.measure()
Optimizes the case when `performance.measure()` needs to find the
startMark by name, by avoiding copying and reversing the complete
entries list.
esroyo committed Sep 20, 2024
commit ca392b44ed92131b948d4cf7df455d0a7c3b7d5d
13 changes: 6 additions & 7 deletions ext/web/15_performance.js
Original file line number Diff line number Diff line change
@@ -3,10 +3,7 @@
import { primordials } from "ext:core/mod.js";
const {
ArrayPrototypeFilter,
ArrayPrototypeFind,
ArrayPrototypePush,
ArrayPrototypeReverse,
ArrayPrototypeSlice,
ObjectKeys,
ObjectPrototypeIsPrototypeOf,
ReflectHas,
@@ -101,10 +98,12 @@ function findMostRecent(
name,
type,
) {
return ArrayPrototypeFind(
ArrayPrototypeReverse(ArrayPrototypeSlice(performanceEntries)),
(entry) => entry.name === name && entry.entryType === type,
);
for (let i = performanceEntries.length - 1; i >= 0; --i) {
const entry = performanceEntries[i];
if (entry.name === name && entry.entryType === type) {
return entry;
}
}
}

function convertMarkToTimestamp(mark) {
55 changes: 55 additions & 0 deletions tests/unit/performance_test.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
import {
assert,
assertEquals,
assertNotEquals,
assertNotStrictEquals,
assertStringIncludes,
assertThrows,
@@ -36,6 +37,41 @@ Deno.test(function performanceToJSON() {
assertEquals(Object.keys(json).length, 1);
});

Deno.test(function clearMarks() {
performance.mark("a");
performance.mark("a");
performance.mark("b");
performance.mark("c");

const marksNum = performance.getEntriesByType("mark").length;

performance.clearMarks("a");
assertEquals(performance.getEntriesByType("mark").length, marksNum - 2);

performance.clearMarks();
assertEquals(performance.getEntriesByType("mark").length, 0);
});

Deno.test(function clearMeasures() {
performance.measure("from-start");
performance.mark("a");
performance.measure("from-mark-a", "a");
performance.measure("from-start");
performance.measure("from-mark-a", "a");
performance.mark("b");
performance.measure("between-a-and-b", "a", "b");

const measuresNum = performance.getEntriesByType("measure").length;

performance.clearMeasures("from-start");
assertEquals(performance.getEntriesByType("measure").length, measuresNum - 2);

performance.clearMeasures();
assertEquals(performance.getEntriesByType("measure").length, 0);

performance.clearMarks();
});

Deno.test(function performanceMark() {
const mark = performance.mark("test");
assert(mark instanceof PerformanceMark);
@@ -127,6 +163,25 @@ Deno.test(function performanceMeasure() {
});
});

Deno.test(function performanceMeasureUseMostRecentMark() {
const markName1 = "mark1";
const measureName1 = "measure1";
const mark1 = performance.mark(markName1);
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const laterMark1 = performance.mark(markName1);
const measure1 = performance.measure(measureName1, markName1);
assertNotEquals(mark1.startTime, measure1.startTime);
assertEquals(laterMark1.startTime, measure1.startTime);
} catch (e) {
return reject(e);
}
resolve();
}, 100);
});
});

Deno.test(function performanceCustomInspectFunction() {
assertStringIncludes(Deno.inspect(performance), "Performance");
assertStringIncludes(