HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Computationally efficient way of comparing and merging like object keys

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
comparingcomputationallymergingefficientwaylikekeysandobject

Problem

Though I can achieve most ends via a series of conditionals and arithmetic, especially when iterating over or comparing arrays I frequently hear of much more efficient implementations.

Hailing from PHP, I am accustomed to having array_merge() accessible; this JS function is different in that key that don't match the first array should be discarded (and generally, I'm using this sort of thing as a way of generating config objects for various parameterized components of a larger project).

I was hoping a serious comp-sci nerd could comment to the efficiency of this implementation of the same in JavaScript, and if it can be improved, explain—even very briefly—what's happening, computationally as it were. I have the intuitive sense that there it a bit-wise operation that could achieve this faster:

function mapConfig(template, cfg) {
    var configured_template = {};
    for (key in cfg) {
        template[key] = key in template ?  cfg[key] : false;
    }
    return template
}

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = mapConfig(template, cfg);
console.log(cfg_obj);

//output >>> {key1:true, key2:false, key3:false}


I appreciate any insights anyone could offer.

Solution

Interesting question,

there is no faster approach that I know of, in essence, your approach mimics jQuery's $.extend() source code. Except that your code does not clone parameters which might bring you trouble.

I think your code might be messed up, or you might not understand what it is doing.

This:

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = mapConfig(template, cfg);
console.log(cfg_obj);


is equivalent to this:

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

mapConfig(template, cfg);
console.log(template);


because you change the provided object itself, which probably is not right in all circumstances. I assume that is why you have configured_template in there ?

Furthermore, it does not seem to make sense to assign false to properties that you want to discard.. It eats up memory for no good reason.

All in all, I would counter propose

function configureTemplate( template, config) {
    var result = {}, key;
    for (key in config) {
        if( key in template ){
          result[key] = config[key];
        }
    }
    return result;
}


Note that you were polluting the global namespace by not declaring key with var.

Personally, I would drop most of the curly braces:

function configureTemplate( template, config) {
    var result = {}, key;
    for (key in config)
        if( key in template )
          result[key] = config[key];
    return result;
}

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = configureTemplate(template, cfg);
console.log(cfg_obj);


On a final note, try not to disemvowel your variables. It makes grokking code needlessly harder.

Code Snippets

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = mapConfig(template, cfg);
console.log(cfg_obj);
var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

mapConfig(template, cfg);
console.log(template);
function configureTemplate( template, config) {
    var result = {}, key;
    for (key in config) {
        if( key in template ){
          result[key] = config[key];
        }
    }
    return result;
}
function configureTemplate( template, config) {
    var result = {}, key;
    for (key in config)
        if( key in template )
          result[key] = config[key];
    return result;
}

var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};

var cfg_obj = configureTemplate(template, cfg);
console.log(cfg_obj);

Context

StackExchange Code Review Q#49772, answer score: 3

Revisions (0)

No revisions yet.