Skip to content

Commit 53f9f57

Browse files
Lazily calculate the hash (#130)
* Lazily calculate the hash * Refactor to switch statement
1 parent 9e4bbab commit 53f9f57

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

flagsmith-engine/features/models.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,27 @@ export class FeatureStateModel {
9696
}
9797
return this.featureSegment.priority < other.featureSegment.priority;
9898
}
99-
100-
getMultivariateValue(identityID: number | string) {
101-
const percentageValue = getHashedPercentateForObjIds([
102-
this.djangoID || this.featurestateUUID,
103-
identityID
104-
]);
10599

100+
getMultivariateValue(identityID: number | string) {
101+
let percentageValue: number | undefined;
106102
let startPercentage = 0;
107-
const sortedF = this.multivariateFeatureStateValues.sort((a, b) =>{
103+
const sortedF = this.multivariateFeatureStateValues.sort((a, b) => {
108104
return a.id - b.id;
109105
});
110106
for (const myValue of sortedF) {
107+
switch (myValue.percentageAllocation) {
108+
case 0:
109+
continue;
110+
case 100:
111+
return myValue.multivariateFeatureOption.value;
112+
default:
113+
if (percentageValue === undefined) {
114+
percentageValue = getHashedPercentateForObjIds([
115+
this.djangoID || this.featurestateUUID,
116+
identityID
117+
]);
118+
}
119+
}
111120
const limit = myValue.percentageAllocation + startPercentage;
112121
if (startPercentage <= percentageValue && percentageValue < limit) {
113122
return myValue.multivariateFeatureOption.value;

0 commit comments

Comments
 (0)