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

Optimization: Check whether encoding/decoding is needed

Open mudcube opened this issue 8 years ago • 0 comments

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.

mudcube avatar Jul 12 '17 18:07 mudcube