diff --git a/lib/nodes.js b/lib/nodes.js index 026e7cfd..2fb2d273 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1480,7 +1480,14 @@ return "(" + (tests.join(' || ')) + ")"; }; In.prototype.compileLoopTest = function(o) { - return "" + (utility('inArray')) + "(" + (this.object.compile(o)) + ", " + (this.array.compile(o)) + ")"; + var _ref2, obj1, obj2, prefix; + _ref2 = this.object.compileReference(merge(o, { + top: true + }), { + precompile: true + }), obj1 = _ref2[0], obj2 = _ref2[1]; + prefix = obj1 !== obj2 ? ("" + obj1 + ", ") : ''; + return "(" + prefix + (utility('indexOf')) + ".call(" + (this.array.compile(o)) + ", " + obj2 + ") >= 0)"; }; return In; })(); @@ -1946,7 +1953,7 @@ UTILITIES = { "extends": 'function(child, parent) {\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n}', bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}', - inArray: '(function() {\n var indexOf = Array.prototype.indexOf || function(item) {\n var i = this.length; while (i--) if (this[i] === item) return i;\n return -1;\n };\n return function(item, array) { return indexOf.call(array, item) > -1; };\n})()', + indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) if (this[i] === item) return i;\n return -1;\n}', hasProp: 'Object.prototype.hasOwnProperty', slice: 'Array.prototype.slice' }; diff --git a/src/nodes.coffee b/src/nodes.coffee index 2349727d..0730717c 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1262,7 +1262,9 @@ exports.In = class In extends Base "(#{tests.join(' || ')})" compileLoopTest: (o) -> - "#{utility 'inArray'}(#{@object.compile o}, #{@array.compile o})" + [obj1, obj2] = @object.compileReference merge(o, top: yes), precompile: yes + prefix = if obj1 isnt obj2 then "#{obj1}, " else '' + "(#{prefix}#{utility 'indexOf'}.call(#{@array.compile o}, #{obj2}) >= 0)" #### Try @@ -1462,7 +1464,7 @@ exports.For = class For extends Base unstepPart = "\n#{@tab}" + unstepPart if @object forPart = "#{ivar} in #{sourcePart}" - guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw + guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw body = body.compile merge o, indent: idt1, top: true vars = if range then name else "#{name}, #{ivar}" """ @@ -1663,16 +1665,13 @@ UTILITIES = return function() { return func.apply(context, arguments); }; } ''' - + # Discover if an item is in an array. - inArray: ''' - (function() { - var indexOf = Array.prototype.indexOf || function(item) { - var i = this.length; while (i--) if (this[i] === item) return i; - return -1; - }; - return function(item, array) { return indexOf.call(array, item) > -1; }; - })() + indexOf: ''' + Array.prototype.indexOf || function(item) { + for (var i = 0, l = this.length; i < l; i++) if (this[i] === item) return i; + return -1; + } ''' # Shortcuts to speed up the lookup time for native functions.