Permalink
Browse files

Use literal notation for creation arrays/objects.

Cache array length when looping.
  • Loading branch information...
1 parent 97bd115 commit 3fd877b137c84c41061f5babb168b1655292e19f @scottgonzalez scottgonzalez committed May 5, 2010
Showing with 17 additions and 17 deletions.
  1. +17 −17 lib/JSLINQ.js
View
34 lib/JSLINQ.js
@@ -20,30 +20,30 @@
ToArray: function() { return this.items; },
Where: function(clause) {
- var newArray = new Array();
+ var newArray = [];
// The clause was passed in as a Method that return a Boolean
- for (var index = 0; index < this.items.length; index++) {
+ for (var index = 0, length = this.items.length; index < length; index++) {
if (clause(this.items[index], index)) {
newArray[newArray.length] = this.items[index];
}
}
return new JSLINQ(newArray);
},
Select: function(clause) {
- var newArray = new Array();
+ var newArray = [];
// The clause was passed in as a Method that returns a Value
- for (var i = 0; i < this.items.length; i++) {
+ for (var i = 0, length = this.items.length; i < length; i++) {
if (clause(this.items[i])) {
newArray[newArray.length] = clause(this.items[i]);
}
}
return new JSLINQ(newArray);
},
OrderBy: function(clause) {
- var tempArray = new Array();
- for (var i = 0; i < this.items.length; i++) {
+ var tempArray = [];
+ for (var i = 0, length = this.items.length; i < length; i++) {
tempArray[tempArray.length] = this.items[i];
}
return new JSLINQ(
@@ -55,8 +55,8 @@
);
},
OrderByDescending: function(clause) {
- var tempArray = new Array();
- for (var i = 0; i < this.items.length; i++) {
+ var tempArray = [];
+ for (var i = 0, length = this.items.length; i < length; i++) {
tempArray[tempArray.length] = this.items[i];
}
return new JSLINQ(
@@ -68,8 +68,8 @@
);
},
SelectMany: function(clause) {
- var r = new Array();
- for (var i = 0; i < this.items.length; i++) {
+ var r = [];
+ for (var i = 0, length = this.items.length; i < length; i++) {
r = r.concat(clause(this.items[i]));
}
return new JSLINQ(r);
@@ -82,9 +82,9 @@
},
Distinct: function(clause) {
var item;
- var dict = new Object();
- var retVal = new Array();
- for (var i = 0; i < this.items.length; i++) {
+ var dict = {};
+ var retVal = [];
+ for (var i = 0, length = this.items.length; i < length; i++) {
item = clause(this.items[i]);
// TODO - This doens't correctly compare Objects. Need to fix this
if (dict[item] == null) {
@@ -96,19 +96,19 @@
return new JSLINQ(retVal);
},
Any: function(clause) {
- for (var index = 0; index < this.items.length; index++) {
+ for (var index = 0, length = this.items.length; index < length; index++) {
if (clause(this.items[index], index)) { return true; }
}
return false;
},
All: function(clause) {
- for (var index = 0; index < this.items.length; index++) {
+ for (var index = 0, length = this.items.length; index < length; index++) {
if (!clause(this.items[index], index)) { return false; }
}
return true;
},
Reverse: function() {
- var retVal = new Array();
+ var retVal = [];
for (var index = this.items.length - 1; index > -1; index--)
retVal[retVal.length] = this.items[index];
return new JSLINQ(retVal);
@@ -154,7 +154,7 @@
var sa = secondArray.items || secondArray;
- var result = new Array();
+ var result = [];
for (var a = 0; a < this.items.length; a++) {
for (var b = 0; b < sa.length; b++) {
if (clauseMethod(this.items[a], a, sa[b], b)) {

0 comments on commit 3fd877b

Please sign in to comment.