MOBILE-3001 core: Improve performance of uniqueArray

main
Dani Palou 2019-05-15 13:56:05 +02:00
parent 6dd8786da6
commit a8037b80b7
1 changed files with 6 additions and 8 deletions

View File

@ -1258,18 +1258,16 @@ export class CoreUtilsProvider {
*/ */
uniqueArray(array: any[], key?: string): any[] { uniqueArray(array: any[], key?: string): any[] {
const filtered = [], const filtered = [],
unique = [], unique = {}; // Use an object to make it faster to check if it's duplicate.
len = array.length;
for (let i = 0; i < len; i++) { array.forEach((entry) => {
const entry = array[i], const value = key ? entry[key] : entry;
value = key ? entry[key] : entry;
if (unique.indexOf(value) == -1) { if (!unique[value]) {
unique.push(value); unique[value] = true;
filtered.push(entry); filtered.push(entry);
} }
} });
return filtered; return filtered;
} }