lz-string icon indicating copy to clipboard operation
lz-string copied to clipboard

UTF-16 limitation on Firefox?

Open Kassy2048 opened this issue 5 months ago • 0 comments

From the user guide: "On Firefox and IE, localStorage cannot contain invalid UTF16 characters."

I've tried the following code in Firefox to check that, and saw no problem at all:

for(let t = 0 ; t < 100 ; ++t) {
    console.log("t=" + t);
    // Generate a 100K array with random 16-bit values
    let array = [];
    for(let i = 0 ; i < 100000 ; ++i) array.push(parseInt(Math.floor(Math.random() * 65536)));
    // Convert it to an UTF-16 string
    const src = String.fromCharCode.apply(null, new Uint16Array(array))
    // Store the string in localStorage
    localStorage._test_string = src;
    // Retrieve the string from localStorage
    const dst = localStorage._test_string;
    // Check the localStorage content matches the original array
    for(let i = 0 ; i < 100000 ; ++i) {
        if(dst.charCodeAt(i) != array[i]) console.warn("Failure for i=" + i, dst.charCodeAt(i), array[i]);
    }
    // Remove the string from localStorage
    delete localStorage._test_string;
}

Is this limitation still valid? If so, how to replicate it? If not, the documentation should probably be updated to indicate this is not a problem anymore on recent versions of all browsers.

Kassy2048 avatar Sep 22 '24 23:09 Kassy2048