JS Object vs Array Iter

Benchmark created by Alex Ehrnschwender on


Preparation HTML

<script src="http://underscorejs.org/underscore-min.js"></script>

Setup

var teams = [
        {
        players: {
            f9e8aj: 9998,
            fkemq0: 2010
        },
        _id: 'yatta'
    },
    {
        players: {
            adfg: 2934,
            fkevdvdmq0: 935
        },
        _id: 'yatta'
    },
    {
        players: {
            f9ertfse8aj: 1212,
            fkemsdfewq0: 8243
        },
        _id: 'yatta'
    },
    {
        players: {
            f9wefe8aj: 5593,
            fkefsaqqmq0: 4034
        },
        _id: 'yatta'
    }
    ];

Test runner

Ready to run.

Testing in
TestOps/sec
Object reduce
var reduced = _.reduce( teams, function(memo, team){
    for(var id in team.players){
        memo[id] = team.players[id];
    }
    return memo;
}, {});
 
ready
Object reduce to keys array
var reducedArr = _.chain(teams)
.reduce(function(memo, team){
    for(var id in team.players){
        memo[id] = team.players[id];
    }
    return memo;
}, {})
.keys()
.value()
 
ready
Array pluck and keys flatten
var playerWinnerIdsArr = _.chain( teams )
.map(function(t){ return Object.keys(t.players) })
.flatten()
.value();
ready

Revisions

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

  • Revision 1: published by Alex Ehrnschwender on