Make rows comparison (v2)

Revision 2 of this benchmark created by Esailija on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script>
var makeRows1 = (function () {
    "use strict";
    
    function reduceParticipants(previous, participant) {
        previous[participant.id] = [participant.name];

        return previous;
    }

    function reduceResult(previous, subResult) {
        previous[subResult.participantId].push(subResult.answer);

        return previous;
    }

    function filterParticipants(participant) {
        return participant;
    }

    return function (participants, results) {
        var row = participants.reduce(reduceParticipants, []);

        results.forEach(function (result) {
            result.reduce(reduceResult, row);
        });

        return row.filter(filterParticipants);
    };
}());

function makeRows2(participants, results) {
    "use strict";

    var o = {};

    for (var i = 0, len = participants.length; i < len; ++i) {
        o[participants[i].id] = [participants[i].name];
    }

    for (var i = 0, len = results.length; i < len; ++i) {
        var innerResult = results[i];
        for (var j = 0, len2 = innerResult.length; j < len2; ++j) {
            o[innerResult[j].participantId].push(innerResult[j].answer);
        }

    }

    //The rows are in o but you can get an array of course if you want:

    var result = [];

    for (var key in o) {
        result.push(o[key]);
    }

    return result;
}

function makeRows3(participants, results) {
    "use strict";

    return _.reduce(_.groupBy(_.flatten(results), 'participantId'), function (memo, group) {
        var user = _.find(participants, function (p) {
                return p.id === group[0].participantId;
            }),
            arr = _.pluck(group, 'answer');

        arr.unshift(user.name);
        memo.push(arr);

        return memo;
    }, []);
}

function makeRows4(participants, results) {
    var o = {};

    for (var i = 0, len = participants.length; i < len; ++i) {
        o[participants[i].id] = [participants[i].name];
    }

    var innerLength = results[0].length;

    for (var j = 0; j < innerLength; ++j) {
        var array = o[results[0][j].participantId];
        for (var i = 0, len = results.length; i < len; ++i) {
            array.push(results[i][j].answer);
        }
    }

    //The rows are in o but you can get an array of course if you want:

    var result = [];

    for (var key in o) {
        result.push(o[key]);
    }


}

function makeName() {
    var text = "",
        possible = "abcdefghijklmnopqrstuvwxy",
        i;

    for (i = 0; i < 5; i += 1) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }

    return text;
}

var count,
    count2,
    index,
    index2,
    participants = [],
    results = [];

for (index = 0, count = 1000; index < count; index += 4) {
    participants.push({
        id: index,
        name: makeName()
    });
}

for (index = 0, count = 1000; index < count; index += 1) {
    results[index] = [];
    for (index2 = 0, count2 = participants.length; index2 < count2; index2 += 1) {
        results[index].push({
            question: index,
            participantId: participants[index2].id,
            answer: "test" + index
        });
    }
}



</script>

Test runner

Ready to run.

Testing in
TestOps/sec
makeRows1
makeRows1(participants, results);
ready
makeRows2
makeRows2(participants, results);
ready
makeRows3
makeRows3(participants, results);
ready
makeRows4
makeRows4(participants, results);
ready

Revisions

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

  • Revision 2: published by Esailija on