adamvr/MQTT.js — pull request #49 by mcollina (2013-03-06 19:02:03+00:00)
Title: Reduced the number of buffers created

Allocating and copying buffers is a performance hit (in the microseconds world), so we need to reduce that as much as possible.

The Connection now allocates a new Buffer only if the total length of the non-parsed data is greater than the buffer's length. In order to achieve this the residual data is moved at the beginning to make space for the new bytes. Previously two new buffers were allocated most of the time,

Ideally we should manage to avoid the whole copying between buffers by reading directly from the ones that comes from the socket.

The 'publish' packet generator does not allocate a new Buffer for the payload if it is a string.
Instead, It writes it directly on the packet's buffer.

These two PRs allowed me to reduce my benches of [Mosca](https://github.com/mcollina/mosca) by ~10%.