Skip to content

Commit

Permalink
perf: Improve performance of finding unique classes. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
vwochnik authored and AvraamMavridis committed Feb 20, 2017
1 parent 02d4a42 commit fc24bf2
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/index.js
Expand Up @@ -48,6 +48,17 @@ function testUniqueness( element, selector )
return elements.length === 1 && elements[ 0 ] === element;
}

/**
* Tests all selectors for uniqueness and returns the first unique selector.
* @param { Object } element
* @param { Array } selectors
* @return { String }
*/
function getFirstUnique( element, selectors )
{
return selectors.find( testUniqueness.bind( null, element ) );
}

/**
* Checks all the possible selectors of an element to find one unique and return it
* @param { Object } element
Expand All @@ -57,15 +68,23 @@ function testUniqueness( element, selector )
*/
function getUniqueCombination( element, items, tag )
{
const combinations = getCombinations( items, 3 );
const uniqCombinations = combinations.filter( testUniqueness.bind( this, element ) );
if( uniqCombinations.length ) return uniqCombinations[ 0 ];
let combinations = getCombinations( items, 3 ),
firstUnique = getFirstUnique( element, combinations );

if( Boolean( firstUnique ) )
{
return firstUnique;
}

if( Boolean( tag ) )
{
const combinations = items.map( item => tag + item );
const uniqCombinations = combinations.filter( testUniqueness.bind( this, element ) );
if( uniqCombinations.length ) return uniqCombinations[ 0 ];
combinations = combinations.map( combination => tag + combination );
firstUnique = getFirstUnique( element, combinations );

if( Boolean( firstUnique ) )
{
return firstUnique;
}
}

return null;
Expand Down

0 comments on commit fc24bf2

Please sign in to comment.