forked from Rich-Harris/magic-string
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce memory overhead of sourcemapLocations
It was using an object to store positions in the string. This meant that those offsets were first stringified and then added as object properties. For a 200K JS code file (as parsed by rollup), the sourcemapLocations object was taking up 980K. Switching it to a BitSet that uses one bit per offset makes it take up 36K instead. Fixes Rich-Harris#167
- Loading branch information
Showing
3 changed files
with
20 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters