What is the correct method for calculating the Content-length header in node.js

I'm not at all sure whether my current method of calculating the content-length is correct. What are the implications of using string.length() to calculate the content-length. Does setting the charset to utf-8 even mean anything when using node.js?

payload = JSON.stringify( payload );

response.header( 'content-type', application/json; charset=utf-8 );
response.header( 'content-length', payload.length );

response.end( payload );

Content-Length header must be the count of octets in the response body. payload.length refers to string length in characters. Some UTF-8 characters contain multiple bytes. The better way would be to use Buffer.byteLength(string, [encoding]) from http://nodejs.org/api/buffer.html. It's a class method so you do not have to create any Buffer instance.