Local Storage vs Cookies

后端 未结 7 1485
名媛妹妹
名媛妹妹 2020-11-21 11:04

I want to reduce load times on my websites by moving all cookies into local storage since they seem to have the same functionality. Are there any pros/cons (especially perfo

7条回答
  •  失恋的感觉
    2020-11-21 11:44

    With localStorage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Large amounts of data can be stored locally, without affecting website performance. Although localStorage is more modern, there are some pros and cons to both techniques.

    Cookies

    Pros

    • Legacy support (it's been around forever)
    • Persistent data
    • Expiration dates

    Cons

    • Each domain stores all its cookies in a single string, which can make parsing data difficult
    • Data is unencrypted, which becomes an issue because... ... though small in size, cookies are sent with every HTTP request Limited size (4KB)
    • SQL injection can be performed from a cookie

    Local storage

    Pros

    • Support by most modern browsers
    • Persistent data that is stored directly in the browser
    • Same-origin rules apply to local storage data
    • Is not sent with every HTTP request
    • ~5MB storage per domain (that's 5120KB)

    Cons

    • Not supported by anything before: IE 8, Firefox 3.5, Safari 4, Chrome 4, Opera 10.5, iOS 2.0, Android 2.0
    • If the server needs stored client information you purposely have to send it.

    localStorage usage is almost identical with the session one. They have pretty much exact methods, so switching from session to localStorage is really child's play. However, if stored data is really crucial for your application, you will probably use cookies as a backup in case localStorage is not available. If you want to check browser support for localStorage, all you have to do is run this simple script:

    /* 
    * function body that test if storage is available
    * returns true if localStorage is available and false if it's not
    */
    function lsTest(){
        var test = 'test';
        try {
            localStorage.setItem(test, test);
            localStorage.removeItem(test);
            return true;
        } catch(e) {
            return false;
        }
    }
    
    /* 
    * execute Test and run our custom script 
    */
    if(lsTest()) {
        // window.sessionStorage.setItem(name, 1); // session and storage methods are very similar
        window.localStorage.setItem(name, 1);
        console.log('localStorage where used'); // log
    } else {
        document.cookie="name=1; expires=Mon, 28 Mar 2016 12:00:00 UTC";
        console.log('Cookie where used'); // log
    }
    

    "localStorage values on Secure (SSL) pages are isolated" as someone noticed keep in mind that localStorage will not be available if you switch from 'http' to 'https' secured protocol, where the cookie will still be accesible. This is kind of important to be aware of if you work with secure protocols.

提交回复
热议问题