Skip to content

Commit c4b4a04

Browse files
committed
perf(core): replace Array.forEach loops
1 parent 66d7198 commit c4b4a04

File tree

9 files changed

+57
-42
lines changed

9 files changed

+57
-42
lines changed

packages/core/src/composables/useVueFlow.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export class Storage {
4040
const reactiveState = reactive(state)
4141

4242
const hooksOn = <any>{}
43-
Object.entries(reactiveState.hooks).forEach(([n, h]) => {
43+
for (const [n, h] of Object.entries(reactiveState.hooks)) {
4444
const name = `on${n.charAt(0).toUpperCase() + n.slice(1)}`
4545
hooksOn[name] = h.on
46-
})
46+
}
4747

4848
const emits = <any>{}
49-
Object.entries(reactiveState.hooks).forEach(([n, h]) => {
49+
for (const [n, h] of Object.entries(reactiveState.hooks)) {
5050
emits[n] = h.trigger
51-
})
51+
}
5252

5353
// for lookup purposes
5454
const nodeIds = computed(() => reactiveState.nodes.map((n) => n.id))

packages/core/src/composables/useWatchProps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export function useWatchProps(
285285
'autoConnect',
286286
]
287287

288-
Object.keys(props).forEach((key) => {
288+
for (const key of Object.keys(props)) {
289289
const propKey = key as keyof typeof props
290290
if (!skip.includes(propKey)) {
291291
const propValue = toRef(() => props[propKey])
@@ -306,7 +306,7 @@ export function useWatchProps(
306306
})
307307
}
308308
}
309-
})
309+
}
310310
}
311311

312312
const runAll = () => {

packages/core/src/container/EdgeRenderer/MarkerDefinitions.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ const markers = computed(() => {
2020
} else {
2121
markers.push({ id: markerId, color: defaultColor, type: marker as MarkerType })
2222
}
23+
2324
ids.push(markerId)
2425
}
2526
}
2627
}
2728
28-
;[connectionLineOptions.markerEnd, connectionLineOptions.markerStart].forEach(createMarkers)
29+
for (const marker of [connectionLineOptions.markerEnd, connectionLineOptions.markerStart]) {
30+
createMarkers(marker)
31+
}
2932
3033
edges.reduce<MarkerProps[]>((markers, edge) => {
31-
;[edge.markerStart, edge.markerEnd].forEach(createMarkers)
34+
for (const marker of [edge.markerStart, edge.markerEnd]) {
35+
createMarkers(marker)
36+
}
37+
3238
return markers.sort((a, b) => a.id.localeCompare(b.id))
3339
}, markers)
3440

packages/core/src/store/actions.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function useActions(
110110
const updateNodePositions: Actions['updateNodePositions'] = (dragItems, changed, dragging) => {
111111
const changes: NodePositionChange[] = []
112112

113-
dragItems.forEach((node) => {
113+
for (const node of dragItems) {
114114
const change: Partial<NodePositionChange> = {
115115
id: node.id,
116116
type: 'position',
@@ -132,7 +132,7 @@ export function useActions(
132132
}
133133

134134
changes.push(change as NodePositionChange)
135-
})
135+
}
136136

137137
if (changes?.length) {
138138
state.hooks.nodesChange.trigger(changes)
@@ -485,8 +485,8 @@ export function useActions(
485485
}
486486

487487
const removeNodes: Actions['removeNodes'] = (nodes, removeConnectedEdges = true, removeChildren = false) => {
488-
let nodesToRemove = nodes instanceof Function ? nodes(state.nodes) : nodes
489-
nodesToRemove = Array.isArray(nodesToRemove) ? nodesToRemove : [nodesToRemove]
488+
const nextNodes = nodes instanceof Function ? nodes(state.nodes) : nodes
489+
const nodesToRemove = Array.isArray(nextNodes) ? nextNodes : [nextNodes]
490490

491491
const nodeChanges: NodeRemoveChange[] = []
492492
const edgeChanges: EdgeRemoveChange[] = []
@@ -519,21 +519,21 @@ export function useActions(
519519
createEdgeRemovalChanges(children)
520520
}
521521

522-
children.forEach((child) => {
522+
for (const child of children) {
523523
createChildrenRemovalChanges(child.id)
524-
})
524+
}
525525
}
526526
}
527527

528-
nodesToRemove.forEach((item) => {
528+
for (const item of nodesToRemove) {
529529
const currNode = typeof item === 'string' ? findNode(item) : item
530530

531531
if (!currNode) {
532-
return
532+
continue
533533
}
534534

535535
if (isDef(currNode.deletable) && !currNode.deletable) {
536-
return
536+
continue
537537
}
538538

539539
nodeChanges.push(createNodeRemoveChange(currNode.id))
@@ -545,7 +545,7 @@ export function useActions(
545545
if (removeChildren) {
546546
createChildrenRemovalChanges(currNode.id)
547547
}
548-
})
548+
}
549549

550550
if (edgeChanges.length) {
551551
state.hooks.edgesChange.trigger(edgeChanges)
@@ -557,20 +557,20 @@ export function useActions(
557557
}
558558

559559
const removeEdges: Actions['removeEdges'] = (edges) => {
560-
let edgesToRemove = edges instanceof Function ? edges(state.edges) : edges
561-
edgesToRemove = Array.isArray(edgesToRemove) ? edgesToRemove : [edgesToRemove]
560+
const nextEdges = edges instanceof Function ? edges(state.edges) : edges
561+
const edgesToRemove = Array.isArray(nextEdges) ? nextEdges : [nextEdges]
562562

563563
const changes: EdgeRemoveChange[] = []
564564

565-
edgesToRemove.forEach((item) => {
565+
for (const item of edgesToRemove) {
566566
const currEdge = typeof item === 'string' ? findEdge(item) : item
567567

568568
if (!currEdge) {
569-
return
569+
continue
570570
}
571571

572572
if (isDef(currEdge.deletable) && !currEdge.deletable) {
573-
return
573+
continue
574574
}
575575

576576
changes.push(
@@ -582,7 +582,7 @@ export function useActions(
582582
currEdge.targetHandle,
583583
),
584584
)
585-
})
585+
}
586586

587587
state.hooks.edgesChange.trigger(changes)
588588
}
@@ -777,14 +777,14 @@ export function useActions(
777777
}
778778
}
779779

780-
Object.keys(opts).forEach((o) => {
780+
for (const o of Object.keys(opts)) {
781781
const key = o as keyof State
782782
const option = opts[key]
783783

784784
if (![...skip, ...exclude].includes(key) && isDef(option)) {
785785
;(<any>state)[key] = option
786786
}
787-
})
787+
}
788788

789789
until(() => state.d3Zoom)
790790
.not.toBeNull()

packages/core/src/store/getters.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function useGetters(state: State, nodeIds: ComputedRef<string[]>, edgeIds
3535

3636
const keys = Object.keys(edgeTypes)
3737

38-
state.edges?.forEach((e) => e.type && !keys.includes(e.type) && (edgeTypes[e.type] = e.type))
38+
for (const e of state.edges) {
39+
e.type && !keys.includes(e.type) && (edgeTypes[e.type] = e.type)
40+
}
3941

4042
return edgeTypes
4143
})
@@ -48,7 +50,9 @@ export function useGetters(state: State, nodeIds: ComputedRef<string[]>, edgeIds
4850

4951
const keys = Object.keys(nodeTypes)
5052

51-
state.nodes?.forEach((n) => n.type && !keys.includes(n.type) && (nodeTypes[n.type] = n.type))
53+
for (const n of state.nodes) {
54+
n.type && !keys.includes(n.type) && (nodeTypes[n.type] = n.type)
55+
}
5256

5357
return nodeTypes
5458
})

packages/core/src/store/state.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,14 @@ function defaultState(): State {
145145

146146
export function useState(opts?: FlowOptions): State {
147147
const state = defaultState()
148+
148149
if (opts) {
149-
Object.keys(opts).forEach((o) => {
150-
const option = opts[o as keyof typeof opts]
150+
for (const key of Object.keys(opts)) {
151+
const option = opts[key as keyof typeof opts]
151152
if (isDef(option)) {
152-
;(state as any)[o] = option
153+
;(state as any)[key] = option
153154
}
154-
})
155+
}
155156
}
156157

157158
return state

packages/core/src/utils/changes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function applyChanges<
114114
| EdgeRemoveChange
115115
)[]
116116

117-
addRemoveChanges.forEach((change) => {
117+
for (const change of addRemoveChanges) {
118118
if (change.type === 'add') {
119119
const index = elements.findIndex((el) => el.id === change.item.id)
120120

@@ -128,11 +128,11 @@ export function applyChanges<
128128
elements.splice(index, 1)
129129
}
130130
}
131-
})
131+
}
132132

133133
const elementIds = elements.map((el) => el.id)
134134

135-
elements.forEach((element) => {
135+
for (const element of elements) {
136136
const currentChanges = changes.filter((c) => (<any>c).id === element.id)
137137

138138
for (const currentChange of currentChanges) {
@@ -198,7 +198,7 @@ export function applyChanges<
198198
break
199199
}
200200
}
201-
})
201+
}
202202

203203
return elements
204204
}

packages/core/src/utils/graph.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ function getConnectedElements<T extends Node = Node>(
169169

170170
const origin = dir === 'source' ? 'target' : 'source'
171171

172-
edges.forEach((edge) => {
172+
for (const edge of edges) {
173173
if (edge[origin] === id) {
174174
connectedIds.add(edge[dir])
175175
}
176-
})
176+
}
177177

178178
return nodes.filter((n) => connectedIds.has(n.id))
179179
}
@@ -415,7 +415,9 @@ export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, ed
415415
if (typeof nodesOrId === 'string') {
416416
nodeIds.add(nodesOrId)
417417
} else if (nodesOrId.length >= 1) {
418-
nodesOrId.forEach((n) => nodeIds.add(n.id))
418+
for (const n of nodesOrId) {
419+
nodeIds.add(n.id)
420+
}
419421
}
420422

421423
return edges.filter((edge) => nodeIds.has(edge.source) || nodeIds.has(edge.target))
@@ -424,7 +426,9 @@ export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, ed
424426
export function getConnectedNodes<N extends Node | { id: string } | string>(nodes: N[], edges: Edge[]) {
425427
const nodeIds = new Set()
426428

427-
nodes.forEach((node) => nodeIds.add(typeof node === 'string' ? node : node.id))
429+
for (const node of nodes) {
430+
nodeIds.add(typeof node === 'string' ? node : node.id)
431+
}
428432

429433
const connectedNodeIds = edges.reduce((acc, edge) => {
430434
if (nodeIds.has(edge.source)) {

packages/core/src/utils/handle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function getClosestHandle(
101101
let closestHandles: { handle: ConnectionHandle; validHandleResult: ValidHandleResult }[] = []
102102
let minDistance = Number.POSITIVE_INFINITY
103103

104-
handles.forEach((handle) => {
104+
for (const handle of handles) {
105105
const distance = Math.sqrt((handle.x - pos.x) ** 2 + (handle.y - pos.y) ** 2)
106106

107107
if (distance <= connectionRadius) {
@@ -121,7 +121,7 @@ export function getClosestHandle(
121121
minDistance = distance
122122
}
123123
}
124-
})
124+
}
125125

126126
if (!closestHandles.length) {
127127
return { handle: null, validHandleResult: defaultValidHandleResult() }

0 commit comments

Comments
 (0)