Skip to content

Commit

Permalink
Merge pull request #40 from jevancc/performance-improvement
Browse files Browse the repository at this point in the history
Improvements on OriginalSource and ReplaceSource
  • Loading branch information
sokra authored Sep 20, 2018
2 parents 9d14ed4 + 54e8033 commit 931d7ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 53 deletions.
26 changes: 2 additions & 24 deletions lib/OriginalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,10 @@ var SourceMapConsumer = require("source-map").SourceMapConsumer;
var SourceListMap = require("source-list-map").SourceListMap;
var Source = require("./Source");

function isSplitter(c) {
switch(c) {
case 10: // \n
case 13: // \r
case 59: // ;
case 123: // {
case 125: // }
return true;
}
return false;
}
var SPLIT_REGEX = /(?!$)[^\n\r;{}]*[\n\r;{}]*/g;

function _splitCode(code) {
var result = [];
var i = 0;
var j = 0;
for(; i < code.length; i++) {
if(isSplitter(code.charCodeAt(i))) {
while(isSplitter(code.charCodeAt(++i)));
result.push(code.substring(j, i));
j = i;
}
}
if(j < code.length)
result.push(code.substr(j));
return result;
return code.match(SPLIT_REGEX) || [];
}

class OriginalSource extends Source {
Expand Down
50 changes: 21 additions & 29 deletions lib/ReplaceSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ReplaceSource extends Source {
if(this.replacements.length === 0) {
return node;
}
this.replacements.sort(sortReplacementsAscending);
this._sortReplacements();
var replace = new ReplacementEnumerator(this.replacements);
var output = [];
var position = 0;
Expand Down Expand Up @@ -270,32 +270,17 @@ class ReplaceSource extends Source {
}
}

function sortReplacementsAscending(a, b) {
var diff = a.end - b.end;
if(diff !== 0)
return diff;
diff = a.start - b.start;
if(diff !== 0)
return diff;
return a.insertIndex - b.insertIndex;
}

class ReplacementEnumerator {
/**
* @param {Replacement[]} replacements list of replacements
*/
constructor(replacements) {
this.emit = true;
this.done = !replacements || replacements.length === 0;
this.index = 0;
this.replacements = replacements;
if(!this.done) {
// Set initial start position in case .header is not called
var repl = replacements[0];
this.position = Math.floor(repl.start);
if(this.position < 0)
this.position = 0;
}
this.replacements = replacements || [];
this.index = this.replacements.length;
this.done = false;
this.emit = false;
// Set initial start position
this.next();
}

next() {
Expand All @@ -310,8 +295,8 @@ class ReplacementEnumerator {
this.name = repl.name;
} else {
// End point found. start emitting. set position to find next start
this.index++;
if(this.index >= this.replacements.length) {
this.index--;
if(this.index < 0) {
this.done = true;
} else {
var nextRepl = this.replacements[this.index];
Expand All @@ -328,11 +313,18 @@ class ReplacementEnumerator {
footer() {
if(!this.done && !this.emit)
this.next(); // If we finished _replaceInNode mid emit we advance to next entry
return this.done ? [] : this.replacements.slice(this.index).map(function(repl) {
// this doesn't need to handle repl.name, because in SourceMaps generated code
// without pointer to original source can't have a name
return repl.content;
}).join("");
if(this.done) {
return [];
} else {
var resultStr = "";
for(var i = this.index; i >= 0; i--) {
var repl = this.replacements[i];
// this doesn't need to handle repl.name, because in SourceMaps generated code
// without pointer to original source can't have a name
resultStr += repl.content;
}
return resultStr;
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions test/OriginalSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var should = require("should");
var OriginalSource = require("../lib/OriginalSource");

describe("OriginalSource", function() {
it("should handle multiline string", function() {
var source = new OriginalSource("Line1\n\nLine3\n", "file.js");
var resultText = source.source();
var resultMap = source.sourceAndMap({
columns: true
});
var resultListMap = source.sourceAndMap({
columns: false
});

resultText.should.be.eql("Line1\n\nLine3\n");
resultMap.source.should.be.eql(resultText);
resultListMap.source.should.be.eql(resultText);
resultListMap.map.file.should.be.eql(resultMap.map.file);
resultListMap.map.version.should.be.eql(resultMap.map.version);
resultMap.map.sources.should.be.eql(["file.js"]);
resultListMap.map.sources.should.be.eql(resultMap.map.sources);
resultMap.map.sourcesContent.should.be.eql(["Line1\n\nLine3\n"]);
resultListMap.map.sourcesContent.should.be.eql(resultMap.map.sourcesContent);
resultMap.map.mappings.should.be.eql("AAAA;;AAEA");
resultListMap.map.mappings.should.be.eql("AAAA;AACA;AACA;");
});

it("should handle empty string", function() {
var source = new OriginalSource("", "file.js");
var resultText = source.source();
var resultMap = source.sourceAndMap({
columns: true
});
var resultListMap = source.sourceAndMap({
columns: false
});

resultText.should.be.eql("");
resultMap.source.should.be.eql(resultText);
resultListMap.source.should.be.eql(resultText);
resultListMap.map.file.should.be.eql(resultMap.map.file);
resultListMap.map.version.should.be.eql(resultMap.map.version);
resultMap.map.sources.should.be.eql([]);
resultListMap.map.sources.should.be.eql(resultMap.map.sources);
resultMap.map.mappings.should.be.eql("");
resultListMap.map.mappings.should.be.eql("");
});

it("should omit mappings for columns with node", function() {
var source = new OriginalSource("Line1\n\nLine3\n", "file.js");
var resultMap = source.node({
columns: false
}).toStringWithSourceMap({
file: "x"
}).map.toJSON();

resultMap.mappings.should.be.eql("AAAA;AACA;AACA");
});
});

0 comments on commit 931d7ea

Please sign in to comment.