Skip to content

Commit fc24bf2

Browse files
vwochnikAvraamMavridis
authored andcommitted
perf: Improve performance of finding unique classes. (ericclemmons#24)
1 parent 02d4a42 commit fc24bf2

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ function testUniqueness( element, selector )
4848
return elements.length === 1 && elements[ 0 ] === element;
4949
}
5050

51+
/**
52+
* Tests all selectors for uniqueness and returns the first unique selector.
53+
* @param { Object } element
54+
* @param { Array } selectors
55+
* @return { String }
56+
*/
57+
function getFirstUnique( element, selectors )
58+
{
59+
return selectors.find( testUniqueness.bind( null, element ) );
60+
}
61+
5162
/**
5263
* Checks all the possible selectors of an element to find one unique and return it
5364
* @param { Object } element
@@ -57,15 +68,23 @@ function testUniqueness( element, selector )
5768
*/
5869
function getUniqueCombination( element, items, tag )
5970
{
60-
const combinations = getCombinations( items, 3 );
61-
const uniqCombinations = combinations.filter( testUniqueness.bind( this, element ) );
62-
if( uniqCombinations.length ) return uniqCombinations[ 0 ];
71+
let combinations = getCombinations( items, 3 ),
72+
firstUnique = getFirstUnique( element, combinations );
73+
74+
if( Boolean( firstUnique ) )
75+
{
76+
return firstUnique;
77+
}
6378

6479
if( Boolean( tag ) )
6580
{
66-
const combinations = items.map( item => tag + item );
67-
const uniqCombinations = combinations.filter( testUniqueness.bind( this, element ) );
68-
if( uniqCombinations.length ) return uniqCombinations[ 0 ];
81+
combinations = combinations.map( combination => tag + combination );
82+
firstUnique = getFirstUnique( element, combinations );
83+
84+
if( Boolean( firstUnique ) )
85+
{
86+
return firstUnique;
87+
}
6988
}
7089

7190
return null;

0 commit comments

Comments
 (0)