Skip to content
Browse files

Improves performance of sortUnique()

  • Loading branch information...
1 parent f09221d commit 84a543e7c20789e8e5b0f41881aeb9c0d07855e0 @deliminator deliminator committed Mar 7, 2012
Showing with 4 additions and 8 deletions.
  1. +4 −8 src/lib/util/lang.js
View
12 src/lib/util/lang.js
@@ -38,21 +38,17 @@ define( "util/lang", [], function(){
sorted.sort();
}
- var result = new Array( sorted.length );
- var lastValue = sorted[ 0 ]; // array.length >= 1 checked above
- result[ 0 ] = lastValue;
-
- var j = 1;
+ var j = 0;
var len = sorted.length;
for ( var i = 1; i < len; i++ ) {
var value = sorted[ i ];
// Use loosely typed comparsion if no compFunc is given
// to avoid sortUnique( [6, "6", 6] ) => [6, "6", 6]
- if ( compFunc ? 0 !== compFunc( lastValue, value ) : lastValue != value ) {
- lastValue = result[ j++ ] = value;
+ if ( compFunc ? 0 !== compFunc( sorted[ j ], value ) : sorted[ j ] != value ) {
+ sorted[ ++j ] = value;
}
}
- return result.slice(0, j);
+ return sorted.slice(0, j + 1);
}
};
} );

0 comments on commit 84a543e

Please sign in to comment.
Something went wrong with that request. Please try again.