underscore group

Benchmark created by WearyMonkey on


Preparation HTML

<script src="//raw.github.com/bestiejs/lodash/v0.9.1/lodash.js"></script>
<script>lodash = _.noConflict();</script>
<script src="//underscorejs.org/underscore.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$.mapObj = function(array, key, groupElements, existing) {
    var map = existing || {}, i, length = array.length;
    if (key) {
        if (groupElements) {
            for (i = 0; i < length; ++i) {
                if (!map[array[i][key]]) map[array[i][key]] = [array[i]];
                else map[array[i][key]].push(array[i]);
            }
        } else {
            for (i = 0; i < length; ++i) {
                map[array[i][key]] = array[i];
            }
        }
    } else {
        for (i = 0; i < length; ++i) {
            map[array[i]] = true;
        }
    }
    return map;
};

var bigArray = [];
for (var i = 0; i < 500; ++i) {
   bigArray.push({id: i, ten: i % 10, hundred: 1 % 100}) 
}

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
underscore
var obj = _.groupBy(bigArray, 'ten')
ready
custom non grouping
var obj =$.mapObj(bigArray, 'ten')
ready
custom grouping
var obj =$.mapObj(bigArray, 'ten', true)
ready
lodash
var obj = lodash.groupBy(bigArray, 'ten')
ready

Revisions

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

  • Revision 1: published by WearyMonkey on