Skip to content

Commit 264fcb4

Browse files
authored
Merge pull request #300 from cspotcode/feature/performance-improvements
Sort mappings on-demand to greatly improve performance
2 parents 2f17c63 + b09169a commit 264fcb4

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

lib/source-map-consumer.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', {
6767
enumerable: true,
6868
get: function () {
6969
if (!this.__generatedMappings) {
70-
this._parseMappings(this._mappings, this.sourceRoot);
70+
this._sortGeneratedMappings();
7171
}
7272

7373
return this.__generatedMappings;
@@ -80,13 +80,39 @@ Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', {
8080
enumerable: true,
8181
get: function () {
8282
if (!this.__originalMappings) {
83-
this._parseMappings(this._mappings, this.sourceRoot);
83+
this._sortOriginalMappings();
8484
}
8585

8686
return this.__originalMappings;
8787
}
8888
});
8989

90+
SourceMapConsumer.prototype.__generatedMappingsUnsorted = null;
91+
Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappingsUnsorted', {
92+
configurable: true,
93+
enumerable: true,
94+
get: function () {
95+
if (!this.__generatedMappingsUnsorted) {
96+
this._parseMappings(this._mappings, this.sourceRoot);
97+
}
98+
99+
return this.__generatedMappingsUnsorted;
100+
}
101+
});
102+
103+
SourceMapConsumer.prototype.__originalMappingsUnsorted = null;
104+
Object.defineProperty(SourceMapConsumer.prototype, '_originalMappingsUnsorted', {
105+
configurable: true,
106+
enumerable: true,
107+
get: function () {
108+
if (!this.__originalMappingsUnsorted) {
109+
this._parseMappings(this._mappings, this.sourceRoot);
110+
}
111+
112+
return this.__originalMappingsUnsorted;
113+
}
114+
});
115+
90116
SourceMapConsumer.prototype._charIsMappingSeparator =
91117
function SourceMapConsumer_charIsMappingSeparator(aStr, index) {
92118
var c = aStr.charAt(index);
@@ -103,6 +129,20 @@ SourceMapConsumer.prototype._parseMappings =
103129
throw new Error("Subclasses must implement _parseMappings");
104130
};
105131

132+
SourceMapConsumer.prototype._sortGeneratedMappings =
133+
function SourceMapConsumer_sortGeneratedMappings() {
134+
const mappings = this._generatedMappingsUnsorted;
135+
quickSort(mappings, util.compareByGeneratedPositionsDeflated);
136+
this.__generatedMappings = mappings;
137+
};
138+
139+
SourceMapConsumer.prototype._sortOriginalMappings =
140+
function SourceMapConsumer_sortOriginalMappings() {
141+
const mappings = this._originalMappingsUnsorted;
142+
quickSort(mappings, util.compareByOriginalPositions);
143+
this.__originalMappings = mappings;
144+
};
145+
106146
SourceMapConsumer.GENERATED_ORDER = 1;
107147
SourceMapConsumer.ORIGINAL_ORDER = 2;
108148

@@ -564,11 +604,9 @@ BasicSourceMapConsumer.prototype._parseMappings =
564604
}
565605
}
566606

567-
quickSort(generatedMappings, util.compareByGeneratedPositionsDeflated);
568-
this.__generatedMappings = generatedMappings;
607+
this.__generatedMappingsUnsorted = generatedMappings;
569608

570-
quickSort(originalMappings, util.compareByOriginalPositions);
571-
this.__originalMappings = originalMappings;
609+
this.__originalMappingsUnsorted = originalMappings;
572610
};
573611

574612
/**
@@ -1097,8 +1135,8 @@ IndexedSourceMapConsumer.prototype.generatedPositionFor =
10971135
*/
10981136
IndexedSourceMapConsumer.prototype._parseMappings =
10991137
function IndexedSourceMapConsumer_parseMappings(aStr, aSourceRoot) {
1100-
this.__generatedMappings = [];
1101-
this.__originalMappings = [];
1138+
const generatedMappings = this.__generatedMappingsUnsorted = [];
1139+
const originalMappings = this.__originalMappingsUnsorted = [];
11021140
for (var i = 0; i < this._sections.length; i++) {
11031141
var section = this._sections[i];
11041142
var sectionMappings = section.consumer._generatedMappings;
@@ -1134,15 +1172,12 @@ IndexedSourceMapConsumer.prototype._parseMappings =
11341172
name: name
11351173
};
11361174

1137-
this.__generatedMappings.push(adjustedMapping);
1175+
generatedMappings.push(adjustedMapping);
11381176
if (typeof adjustedMapping.originalLine === 'number') {
1139-
this.__originalMappings.push(adjustedMapping);
1177+
originalMappings.push(adjustedMapping);
11401178
}
11411179
}
11421180
}
1143-
1144-
quickSort(this.__generatedMappings, util.compareByGeneratedPositionsDeflated);
1145-
quickSort(this.__originalMappings, util.compareByOriginalPositions);
11461181
};
11471182

11481183
exports.IndexedSourceMapConsumer = IndexedSourceMapConsumer;

0 commit comments

Comments
 (0)