procrust.js vs manual parsing vs pun vs matches.js

JavaScript performance comparison

Revision 2 of this test case created by GRaAL

Preparation code

<script src="https://rawgit.com/AlexeyGrishin/js-pattern-matching/master/procrust.js"></script>
<script src="https://rawgit.com/CRogers/pun/master/lib/pun.js"></script>
<script src="https://rawgit.com/natefaubion/matches.js/master/matches.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var When = procrust.When, Match = procrust.Match, Tail = procrust.Tail;
  
  var demoJson = {
    "user": {
      "firstname": "Alex",
      "enabled": true,
      "group": {
        "id": "admin"
      },
      "settings": {
        "mail": {
          "notify": ["change", "add", "delete"]
        }
      },
      "mailbox": {
        "id": "p1",
        "kind": "personal",
        "unreadmails": [
          {"id": 10, "subject": "Hello"},
          {"id": 11, "subject": "Hello"},
          {"id": 12, "subject": "Hello"},
          {"id": 13, "subject": "Hello"},
          {"id": 14, "subject": "Hello"},
          {"id": 15, "subject": "Hello"},
          {"id": 16, "subject": "Hello"}
        ],
        "readmails": [
          {"id": 23, "subject": "Hello"},
          {"id": 24, "subject": "Hello"},
          {"id": 25, "subject": "Hello"},
          {"id": 26, "subject": "Hello"}
        ]
      }
    }
  };
  
  
  var matchProcrust = Match(function() {
      return [
        When({
          user: {
            firstname: this.firstname,
            enabled: true,
            group: {
              id: "admin"
            },
            settings: {
              mail: {
                notify: this.notifications
              }
            },
            mailbox: {
              id: this.mailboxId,
              kind: "personal",
              unreadmails: [this.firstUnread | this.restUnread],
              readmails: [
                {
                  subject: "Hello"
                }, Tail(this.rest)
              ]
            }
          }
        }, function() {
          return "ok";
        })
      ];
    });
  
  var matchManual = function(item) {
    if (!item.user) return;
    var usr = item.user;
    if (!usr.enabled || !usr.group || usr.group.id != 'admin') return;
    var firstname = usr.firstname;
    if (!usr.settings || !usr.settings.mail) return;
    var notify = usr.settings.mail.notify;
    if (!usr.mailbox || usr.mailbox.kind != 'personal') return;
    var mailbox = usr.mailbox;
    var mailboxId = mailbox.id;
    if (!mailbox.unreadmails || mailbox.unreadmails.length == 0) return;
    var firstUnread = mailbox.unreadmails[0], restUnread= mailbox.unreadmails.slice(1);
    if (!mailbox.readmails || mailbox.readmails.length == 0) return;
    if (mailbox.readmails[0].subject != "Hello") return;
    var rest = mailbox.readmails.slice(1);
    
    return {firstname: firstname, notify: notify, mailboxId: mailboxId, rest: rest, firstUnread: firstUnread, restUnread: restUnread};
  
  };
  
  var $ = pun.$;
  var _ = pun._;
      var matchPun =  pun.match({user: {
          firstname: $('firstname'),
          enabled: true,
          group: {id: "admin"},
          settings: {mail: {notify: $('notifications')}},
          mailbox: {
            id: $('mailboxId'),
            kind: 'personal',
            unreadmails: $('unreadmails'),
            readmails: [{subject: 'Hello'}, _, _, _]
          }
        }}, function() { return "ok"; });
  
  var pattern = matches.pattern;
  var matchMatchesJs = pattern({
      '{user: {firstname, enabled: true, group: {id: "admin"}, settings: {mail: {notify: notifications}}, mailbox: {id: mailboxId, kind: "personal", unreadmails: [firstUnread, ...restUnread], readmails: [{subject: "Hello"}, ...rest]}}}': function() {
          return "ok";
      }
  });

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
procrust
matchProcrust(demoJson);
pending…
manual
if (!matchManual(demoJson)) throw "Error";
pending…
pun
matchPun(demoJson)
pending…
matches.js
matchMatchesJs(demoJson)
pending…

Compare results of other browsers

Revisions

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

0 Comments