Permalink
Browse files

inArray -> indexOf ...

  • Loading branch information...
1 parent 35b6a70 commit c1d24944dc6918ac36dff3a34e89b3130566daa0 @jashkenas committed Oct 20, 2010
Showing with 19 additions and 13 deletions.
  1. +9 −2 lib/nodes.js
  2. +10 −11 src/nodes.coffee
View
@@ -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'
};
View
@@ -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;
@jdalton
jdalton Oct 14, 2011

Should prolly be something like:

for (var i = 0, l = this.length >>> 0; i < l; i++) if (i in this && this[i] === item) return i;

Array comprehensions have similar issues with sparse arrays [as compared to JavaScript 1.7](https://developer.mozilla.org/en/New_in_JavaScript_1.7#Array_comprehensions_(Merge_into_Array_comprehensions\)).

@michaelficarra
michaelficarra Oct 14, 2011 Collaborator

I agree. @jdalton: mind opening up an issue to make sure this doesn't get lost?

+ return -1;
+ }
'''
# Shortcuts to speed up the lookup time for native functions.

2 comments on commit c1d2494

@michaelficarra
Collaborator

I prefer Tesco's commit. He doesn't require a .call at every call point and he pushes the > -1 or >= 0 check inside the function rather than including it at every call point as well.

@jashkenas
Owner

That included inArray function was just a little big and hairy... If you compiled this:

a in b

And got all that at the top, it would be frightening. It also doesn't remove the ".call" ... it just pushes it into the function, so there's actually an additional function invocation each time...

Please sign in to comment.