diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index e26731db4..4e22b591a 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -490,10 +490,11 @@ */ - (unsigned)indexOfObject:(id)anObject sortedByDescriptors:(CPArray)descriptors { + var count = [descriptors count]; + return [self indexOfObject:anObject sortedByFunction:function(lhs, rhs) { var i = 0, - count = [descriptors count], result = CPOrderedSame; while (i < count) @@ -971,21 +972,22 @@ - (unsigned)insertObject:(id)anObject inArraySortedByDescriptors:(CPArray)descriptors { + var count = [descriptors count]; + var index = [self _indexOfObject:anObject sortedByFunction:function(lhs, rhs) { var i = 0, - count = [descriptors count], result = CPOrderedSame; while (i < count) - if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame) + if((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame) return result; return result; } context:nil]; if (index < 0) - index = -index - 1; + index = -index-1; [self insertObject:anObject atIndex:index]; return index; @@ -1191,10 +1193,11 @@ - (CPArray)sortUsingDescriptors:(CPArray)descriptors { + var count = [descriptors count]; + sort(function(lhs, rhs) { var i = 0, - count = [descriptors count], result = CPOrderedSame; while (i < count) diff --git a/Tests/Foundation/CPArrayPerformance.j b/Tests/Foundation/CPArrayPerformance.j new file mode 100644 index 000000000..a41c16dce --- /dev/null +++ b/Tests/Foundation/CPArrayPerformance.j @@ -0,0 +1,56 @@ +@import +@import +@import +@import + +@implementation CPArrayPerformance : OJTestCase + +- (void)testSortUsingDescriptorsSpeed +{ + + var ELEMENTS = 1000, + REPEATS = 10, + array = []; + for (var i=0; i