diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-04-27 04:30:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-27 04:30:29 +0800 |
commit | 838f83737904f3067b2e0585034b2f71731d901d (patch) | |
tree | 7fdc2e12c9880e0e5839faee950c15e27d0b3300 /lib/utils.js | |
parent | 82a8b6f6122d7fe887619138b6913475ce0f2093 (diff) | |
download | tracifyjs-838f83737904f3067b2e0585034b2f71731d901d.tar.gz tracifyjs-838f83737904f3067b2e0585034b2f71731d901d.zip |
improve general performance (#3104)
Diffstat (limited to 'lib/utils.js')
-rw-r--r-- | lib/utils.js | 96 |
1 files changed, 34 insertions, 62 deletions
diff --git a/lib/utils.js b/lib/utils.js index 7e99f0d9..d4d28348 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -45,27 +45,25 @@ function characters(str) { return str.split(""); -}; +} function member(name, array) { return array.indexOf(name) >= 0; -}; +} function find_if(func, array) { for (var i = 0, n = array.length; i < n; ++i) { - if (func(array[i])) - return array[i]; + if (func(array[i])) return array[i]; } -}; +} function repeat_string(str, i) { if (i <= 0) return ""; if (i == 1) return str; var d = repeat_string(str, i >> 1); d += d; - if (i & 1) d += str; - return d; -}; + return i & 1 ? d + str : d; +} function configure_error_stack(fn) { Object.defineProperty(fn.prototype, "stack", { @@ -84,27 +82,23 @@ function configure_error_stack(fn) { function DefaultsError(msg, defs) { this.message = msg; this.defs = defs; -}; +} DefaultsError.prototype = Object.create(Error.prototype); DefaultsError.prototype.constructor = DefaultsError; DefaultsError.prototype.name = "DefaultsError"; configure_error_stack(DefaultsError); -DefaultsError.croak = function(msg, defs) { - throw new DefaultsError(msg, defs); -}; - function defaults(args, defs, croak) { - if (args === true) - args = {}; + if (args === true) args = {}; var ret = args || {}; - if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) - DefaultsError.croak("`" + i + "` is not a supported option", defs); + if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) { + throw new DefaultsError("`" + i + "` is not a supported option", defs); + } for (var i in defs) if (HOP(defs, i)) { ret[i] = (args && HOP(args, i)) ? args[i] : defs[i]; } return ret; -}; +} function merge(obj, ext) { var count = 0; @@ -113,7 +107,7 @@ function merge(obj, ext) { count++; } return count; -}; +} function noop() {} function return_false() { return false; } @@ -172,43 +166,19 @@ var MAP = (function(){ function push_uniq(array, el) { if (array.indexOf(el) < 0) array.push(el); -}; +} function string_template(text, props) { return text.replace(/\{(.+?)\}/g, function(str, p){ return props && props[p]; }); -}; +} function remove(array, el) { for (var i = array.length; --i >= 0;) { if (array[i] === el) array.splice(i, 1); } -}; - -function mergeSort(array, cmp) { - if (array.length < 2) return array.slice(); - function merge(a, b) { - var r = [], ai = 0, bi = 0, i = 0; - while (ai < a.length && bi < b.length) { - cmp(a[ai], b[bi]) <= 0 - ? r[i++] = a[ai++] - : r[i++] = b[bi++]; - } - if (ai < a.length) r.push.apply(r, a.slice(ai)); - if (bi < b.length) r.push.apply(r, b.slice(bi)); - return r; - }; - function _ms(a) { - if (a.length <= 1) - return a; - var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m); - left = _ms(left); - right = _ms(right); - return merge(left, right); - }; - return _ms(array); -}; +} function makePredicate(words) { if (!Array.isArray(words)) words = words.split(" "); @@ -224,12 +194,12 @@ function all(array, predicate) { if (!predicate(array[i])) return false; return true; -}; +} function Dictionary() { this._values = Object.create(null); this._size = 0; -}; +} Dictionary.prototype = { set: function(key, val) { if (!this.has(key)) ++this._size; @@ -290,20 +260,22 @@ function HOP(obj, prop) { // a statement. function first_in_statement(stack) { var node = stack.parent(-1); - for (var i = 0, p; p = stack.parent(i); i++) { - if (p instanceof AST_Statement && p.body === node) - return true; - if ((p instanceof AST_Sequence && p.expressions[0] === node) || - (p.TYPE == "Call" && p.expression === node ) || - (p instanceof AST_Dot && p.expression === node ) || - (p instanceof AST_Sub && p.expression === node ) || - (p instanceof AST_Conditional && p.condition === node ) || - (p instanceof AST_Binary && p.left === node ) || - (p instanceof AST_UnaryPostfix && p.expression === node )) - { - node = p; - } else { - return false; + for (var i = 0, p; p = stack.parent(i++); node = p) { + if (p.TYPE == "Call") { + if (p.expression === node) continue; + } else if (p instanceof AST_Binary) { + if (p.left === node) continue; + } else if (p instanceof AST_Conditional) { + if (p.condition === node) continue; + } else if (p instanceof AST_PropAccess) { + if (p.expression === node) continue; + } else if (p instanceof AST_Sequence) { + if (p.expressions[0] === node) continue; + } else if (p instanceof AST_Statement) { + return p.body === node; + } else if (p instanceof AST_UnaryPostfix) { + if (p.expression === node) continue; } + return false; } } |