Skip to content

Instantly share code, notes, and snippets.

@darrachequesne
Created December 12, 2016 00:11
Show Gist options
  • Save darrachequesne/57c86ca60746bb284969f964d6eeabfc to your computer and use it in GitHub Desktop.
Save darrachequesne/57c86ca60746bb284969f964d6eeabfc to your computer and use it in GitHub Desktop.
// npm i benchmark msgpack-lite notepack socket.io-parser
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var msgpackLite = require('msgpack-lite');
var notepack = require('notepack');
var parser = require('socket.io-parser');
var encoder = new parser.Encoder();
var dataObject = {
'a': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
'b': 'xxxyyyzzzalsdfalskdjfalksdjfalksdjfalksdjfjjfjfjfjjfjfjfj',
'data': {
'is': 'cool',
'or': {
'is': {
'it': true
}
}
}
};
var packet = {
type: parser.EVENT,
nsp: '/bench',
data: dataObject
};
var binaryPacket = {
type: parser.BINARY_ACK,
data: ['a', new Buffer('xxx', 'utf8'), {}],
id: 127,
nsp: '/back'
};
// SIZE
console.log('msgpackLite.length => ' + msgpackLite.encode(packet).byteLength);
encoder.encode(packet, function(encodedPackets) {
for (var i = 0; i < encodedPackets.length; i++) {
console.log('parser.length => ' + encodedPackets[i].length);
}
});
console.log('msgpackLite.length (binary) => ' + msgpackLite.encode(binaryPacket).byteLength);
encoder.encode(binaryPacket, function(encodedPackets) {
for (var i = 0; i < encodedPackets.length; i++) {
console.log('parser.length (binary) => ' + encodedPackets[i].length);
}
});
// SPEED
suite
.add('msgpack-lite', function() {
msgpackLite.decode(msgpackLite.encode(packet));
})
.add('parser', function(deferred) {
encoder.encode(packet, function(encodedPackets) {
var decoder = new parser.Decoder();
decoder.on('decoded', function() {
deferred.resolve();
});
for (var i = 0; i < encodedPackets.length; i++) {
decoder.add(encodedPackets[i]);
}
});
}, { defer: true })
.add('notepack', function() {
notepack.decode(notepack.encode(packet));
})
.add('msgpack-lite (binary)', function() {
msgpackLite.decode(msgpackLite.encode(binaryPacket));
})
.add('parser (binary)', function(deferred) {
encoder.encode(binaryPacket, function(encodedPackets) {
var decoder = new parser.Decoder();
decoder.on('decoded', function() {
deferred.resolve();
});
for (var i = 0; i < encodedPackets.length; i++) {
decoder.add(encodedPackets[i]);
}
});
}, { defer: true })
.add('notepack (binary)', function() {
notepack.decode(notepack.encode(binaryPacket));
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
// run async
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment