Array.unique
JavaScript performance comparison
Info
Testing 2 functions to remove duplicates in Array.
The 1st function Array.unique remove deuplicates by simply using a nested for loop
The 2nd function Array.unique2 uses removes duplicates by using Array.indexOf
Preparation code
<script>
/**************************************************************************************************
Search an element in the array and returns its index if found or -1 if not found.
FF 1.5+ already implements this method. Using the native method is definitely faster therefor is better to add an if test to see if the methos is already defined.
[sources: https://developer.mozilla.org/en/New_in_JavaScript_1.6
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf#Compatibility]
**************************************************************************************************/
if (!Array.prototype.indexOf) Array.prototype.indexOf = function(val) {
//Reversed loop are much faster [source: http://stackoverflow.com/questions/3520688/javascript-loop-performance-why-is-to-decrement-the-iterator-toward-0-faster-th/ and http://blogs.sun.com/greimer/entry/best_way_to_code_a]
var i = this.length;
while (i--)
if (this[i] === val) return i;
return -1;
}
/**************************************************************************************************
Returns a new array where all duplicates has been removed.
On sortable arrays there are faster ways to remove duplicates [ref. http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array/840812#840812], but this is a generic function and the array is not necessarely sortable (i.e. an array of HTML elements, how do you sort it).
**************************************************************************************************/
Array.prototype.unique = function() {
var result = [],
i, j, l1 = this.length,
l2, dupe;
//Adding 1st element to result, than one by one all other elements. Before adding each element we check that is not already present in result.
for (i = 0; i < l1; ++i) {
dupe = false;
for (j = 0, l2 = result.length; j < l2; ++j) {
//If dupe element is found we don't add the element to the result.
if (result[j] === this[i]) {
dupe = true;
break;
}
}
if (!dupe) result[result.length] = this[i];
}
return result;
};
/**************************************************************************************************
Returns a new array where all duplicates has been removed.
It's the same of Array.unique, but this time we use Array.indexOf to find duplicates.
**************************************************************************************************/
Array.prototype.unique2 = function() {
var result = [],
i, j, l1 = this.length,
l2, dupe;
//Adding 1st element to result, than one by one all other elements. Before adding each element we check that is not already present in result.
for (i = 0; i < l1; ++i)
//If dupe element is found we don't add the element to the result.
if (result.indexOf(this[i]) === -1) result[result.length] = this[i];
return result;
};
var v1 = [];
//Filling a vector with 10000 random value in the range 1-10 inclusive
for (i = 0; i < 10000; i++)
v1[i] = Math.floor(10 * Math.random()) + 1;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Remove duplicates without using Array.indexOf |
|
pending… |
Remove duplicates using Array.indexOf |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Marco Demaio
- Revision 2: published
- Revision 3: published by asins
- Revision 4: published
- Revision 5: published by Andy Harman
- Revision 7: published by David A. Mchedlishvili
- Revision 8: published by NiKo
- Revision 9: published by Brandon Benvie
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published by hongymagic
- Revision 15: published
0 comments