`Buffer.byteLength` Performance Regression from v18.x to v22.x
For Buffer.byteLength(string[, encoding]), when set encoding is buffer:
buffers/buffer-bytelength.js n=4000000 len=16 encoding='buffer' *** -36.69 % ±1.78% ±2.38% ±3.09% buffers/buffer-bytelength.js n=4000000 len=2 encoding='buffer' *** -35.95 % ±3.34% ±4.48% ±5.91% buffers/buffer-bytelength.js n=4000000 len=256 encoding='buffer' *** -35.46 % ±5.18% ±6.95% ±9.17%
During my investigation, I found that the performance regression might be due to ArrayBufferView.byteLength.
for the implementation of Buffer.byteLength in lib:
function byteLength(string, encoding) { if (typeof string !== 'string') { if (isArrayBufferView(string) || isAnyArrayBuffer(string)) { return string.byteLength; } // ... } }
when string.byteLength is not called in either v18.x or v22.x(for example, change to return 0), the regression nearly disappears:
confidence improvement accuracy (*) (**) (***) buffers/buffer-bytelength-buffer.js n=60000000 len=16 *** -3.16 % ±1.25% ±1.66% ±2.16% buffers/buffer-bytelength-buffer.js n=60000000 len=2 -1.39 % ±2.15% ±2.86% ±3.74% buffers/buffer-bytelength-buffer.js n=60000000 len=256 ** -2.16 % ±1.28% ±1.72% ±2.25%
Based on the findings, it appears that the handling of ArrayBufferView.byteLength might be the cause of the performance regression observed.