@loken/hierarchies
Version:
Library for working with hierarchies of identifiers and identifiable objects.
105 lines (104 loc) • 3.65 kB
JavaScript
import { describe, bench, expect } from "vitest";
import { ChildMap } from "../utilities/child-map.js";
import { Nodes } from "../nodes/nodes.js";
import { flattenFullGraph, flattenSignalGraph } from "./graph-flatten.js";
import { iterateAll } from "@loken/utilities";
import { traverseFullGraph, traverseSignalGraph } from "./graph-traverse.js";
import { searchGraph } from "./graph-search.js";
const counts = [1e3, 1e4, 1e5], infos = /* @__PURE__ */ new Map();
counts.forEach((count) => {
let searchDepth = -1, searchId = "";
const childMap = ChildMap.generate({
count,
create: ({ ancestry, siblings }) => {
const parentId = ancestry == null ? void 0 : ancestry.at(-1), siblingId = siblings.length + 1, id = parentId ? parentId + "-" + siblingId : siblingId.toString();
return ancestry.length > searchDepth && (searchDepth = ancestry.length, searchId = id), id;
}
});
infos.set(count, { roots: Nodes.assembleIds(childMap), searchId, searchDepth });
});
counts.forEach((count) => {
describe(`traverse graph of ${count} nodes`, () => {
const roots = infos.get(count).roots;
bench("bf traverseFullGraph", () => {
iterateAll(traverseFullGraph({
roots,
next: (node) => node.getChildren(),
type: "breadth-first"
}));
}), bench("df traverseFullGraph", () => {
iterateAll(traverseFullGraph({
roots,
next: (node) => node.getChildren(),
type: "depth-first"
}));
}), bench("bf traverseSignalGraph", () => {
iterateAll(traverseSignalGraph({
roots,
signal: (n, s) => s.next(n.getChildren()),
type: "breadth-first"
}));
}), bench("df traverseSignalGraph", () => {
iterateAll(traverseSignalGraph({
roots,
signal: (n, s) => s.next(n.getChildren()),
type: "depth-first"
}));
}), bench("bf flattenFullGraph", () => {
flattenFullGraph({
roots,
next: (node) => node.getChildren(),
type: "depth-first"
});
}), bench("df flattenFullGraph", () => {
flattenFullGraph({
roots,
next: (node) => node.getChildren(),
type: "breadth-first"
});
}), bench("bf flattenSignalGraph", () => {
flattenSignalGraph({
roots,
signal: (n, s) => s.next(n.getChildren()),
type: "breadth-first"
});
}), bench("df flattenSignalGraph", () => {
flattenSignalGraph({
roots,
signal: (n, s) => s.next(n.getChildren()),
type: "depth-first"
});
});
});
});
counts.forEach((count) => {
describe(`search graph of ${count} nodes`, () => {
const { roots, searchId } = infos.get(count), search = (node) => node.item === searchId;
bench("searchGraph", () => {
const found = searchGraph({
roots,
next: (node) => node.getChildren(),
search
});
expect(found == null ? void 0 : found.item).toEqual(searchId);
}), bench("traverseSignalGraph", () => {
var _a;
const found = (_a = traverseSignalGraph({
roots,
signal: (n, s) => {
search(n) ? s.end() : (s.skip(), s.next(n.getChildren()));
}
}).next()) == null ? void 0 : _a.value;
expect(found == null ? void 0 : found.item).toEqual(searchId);
}), bench("flattenSignalGraph", () => {
const found = flattenSignalGraph({
roots,
signal: (n, s) => {
search(n) ? s.end() : (s.skip(), s.next(n.getChildren()));
}
})[0];
expect(found == null ? void 0 : found.item).toEqual(searchId);
});
});
});
//# sourceMappingURL=graph.bench.js.map