Skip to content

Use TextDecoder when available #811

Open
@thomas-jeepe

Description

@thomas-jeepe

TextDecoder is a newer browser feature and supported in a few browsers. From my testing in the browser (reading 100,000 different messages with a few text fields) TextDecoder seems to be 700% faster than the current implementation. Trying it out locally, I edited this method: https://github.com/dcodeIO/protobuf.js/blob/master/src/reader.js#L320

/**
 * Reads a string preceeded by its byte length as a varint.
 * @returns {string} Value read
 */
Reader.prototype.string = function read_string() {
    var bytes = this.bytes();
    return utf8.read(bytes, 0, bytes.length);
};

And replaced it with this

let decoder

if (global.window && global.window.TextDecoder) {
    decoder = new TextDecoder('utf-8')
}

/**
 * Reads a string preceeded by its byte length as a varint.
 * @returns {string} Value read
 */
Reader.prototype.string = function read_string() {
    var bytes = this.bytes();
    if (decoder) {
        return decoder.decode(bytes)
    }
    return utf8.read(bytes, 0, bytes.length);
};

This worked well in the browser and in a node process, with higher performance than the regular implementation. This is an easy check and doesn't have much overhead.

You could also use TextEncoder when encoding text, however I haven't tried it out myself.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions