Skip to content

Commit 25d7461

Browse files
authored
perf: use a Map for tracking chunks (#313)
1 parent 80c16ca commit 25d7461

1 file changed

Lines changed: 31 additions & 31 deletions

File tree

src/MagicString.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ export default class MagicString {
6666
/** @internal */
6767
declare lastSearchedChunk: Chunk
6868
/** @internal */
69-
declare byStart: Record<number, Chunk>
69+
declare byStart: Map<number, Chunk>
7070
/** @internal */
71-
declare byEnd: Record<number, Chunk>
71+
declare byEnd: Map<number, Chunk>
7272
/** @internal */
7373
declare sourcemapLocations: BitSet
7474
/** @internal */
@@ -103,8 +103,8 @@ export default class MagicString {
103103
Object.defineProperty(this, 'stats', { value: new Stats() })
104104
}
105105

106-
this.byStart[0] = chunk
107-
this.byEnd[string.length] = chunk
106+
this.byStart = new Map<number, Chunk>([[0, chunk]])
107+
this.byEnd = new Map<number, Chunk>([[string.length, chunk]])
108108
}
109109

110110
/**
@@ -141,7 +141,7 @@ export default class MagicString {
141141

142142
this._split(index)
143143

144-
const chunk = this.byEnd[index]
144+
const chunk = this.byEnd.get(index)
145145

146146
if (chunk) {
147147
chunk.appendLeft(content)
@@ -171,7 +171,7 @@ export default class MagicString {
171171

172172
this._split(index)
173173

174-
const chunk = this.byStart[index]
174+
const chunk = this.byStart.get(index)
175175

176176
if (chunk) {
177177
chunk.appendRight(content)
@@ -195,8 +195,8 @@ export default class MagicString {
195195
let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone())
196196

197197
while (originalChunk) {
198-
cloned.byStart[clonedChunk.start] = clonedChunk
199-
cloned.byEnd[clonedChunk.end] = clonedChunk
198+
cloned.byStart.set(clonedChunk.start, clonedChunk)
199+
cloned.byEnd.set(clonedChunk.end, clonedChunk)
200200

201201
const nextOriginalChunk = originalChunk.next
202202
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone()
@@ -497,13 +497,13 @@ export default class MagicString {
497497
this._split(end)
498498
this._split(index)
499499

500-
const first = this.byStart[start]
501-
const last = this.byEnd[end]
500+
const first = this.byStart.get(start)
501+
const last = this.byEnd.get(end)
502502

503503
const oldLeft = first.previous
504504
const oldRight = last.next
505505

506-
const newRight = this.byStart[index]
506+
const newRight = this.byStart.get(index)
507507
if (!newRight && last === this.lastChunk)
508508
return this
509509
const newLeft = newRight ? newRight.previous : this.lastChunk
@@ -617,13 +617,13 @@ export default class MagicString {
617617
})
618618
}
619619

620-
const first = this.byStart[start]
621-
const last = this.byEnd[end]
620+
const first = this.byStart.get(start)
621+
const last = this.byEnd.get(end)
622622

623623
if (first) {
624624
let chunk = first
625625
while (chunk !== last) {
626-
if (chunk.next !== this.byStart[chunk.end]) {
626+
if (chunk.next !== this.byStart.get(chunk.end)) {
627627
throw new Error('Cannot overwrite across a split point')
628628
}
629629
chunk = chunk.next
@@ -671,7 +671,7 @@ export default class MagicString {
671671

672672
this._split(index)
673673

674-
const chunk = this.byEnd[index]
674+
const chunk = this.byEnd.get(index)
675675

676676
if (chunk) {
677677
chunk.prependLeft(content)
@@ -699,7 +699,7 @@ export default class MagicString {
699699

700700
this._split(index)
701701

702-
const chunk = this.byStart[index]
702+
const chunk = this.byStart.get(index)
703703

704704
if (chunk) {
705705
chunk.prependRight(content)
@@ -740,14 +740,14 @@ export default class MagicString {
740740
this._split(start)
741741
this._split(end)
742742

743-
let chunk = this.byStart[start]
743+
let chunk = this.byStart.get(start)
744744

745745
while (chunk) {
746746
chunk.intro = ''
747747
chunk.outro = ''
748748
chunk.edit('')
749749

750-
chunk = end > chunk.end ? this.byStart[chunk.end] : null
750+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null
751751
}
752752

753753
if (DEBUG)
@@ -781,12 +781,12 @@ export default class MagicString {
781781
this._split(start)
782782
this._split(end)
783783

784-
let chunk = this.byStart[start]
784+
let chunk = this.byStart.get(start)
785785

786786
while (chunk) {
787787
chunk.reset()
788788

789-
chunk = end > chunk.end ? this.byStart[chunk.end] : null
789+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null
790790
}
791791

792792
if (DEBUG)
@@ -919,7 +919,7 @@ export default class MagicString {
919919

920920
/** @internal */
921921
_split(index: number): boolean | void {
922-
if (this.byStart[index] || this.byEnd[index])
922+
if (this.byStart.get(index) || this.byEnd.get(index))
923923
return
924924

925925
if (DEBUG)
@@ -933,7 +933,7 @@ export default class MagicString {
933933
if (chunk.contains(index))
934934
return this._splitChunk(chunk, index)
935935

936-
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start]
936+
chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start)
937937

938938
// Prevent infinite loop (e.g. via empty chunks, where start === end)
939939
if (chunk === previousChunk)
@@ -955,9 +955,9 @@ export default class MagicString {
955955

956956
const newChunk = chunk.split(index)
957957

958-
this.byEnd[index] = chunk
959-
this.byStart[index] = newChunk
960-
this.byEnd[newChunk.end] = newChunk
958+
this.byEnd.set(index, chunk)
959+
this.byStart.set(index, newChunk)
960+
this.byEnd.set(newChunk.end, newChunk)
961961

962962
if (chunk === this.lastChunk)
963963
this.lastChunk = newChunk
@@ -1045,9 +1045,9 @@ export default class MagicString {
10451045
this.lastChunk = chunk.next
10461046
}
10471047

1048-
this.byEnd[chunk.end] = chunk
1049-
this.byStart[chunk.next.start] = chunk.next
1050-
this.byEnd[chunk.next.end] = chunk.next
1048+
this.byEnd.set(chunk.end, chunk)
1049+
this.byStart.set(chunk.next.start, chunk.next)
1050+
this.byEnd.set(chunk.next.end, chunk.next)
10511051
}
10521052

10531053
if (aborted)
@@ -1085,9 +1085,9 @@ export default class MagicString {
10851085
if (chunk === this.lastChunk)
10861086
this.lastChunk = chunk.next
10871087

1088-
this.byEnd[chunk.end] = chunk
1089-
this.byStart[chunk.next.start] = chunk.next
1090-
this.byEnd[chunk.next.end] = chunk.next
1088+
this.byEnd.set(chunk.end, chunk)
1089+
this.byStart.set(chunk.next.start, chunk.next)
1090+
this.byEnd.set(chunk.next.end, chunk.next)
10911091
}
10921092

10931093
if (aborted)

0 commit comments

Comments
 (0)