protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

utf8 read function frequent string concatenation generate a large number of ConsString objects

Open LukeChenyk opened this issue 1 year ago • 0 comments

protobuf.js version: 7.2.6

https://github.com/protobufjs/protobuf.js/commit/75172cd11be137bbabd2fba7a02b15067695ebad, this commit has some problem. On v8, frequent string concatenation can generate a large number of ConsString objects, leading to increased memory usage.


// lib/utf8/index.js
utf8.read = function utf8_read(buffer, start, end) {
    if (end - start < 1) {
        return "";
    }

    var str = "";
    for (var i = start; i < end;) {
        var t = buffer[i++];
        if (t <= 0x7F) {
            str += String.fromCharCode(t);
        } else if (t >= 0xC0 && t < 0xE0) {
            str += String.fromCharCode((t & 0x1F) << 6 | buffer[i++] & 0x3F);
        } else if (t >= 0xE0 && t < 0xF0) {
            str += String.fromCharCode((t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F);
        } else if (t >= 0xF0) {
            var t2 = ((t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F) - 0x10000;
            str += String.fromCharCode(0xD800 + (t2 >> 10));
            str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
        }
    }

    return str;
};

chrome inspect: image

LukeChenyk avatar Aug 21 '24 12:08 LukeChenyk