Skip to content

Commit 08ccfb1

Browse files
committed
perf: reduce bundle size
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent f295613 commit 08ccfb1

File tree

7 files changed

+107
-104
lines changed

7 files changed

+107
-104
lines changed

src/BeautifulTree.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ export function BeautifulTree({
5858
}}
5959
className={'beautiful-tree-react'}
6060
>
61-
<style>
62-
{'line { stroke: black; } circle { stroke: black; fill: white; }'}
63-
</style>
61+
<style>{'line{stroke:black;}circle{stroke:black;fill:white;}'}</style>
6462
{Array.from(edgesIterator(treeWithLayout), (edge, idx) => {
6563
return (
6664
<line
6765
key={`${id}-edge-${idx}`}
6866
className={`beautiful-tree-edge${runClassesGetter(
6967
edgeClassesInferrer,
70-
edge.edgeData,
68+
edge.eData,
7169
)}`}
7270
x1={(edge.start.x + 1) * xCoef}
7371
y1={(edge.start.y + 1) * yCoef}

src/layouts.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface WrappedTreeWithLayout {
77
}
88

99
const M = Math.max
10+
const C = 'children'
1011

1112
const _computeNaiveLayout = (
1213
tree: Tree,
@@ -19,11 +20,11 @@ const _computeNaiveLayout = (
1920
l[depth] = x
2021
counters.mX = M(counters.mX, x)
2122

22-
const tc = tree.children
23+
const tc = tree[C]
2324
return {
2425
data: tree.data,
2526
children: tc?.map((child: Readonly<TreeChild>) => ({
26-
edgeData: child.edgeData,
27+
eData: child.eData,
2728
node: _computeNaiveLayout(child.node, depth + 1, counters),
2829
})),
2930
meta: {
@@ -49,32 +50,36 @@ type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> }
4950

5051
const _addMods = (
5152
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
52-
tree: DeepWriteable<TreeWithLayout>,
53+
{ meta, children }: DeepWriteable<TreeWithLayout>,
5354
modsum = 0,
5455
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
5556
tracer: { mX: number },
5657
): void => {
57-
tree.meta.pos.x += modsum
58-
tracer.mX = M(tracer.mX, tree.meta.pos.x)
58+
meta.pos.x += modsum
59+
tracer.mX = M(tracer.mX, meta.pos.x)
5960
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
60-
modsum += tree.meta.m! // We know it's defined because we control when it's called
61-
for (const child of tree.children ?? []) {
61+
modsum += meta.m! // We know it's defined because we control when it's called
62+
for (const child of children ?? []) {
6263
_addMods(child.node, modsum, tracer)
6364
}
6465
}
6566

66-
const _computeCenter2Layout = (
67+
const _getPosX = (v: { readonly node: Readonly<TreeWithLayout> }): number => {
68+
return v.node.meta.pos.x
69+
}
70+
71+
const _computeSmartLayout = (
6772
tree: Tree,
6873
depth = 0,
6974
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
7075
offsets: number[],
7176
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
7277
tracer: { mX: number },
7378
): DeepWriteable<TreeWithLayout> => {
74-
const tc = tree.children
79+
const tc = tree[C]
7580
const children = tc?.map((child) => ({
76-
edgeData: child.edgeData,
77-
node: _computeCenter2Layout(child.node, depth + 1, offsets, tracer),
81+
eData: child.eData,
82+
node: _computeSmartLayout(child.node, depth + 1, offsets, tracer),
7883
}))
7984

8085
let x: number
@@ -84,12 +89,12 @@ const _computeCenter2Layout = (
8489
x = offsets[depth] ?? 0
8590
} else if (numChildren === 1) {
8691
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
87-
x = children![0]!.node.meta.pos.x
92+
x = _getPosX(children![0]!)
8893
} else {
8994
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
90-
(children![0]!.node.meta.pos.x +
95+
(_getPosX(children![0]!) +
9196
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
92-
children![numChildren - 1]!.node.meta.pos.x) *
97+
_getPosX(children![numChildren - 1]!)) *
9398
0.5
9499
x = M(offsets[depth] ?? 0, c)
95100
m = x - c
@@ -126,12 +131,12 @@ const _inPlaceEvenSpacingUpdate = (
126131
tmp.x += shift
127132
} else if (numChildren === 1) {
128133
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
129-
tmp.x = tree.children![0]!.node.meta.pos.x
134+
tmp.x = _getPosX(tree[C]![0]!)
130135
} else {
131136
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
132-
(tree.children![0]!.node.meta.pos.x +
137+
(_getPosX(tree[C]![0]!) +
133138
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
134-
tree.children![numChildren - 1]!.node.meta.pos.x) *
139+
_getPosX(tree[C]![numChildren - 1]!)) *
135140
0.5
136141
tmp.x = M(offsets[depth] ?? 0, c)
137142
}
@@ -151,18 +156,18 @@ const _siblingsEvenSpacing = (
151156
shift = 0,
152157
// eslint-disable-next-line sonarjs/cognitive-complexity
153158
): void => {
154-
const tc = tree.children
159+
const tc = tree[C]
155160
const numChildren = tc?.length ?? 0
156161
let lastFixedIdx: number | undefined
157162
let maxSpacing = 1
158163
for (const [idx, { node }] of (tc ?? []).entries()) {
159-
const isFixed = (node.children?.length ?? 0) > 0
164+
const isFixed = (node[C]?.length ?? 0) > 0
160165
if (isFixed) {
161166
if (lastFixedIdx !== undefined) {
162167
const spacing =
163168
(node.meta.pos.x -
164169
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165-
tc![lastFixedIdx]!.node.meta.pos.x) /
170+
_getPosX(tc![lastFixedIdx]!)) /
166171
(idx - lastFixedIdx)
167172
maxSpacing = M(maxSpacing, spacing)
168173
}
@@ -178,7 +183,7 @@ const _siblingsEvenSpacing = (
178183
0,
179184
shift +
180185
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
181-
tc![1]!.node.meta.pos.x -
186+
_getPosX(tc![1]!) -
182187
maxSpacing -
183188
node.meta.pos.x,
184189
)
@@ -189,7 +194,7 @@ const _siblingsEvenSpacing = (
189194
M(
190195
0,
191196
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
192-
tc![idx - 1]!.node.meta.pos.x + maxSpacing - node.meta.pos.x,
197+
_getPosX(tc![idx - 1]!) + maxSpacing - node.meta.pos.x,
193198
)
194199
}
195200
_siblingsEvenSpacing(node, offsets, tracer, depth + 1, accShift)
@@ -208,7 +213,7 @@ const _cousinsEvenSpacing = (
208213
depth = 0,
209214
shift = 0,
210215
): void => {
211-
const tc = tree.children
216+
const tc = tree[C]
212217
const numChildren = tc?.length ?? 0
213218

214219
const nextOffset = offsets[depth + 1]
@@ -217,12 +222,12 @@ const _cousinsEvenSpacing = (
217222
numChildren >= 2 &&
218223
nextOffset !== undefined &&
219224
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
220-
(tc![0]!.node.children?.length ?? 0) === 0
225+
(tc![0]!.node[C]?.length ?? 0) === 0
221226
) {
222227
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223-
const mid = tc![1]!.node.meta.pos.x - 1
228+
const mid = _getPosX(tc![1]!) - 1
224229
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
225-
accShift = shift + M(0, mid - tc![0]!.node.meta.pos.x)
230+
accShift = shift + M(0, mid - _getPosX(tc![0]!))
226231
}
227232

228233
for (const [idx, { node }] of (tc ?? []).entries()) {
@@ -242,7 +247,7 @@ export const computeSmartLayout = (
242247
const offsets: number[] = []
243248
const tracer = { mX: 0 }
244249

245-
const t = _computeCenter2Layout(tree, 0, offsets, tracer)
250+
const t = _computeSmartLayout(tree, 0, offsets, tracer)
246251
_addMods(t, 0, tracer)
247252

248253
_cousinsEvenSpacing(t, [], tracer)

src/stories/BeautifulTree.stories.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Story = StoryObj<typeof meta>
2525

2626
const mirrorTree = (tree: Tree): Tree => {
2727
const children = tree.children?.map((child) => ({
28-
edgeData: child.edgeData,
28+
eData: child.eData,
2929
node: mirrorTree(child.node),
3030
}))
3131
return {
@@ -239,7 +239,7 @@ const wideTree_D: Tree = {
239239
data: { v: -1 },
240240
children: [
241241
{
242-
edgeData: { e: 1 },
242+
eData: { e: 1 },
243243
node: {
244244
data: { v: -2 },
245245
children: [
@@ -248,7 +248,7 @@ const wideTree_D: Tree = {
248248
data: { v: -3 },
249249
children: [
250250
{ node: { data: { v: -4 } } },
251-
{ edgeData: { e: 1 }, node: { data: { v: -2.5 } } },
251+
{ eData: { e: 1 }, node: { data: { v: -2.5 } } },
252252
],
253253
},
254254
},

0 commit comments

Comments
 (0)