Skip to content

Optimization: Check whether encoding/decoding is needed #31

Description

@mudcube

Would it make sense to check whether encoding/decoding is needed?

if (/^[\u0000-\u007F]*$/i.test(str)) {
    return str
} else {
    return encode(str) // or decode(str)
}

Use case—I'm using UTF8.js to encode SVGs into dataURLs, and for larger SVGs it can take a little bit of time to encode them (not a huge amount, maybe 100ms per MB). Most of the time the SVGs do not actually need encoding as they're entirely ASCII, this RegExp check is fairly quick, so saves a bit of time overall.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions