Alasql vs Lodash

Benchmark created by Andrey Gershun on


Description

Based on StackOverflow question:

http://stackoverflow.com/questions/25676026/group-array-with-sub-array/27596147#27596147

Preparation HTML

<script src="http://alasql.org/console/alasql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>

Setup

var data = [{
      'userId': 1,
      'userName': 'bob',
      'category': 'shoes',
      'count': 2
    }, {
      'userId': 1,
      'userName': 'bob',
      'category': 'rocks',
      'count': 4
    }, {
      'userId': 1,
      'userName': 'bob',
      'category': 'bags',
      'count': 3
    }, {
      'userId': 2,
      'userName': 'sue',
      'category': 'shoes',
      'count': 1
    }, {
      'userId': 2,
      'userName': 'sue',
      'category': 'rocks',
      'count': 7
    }, {
      'userId': 2,
      'userName': 'sue',
      'category': 'bags',
      'count': 4
    }, ];

Test runner

Ready to run.

Testing in
TestOps/sec
Lodash
    var groups = _.groupBy(data, 'userId');

    var result = _.map(groups, function(user) {
      return {
        userId: user[0].userId,
        userName: user[0].userName,
        purchases: _.map(user, function(purchase) {
          return {
            category: purchase.category,
            count: purchase.count
          }
        }),
        totalCount: _.reduce(user, function(memo, purchase) {
          return memo + purchase.count;
        }, 0)
      }
    });
ready
Alasql
var res = alasql('SELECT userId, FIRST(userName) AS username, ARRAY({category:category,[count]:[count]}) AS purchases, SUM([count]) AS totalCount FROM ? GROUP BY userId', [data]);
ready

Revisions

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

  • Revision 1: published by Andrey Gershun on