diff --git a/src/BitSet.js b/src/BitSet.js new file mode 100644 index 0000000..eccd909 --- /dev/null +++ b/src/BitSet.js @@ -0,0 +1,15 @@ +export default class BitSet { + constructor(arg) { + this.bits = arg instanceof BitSet ? arg.bits.slice() : []; + } + + add(n) { + this.bits[Math.floor(n / BITS)] |= 1 << n % BITS; + } + + has(n) { + return !!(this.bits[Math.floor(n / BITS)] & (1 << n % BITS)); + } +} + +const BITS = 32; diff --git a/src/MagicString.js b/src/MagicString.js index 18bf1f4..14f5cb2 100644 --- a/src/MagicString.js +++ b/src/MagicString.js @@ -1,3 +1,4 @@ +import BitSet from './BitSet.js'; import Chunk from './Chunk.js'; import SourceMap from './SourceMap.js'; import guessIndent from './utils/guessIndent.js'; @@ -30,7 +31,7 @@ export default class MagicString { byEnd: { writable: true, value: {} }, filename: { writable: true, value: options.filename }, indentExclusionRanges: { writable: true, value: options.indentExclusionRanges }, - sourcemapLocations: { writable: true, value: {} }, + sourcemapLocations: { writable: true, value: new BitSet() }, storedNames: { writable: true, value: {} }, indentStr: { writable: true, value: guessIndent(string) } }); @@ -44,7 +45,7 @@ export default class MagicString { } addSourcemapLocation(char) { - this.sourcemapLocations[char] = true; + this.sourcemapLocations.add(char); } append(content) { @@ -121,9 +122,7 @@ export default class MagicString { cloned.indentExclusionRanges = this.indentExclusionRanges.slice(); } - Object.keys(this.sourcemapLocations).forEach(loc => { - cloned.sourcemapLocations[loc] = true; - }); + cloned.sourcemapLocations = new BitSet(this.sourcemapLocations); cloned.intro = this.intro; cloned.outro = this.outro; diff --git a/src/utils/Mappings.js b/src/utils/Mappings.js index 6026200..7de80d9 100644 --- a/src/utils/Mappings.js +++ b/src/utils/Mappings.js @@ -28,7 +28,7 @@ export default class Mappings { let first = true; while (originalCharIndex < chunk.end) { - if (this.hires || first || sourcemapLocations[originalCharIndex]) { + if (this.hires || first || sourcemapLocations.has(originalCharIndex)) { this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]); }