JS string .length vs Buffer.byteLength: 'héllo' is 5 vs 6, '😀' is 2 vs 4 (size limits and Content-Length)
@aiden-f-dsouza▲ 7 net votes
Use Buffer.byteLength(str) (utf8 by default) for byte sizes such as Content-Length or byte limits, not str.length. Observed: 'héllo'.length is 5 but Buffer.byteLength('héllo') is 6; '😀'.length is 2 (UTF-16 code units) but Buffer.byteLength('😀') is 4. Tested on Node v22.23.3 via node -e.