Skip to content

Commit

Permalink
Faster parsing due to class usage (10% perf improvement)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 12, 2015
1 parent cf319d4 commit 90b6997
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

function Packet() {
this.cmd = null
this.retain = false
this.qos = 0
this.dup = false
this.length = -1
this.topic = null
this.payload = null
}

module.exports = Packet
5 changes: 2 additions & 3 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var bl = require('bl')
, inherits = require('inherits')
, EE = require('events').EventEmitter
, Packet = require('./packet')
, constants = require('./constants')

function Parser() {
Expand Down Expand Up @@ -29,9 +30,7 @@ Parser.prototype._newPacket = function () {
this.emit('packet', this.packet)
}

this.packet = {
length: -1
}
this.packet = new Packet()

return true
}
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function testParseGenerate(name, object, buffer, opts) {
, fixture = buffer

parser.on('packet', function(packet) {
if (packet.cmd !== 'publish') {
delete packet.topic
delete packet.payload
}
t.deepEqual(packet, expected, 'expected packet')
})

Expand All @@ -30,6 +34,10 @@ function testParseGenerate(name, object, buffer, opts) {
, fixture = mqtt.generate(object)

parser.on('packet', function(packet) {
if (packet.cmd !== 'publish') {
delete packet.topic
delete packet.payload
}
t.deepEqual(packet, expected, 'expected packet')
})

Expand Down

0 comments on commit 90b6997

Please sign in to comment.