Skip to content

Commit d9491e1

Browse files
authored
fix(perf): remove usage of SearchParameters.clearRefinements (#6004)
This functions is much slower than individually clearing the specified refinements. In a test with DynamicWidgets + 118 RefinementLists the initial mount goes from 600ms to 350ms in my tests. I've looked around to see if there's good tools for these improvements, to ensure we don't regress, but i couldn't find much (perf is mainly focused on apps). Unfortunately I can't really deprecate the method as we still use it in a non-hot path inside the helper itself to create the right queries in queryBuilder. We'll just have to rely on "seeing other examples" to avoid using parameters.clearRefinements. As widgets can't be mixed across the same attribute, there's no actual risk in this change AFAICT.
1 parent 332933c commit d9491e1

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

packages/instantsearch.js/src/connectors/numeric-menu/connectNumericMenu.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const connectNumericMenu: NumericMenuConnector = function connectNumericMenu(
214214

215215
dispose({ state }) {
216216
unmountFn();
217-
return state.clearRefinements(attribute);
217+
return state.removeNumericRefinement(attribute);
218218
},
219219

220220
getWidgetUiState(uiState, { searchParameters }) {
@@ -250,15 +250,15 @@ const connectNumericMenu: NumericMenuConnector = function connectNumericMenu(
250250
getWidgetSearchParameters(searchParameters, { uiState }) {
251251
const value = uiState.numericMenu && uiState.numericMenu[attribute];
252252

253-
const withoutRefinements = searchParameters.clearRefinements(attribute);
253+
const withoutRefinements = searchParameters.setQueryParameters({
254+
numericRefinements: {
255+
...searchParameters.numericRefinements,
256+
[attribute]: {},
257+
},
258+
});
254259

255260
if (!value) {
256-
return withoutRefinements.setQueryParameters({
257-
numericRefinements: {
258-
...withoutRefinements.numericRefinements,
259-
[attribute]: {},
260-
},
261-
});
261+
return withoutRefinements;
262262
}
263263

264264
const isExact = value.indexOf(':') === -1;

packages/instantsearch.js/src/connectors/rating-menu/connectRatingMenu.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,10 @@ const connectRatingMenu: RatingMenuConnector = function connectRatingMenu(
454454
getWidgetSearchParameters(searchParameters, { uiState }) {
455455
const value = uiState.ratingMenu && uiState.ratingMenu[attribute];
456456

457-
const withoutRefinements = searchParameters.clearRefinements(attribute);
458-
const withDisjunctiveFacet =
459-
withoutRefinements.addDisjunctiveFacet(attribute);
457+
const withDisjunctiveFacet = searchParameters
458+
.addDisjunctiveFacet(attribute)
459+
.removeNumericRefinement(attribute)
460+
.removeDisjunctiveFacetRefinement(attribute);
460461

461462
if (!value) {
462463
return withDisjunctiveFacet.setQueryParameters({

packages/instantsearch.js/src/connectors/refinement-list/connectRefinementList.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,13 @@ As this is not supported, please make sure to only use this attribute with one o
517517
const values =
518518
uiState.refinementList && uiState.refinementList[attribute];
519519

520-
const withoutRefinements =
521-
searchParameters.clearRefinements(attribute);
522520
const withFacetConfiguration = isDisjunctive
523-
? withoutRefinements.addDisjunctiveFacet(attribute)
524-
: withoutRefinements.addFacet(attribute);
521+
? searchParameters
522+
.addDisjunctiveFacet(attribute)
523+
.removeDisjunctiveFacetRefinement(attribute)
524+
: searchParameters
525+
.addFacet(attribute)
526+
.removeFacetRefinement(attribute);
525527

526528
const currentMaxValuesPerFacet =
527529
withFacetConfiguration.maxValuesPerFacet || 0;

packages/instantsearch.js/src/connectors/toggle-refinement/connectToggleRefinement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ As this is not supported, please make sure to remove this other widget or this T
455455
}
456456

457457
let withFacetConfiguration = searchParameters
458-
.clearRefinements(attribute)
459-
.addDisjunctiveFacet(attribute);
458+
.addDisjunctiveFacet(attribute)
459+
.removeDisjunctiveFacetRefinement(attribute);
460460

461461
const isRefined = Boolean(
462462
uiState.toggle && uiState.toggle[attribute]

0 commit comments

Comments
 (0)