optimization - Nested (Condition) ? true:false statements in JavaScript -
in following code snippet,
var paintmode = data.hasownproperty('regions') ? 'regions' : data.hasownproperty('vpcs') ? 'vpcs' : data.hasownproperty('zones') ? 'zones' : data.hasownproperty('subnets') ? 'subnets' : data.hasownproperty('clusters') ? 'clusters' : null; i've used nested (condition) ? true : false -- acceptable? optimized? if bad there alternative?
it used inside recursive function few svg operations, here's function snippet if curious.
function paintgroups(data) { var paintmode = data.hasownproperty('regions') ? 'regions' : data.hasownproperty('vpcs') ? 'vpcs' : data.hasownproperty('zones') ? 'zones' : data.hasownproperty('subnets') ? 'subnets' : data.hasownproperty('clusters') ? 'clusters' : null, depth = data[paintmode].length, i, shape; //= raphealobj.rect(); // register stacking order // paint shape styles based on paintmode // store id & label other ops. if(paintmode) { for(i = 0; < depth; i++) { paintgroups(data[paintmode][i]); } } // reverse order of paint - place statements here }
generally speaking using binary operations should faster conditional statements branching because of branch prediction, caching, yada yada yada. code fine. && || it's preference , not based on empirical.
var data = {a: 1, b: 2, c: 3}; var result = data.x || data.y && "y" || data.z && "z" || data.w && "w" || null; i didn't feel typing .hasownproperty.
edit after looking @ code
var paintmode = data.regions || data.vpcs || data.zones || data.subnets || data.clusters || null; if(paintnode) { for(var i=0; i<paintmode.length; i++) paintgroups(paintmode[i]); }
Comments
Post a Comment