for loop vs map create object from another

Benchmark created on


Setup

var obj = {
      Key1: "Val1",
      Key2: 42,
      Key3: "Val3",
      Key4: "Val4"
    };
    
    function json_to_hstore_map (json) {
        if (typeof json == 'string') json = JSON.parse(json);
        var result = Object.keys(json).map(function(key) {
            var value = json[key];
            value = value === null ? 'NULL' : JSON.stringify(value.toString());
            return '"' + key + '" => ' + value;
        }).join(', ');
    
        return result;
    }
    
    function json_to_hstore_for (json) {
        if (typeof json == 'string') json = JSON.parse(json);
        var hstore = [];
        for (var key in json) {
            var value = json[key];
            value = value === null ? 'NULL' : JSON.stringify(value.toString());
            hstore.push('"' + key + '" => ' + value);
        }
        return hstore.join(', ');
    }
    
    if (json_to_hstore_map(obj) != json_to_hstore_for(obj)) {
    throw "Tests not equal";
    }

Test runner

Ready to run.

Testing in
TestOps/sec
map
json_to_hstore_map(obj);
ready
for
json_to_hstore_for(obj);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.