performance - How would you implement efficiently a feature flip (or feature flag) in Javascript? -


this question not on how write such feature flag per-se. seek out recommendations or hints on such system.

the idea simple:

in order progressively roll out/in features, need fork behaviors.

to so, create featuredescriptionflag object contains arrays of strings. if exist return true, otherwise false

but, also, has consider:

  • if no flag object exists, returns false in no flip has ben triggered
  • the ideal condition remove flags

i refering feature flag, concepts described here:

the question

my main concerns on implementation following:

  1. using global object describe flag feels wrong
  2. when conditional statement exists, should cache outcome subsequent calls (e.g. executes true first, somewhere edits featuredescriptionflag, next calls returns false)

implementation example

this first implementation came (see on jsfiddle):

// warning: requires underscore.js function featureflag(flagcontainername, elementkeytocheck) {   //console.log('feature flagging on '+flagcontainername+', ' + elementkeytocheck); // debug   if (_.has(window.featureflagdescriptor||{}, flagcontainername)) {      if(_.contains(window.featureflagdescriptor[flagcontainername]||[], elementkeytocheck)) {        //console.log('disabled '+flagcontainername+', matching ' + elementkeytocheck); // debug        return true;      }   }   return false; } 

then, flag description object create global

window.featureflagdescriptor = {somefunction: ['example']}; 

my concern here may dangerous allow such flip, have alarms in mind on how exploited.

later, in function has have 2 behaviors, do:

function somefunction(){   // code...   if(featureflag('somefunction', 'example')) {     // work if flag exist   } else {     // former behavior   } } 

conclusion

any recommendations, improvement proposals?

i have created jsfiddle on subject , make full working sample.


Comments