Skip to content

Commit

Permalink
Memoize route match selector
Browse files Browse the repository at this point in the history
#performance
  • Loading branch information
aduth committed May 27, 2017
1 parent a7eec84 commit 33eef22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"querystringify": "^1.0.0",
"redux": "^3.6.0",
"refx": "^2.0.0",
"rememo": "^1.0.0",
"shallow-equal": "^1.0.0",
"textarea-caret": "^3.0.2"
}
Expand Down
46 changes: 25 additions & 21 deletions src/state/selectors/get-matched-route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import createSelector from 'rememo';
import { parse } from 'querystringify';

/**
Expand All @@ -9,28 +10,31 @@ import { parse } from 'querystringify';
import routes from 'routes';
import { getRoutePath } from './';

export default function getMatchedRoute( state ) {
const path = getRoutePath( state );
const [ pathname, search = '' ] = path.split( '?' );
export default createSelector(
( state ) => {
const path = getRoutePath( state );
const [ pathname, search = '' ] = path.split( '?' );

for ( let r = 0, rl = routes.length; r < rl; r++ ) {
const { regexp, keys, Route } = routes[ r ];
const match = pathname.match( regexp );
if ( ! match ) {
continue;
}
for ( let r = 0, rl = routes.length; r < rl; r++ ) {
const { regexp, keys, Route } = routes[ r ];
const match = pathname.match( regexp );
if ( ! match ) {
continue;
}

const params = {};
for ( let m = 1, ml = match.length; m < ml; m++ ) {
params[ keys[ m - 1 ].name ] = decodeURIComponent( match[ m ] );
}
const params = {};
for ( let m = 1, ml = match.length; m < ml; m++ ) {
params[ keys[ m - 1 ].name ] = decodeURIComponent( match[ m ] );
}

return {
params,
Route,
query: parse( search )
};
}
return {
params,
Route,
query: parse( search )
};
}

return {};
}
return {};
},
( state ) => getRoutePath( state )
);

0 comments on commit 33eef22

Please sign in to comment.