Skip to content

Instantly share code, notes, and snippets.

@creynders
Created March 7, 2013 18:41
Show Gist options
  • Save creynders/5110583 to your computer and use it in GitHub Desktop.
Save creynders/5110583 to your computer and use it in GitHub Desktop.
package arrayDuplicates
{
import flash.utils.getTimer;
/**
* @author creynder
*/
public class ArrayDuplicates{
public function ArrayDuplicates()
{
var list : Array = generateArray();
trace( '-- run --' );
var l1 : Array = list.concat();
var t1 : Number = getTimer();
var o1 : Array = methA( l1 );
trace( getTimer() - t1 );
var l2 : Array = list.concat();
var t2 : Number = getTimer();
var o2 : Array = methB( l2 );
trace( getTimer() - t2 );
}
public function generateArray() : Array{
var output : Array = [];
var i : int;
var n : int = 100000;
for ( i = 0; i<n; i++ ){
output.push( Math.floor( Math.random() * 12 ) );
}
return output;
}
public function methA( list : Array ) : Array{
list = list.sort( Array.NUMERIC );
for ( var i = list.length -1; 1 <= i; --i )
{
if ( list[i-1] == list[i] )
{
list.splice(i-1,1);
}
}
return list;
}
public function methB( list : Array ) : Array{
return list.filter( myFilter );
}
function myFilter(element:*, index:int, arr:Array):Boolean {
return (index==arr.lastIndexOf(element));
}
}
}
-- run --
3646
161
-- run --
3696
129
-- run --
3696
129
-- run --
3968
111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment