Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Last active March 1, 2018 03:03
Show Gist options
  • Save Gaubee/5945554 to your computer and use it in GitHub Desktop.
Save Gaubee/5945554 to your computer and use it in GitHub Desktop.
司徒正美的解析和abcd的解析比较测试。
(function(){
var KEYWORDS = "abstract,boolean,byte,char,double,final,float,goto,implements,int,interface,long,native,package,private,protected,public,short,static,synchronized,throws,transient,volatile,arguments,let,yield";
var NO_VAR_KEYWORDS={};
var REMOVE_RE = /\/\*(?:.|\n)*?\*\/|\/\/[^\n]*\n|\/\/[^\n]*$|'[^']*'|"[^"]*"|[\s\t\n]*\.[\s\t\n]*[$\w\.]+/g;
var SPLIT_RE = /[^\w$]+/g;
var KEYWORDS_RE = new RegExp(["\\b" + KEYWORDS.replace(/,/g, '\\b|\\b') + "\\b"].join('|'), 'g');
var NUMBER_RE = /\b\d[^,]*/g;
var BOUNDARY_RE = /^,+|,+$/g;
var getVariables = this.g1 = function(code) {
code = code
.replace(REMOVE_RE, '')
.replace(SPLIT_RE, ',')
.replace(KEYWORDS_RE, '')
.replace(NUMBER_RE, '')
.replace(BOUNDARY_RE, '');
code = code ? code.split(/,+/) : [];
for(var i = 1,Len = code.length,filterCodeArray = [],filterCodeHash = {},arg;i<Len;i+=1){//过滤关键字与重复出现的变量
arg = code[i];
if (!filterCodeHash[arg]&&!NO_VAR_KEYWORDS[arg]) {
try{
(function(k){
eval("var "+k);
}(arg));
}catch(e){
NO_VAR_KEYWORDS[arg] = 1;
continue;
}
filterCodeArray.push(arg);
filterCodeHash[arg] = 1;
}
}
return filterCodeArray;
};
}());
(function(){
var rword = /[^, ]+/g
function oneObject(array, val) {
if (typeof array === "string") {
array = array.match(rword) || []
}
var result = {},
value = val !== void 0 ? val : 1
for (var i = 0, n = array.length; i < n; i++) {
result[array[i]] = value
}
return result
}
this.g2 = function getVars(s) {
var s_kws = " break case catch continue default delete do else finally for function if in instanceof new return switch this throw try typeof var void while with null true false"
+ " abstract boolean byte char class const debugger double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile ";
var keywords = oneObject(s_kws)
var re_ws = /\s+/;
var re_comm = /\/\/[^\r\n\u2028\u2029]*|\/\*(?:\/|\**[^*/])*\*+\//;
var re_id = /[a-zA-Z_$][\w$]*/;
var re_punc = /~|\}|\|\||\|=|\||\{|\^=|\^|\]|\[|\?|>>>=|>>>|>>=|>>|>=|>|===|==|=|<=|<<=|<<|<|;|:|\.|-=|--|-|,|\+=|\+\+|\+|\*=|\*|\)|\(|&=|&&|&|%=|%|!==|!=|!|\/=?/;
var re_num = /0x[\dA-Fa-f]+|(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/;
var re_str = /"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'/;
var re = function(a) {
for (var i = 0; i < a.length; ++i)
a[i] = '(' + a[i].source + ')';
return RegExp(a.join('|') + '|', 'g');
}([re_ws, re_comm, re_id, re_punc, re_num, re_str]);
var beforeIsDot = false;
var r = [];
var rCatch = {};
var t;
function f(all, ws, comm, id, punc, num, str) {
if (beforeIsDot) {
beforeIsDot = false;
}
else {
if (all == '.') {
beforeIsDot = true;
}
else {
if (id && !keywords[id] && !rCatch[id]){
r.push(id);
rCatch[id] = true;
}
}
}
}
while (t = re.exec(s)) {
if (!t[0]) {
if (t.index != s.length)
throw Error("error: " + t.index);
break;
}
f.apply(null, t);
}
return r;
}
}());
code = g1+"";
console.log(g1(code));
console.log(g2(code));
console.time("RubyLouvre")
var time = 1000;
do{
g1(code);
}while(time--);
console.timeEnd("RubyLouvre")
console.time("abcd")
var time = 1000;
do{
g2(code);
}while(time--);
console.timeEnd("abcd");
code = g2+"";
console.log(g1(code));
console.log(g2(code));//BOOM!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment