Skip to content

Commit 006120d

Browse files
committed
localstorage vs sessionstorage
1 parent 3ab4f2b commit 006120d

11 files changed

+34
-12
lines changed

Javascript/closure.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
1+
A closure is the combination of a function bundled together (enclosed) with **references to its surrounding state (the lexical environment)**. In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
22

3-
A closure is a function that retains access to variables of the context it was created in even after said function call has returned.
3+
**A closure is a function** that retains access to variables of the context it was created in even after said function call has returned.
44

55
```js
66
var awFunc = function (first) {
@@ -21,15 +21,16 @@ console.log(someMoreFunc('some')) // returns awesome
2121

2222
Among other things, closures are commonly used to give objects data privacy. Data privacy is an essential property that helps us program to an interface, not an implementation. This is an important concept that helps us build more robust software because implementation details are more likely to change in breaking ways than interface contracts.
2323

24-
In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged. For example:
24+
To use a closure, simply define a function inside another function and expose it. To expose a function, return it or pass it to another function.
25+
26+
#### In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged. For example:
2527

2628
```js
2729
const getSecret = (secret) => {
2830
return {
2931
getPrivileged: () => secret
3032
}
3133
}
32-
3334
```
3435

3536
In the example above, the `.getPrivileged()` method is defined inside the scope of `getSecret()`, which gives it access to any variables from `getSecret()`, and makes it a privileged method. In this case, the parameter, `secret`.
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### Transform the following code with
1+
### Transform the following code with sequential execution. I want to fire second request based on one value returned by the first request.
22

33
```js
44
fetch("https://api.com/values/1")
@@ -18,4 +18,11 @@ const request = async () => {
1818
request();
1919
```
2020

21-
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the response variable.
21+
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the *response* variable.
22+
23+
In the second line, we get the JSON version of the response. Again, we use await so we can wait for it to complete (or fail) and then pass the result to the json variable.
24+
25+
26+
#### Further Reading
27+
28+
https://dev.to/johnpaulada/synchronous-fetch-with-asyncawait

Node-Express/sesstionStorage-vs-localStorage-vs-Cookie.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
**localStorage** and **sessionStorage** both extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage.
2+
3+
**That is, the data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.**
4+
5+
For sessionStorage, changes are only available per tab. Changes made are saved and available for the current page in the that tab until it is closed. Once it is closed, the stored data is deleted.
6+
17
[From Official Dox](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)
28

39
The two mechanisms within Web Storage are as follows:
@@ -6,29 +12,37 @@ The two mechanisms within Web Storage are as follows:
612

713
**localStorage** does the same thing, but persists even when the browser is closed and reopened.
814

9-
These mechanisms are available via the Window.sessionStorage and Window.localStorage properties (to be more precise, in supporting browsers the Window object implements the WindowLocalStorage and WindowSessionStorage objects, which the localStorage and sessionStorage properties hang off) — invoking one of these will create an instance of the Storage object, through which data items can be set, retrieved and removed. A different Storage object is used for the sessionStorage and localStorage for each origin — they function and are controlled separately.
15+
These mechanisms are available via the **Window.sessionStorage** and **Window.localStorage** properties (to be more precise, in supporting browsers the Window object implements the WindowLocalStorage and WindowSessionStorage objects, which the localStorage and sessionStorage properties hang off) — invoking one of these will create an instance of the Storage object, through which data items can be set, retrieved and removed. A different Storage object is used for the sessionStorage and localStorage for each origin — they function and are controlled separately.
1016

1117
localStorage and sessionStorage are perfect for persisting non-sensitive data needed within client scripts between pages (for example: preferences, scores in games). The data stored in localStorage and sessionStorage can easily be read or changed from within the client/browser so should not be relied upon for storage of sensitive or security-related data within applications.
1218

13-
#### So sessionStorage (as the name suggests) is only available for the duration of the browser session (and is deleted when the tab or window is closed) - it does, however, survive page reloads (source DOM Storage guide - Mozilla Developer Network). Clearly, if the data you are storing needs to be available on an ongoing basis then localStorage is preferable to sessionStorage. The data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
19+
#### sessionStorage (as the name suggests) is only available for the duration of the browser session (and is deleted when the tab or window is closed) - it does, however, survive page reloads (source DOM Storage guide - Mozilla Developer Network). Clearly, if the data you are storing needs to be available on an ongoing basis then localStorage is preferable to sessionStorage. The data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
1420

1521
### LocalStorage - Some more details
1622

1723
##### Advantage of localStorage -
24+
1> Storage limit is the maximum amongst the three
25+
26+
2> One of the most important differences is that unlike with cookies, data does not have to be sent back and forth with every HTTP request. This reduces the overall traffic between the client and the server and the amount of wasted bandwidth. This is because data is stored on the user’s local disk and is not destroyed or cleared by the loss of an internet connection.
1827

1928
##### Disadvantage of localStorage -
2029

21-
1> It works on same-origin policy. So, data stored will only be available on the same origin.
30+
1> LocalStorage becomes slower as more data is stored4. For most browsers the time it takes for localStorage.getItem to return increases from a few milliseconds with a near empty storage to over a second with a full storage.
31+
32+
2> It works on same-origin policy. So, data stored will only be available on the same origin.
33+
34+
3> Any JavaScript code on your page can access local storage: it has no data protection whatsoever. This is the big one for security reasons
2235

2336
### SessionStorage - Some more details
37+
1>
2438

2539
##### Advantage of sessionStorage -
2640

2741
##### Disadvantage of sessionStorage -
2842

2943
1> The data is available only inside the window/tab in which it was set.
3044

31-
2> Like localStorage, tt works on same-origin policy. So, data stored will only be available on the same origin.
45+
2> Like localStorage, it works on same-origin policy. So, data stored will only be available on the same origin.
3246

3347
### More General Info
3448

@@ -41,7 +55,7 @@ The maximum storage available is different per browser, but most browsers have i
4155
+----------------+--------+---------+-----------+--------+
4256
| SessionStorage | 10MB | 10MB | Unlimited | 10MB |
4357
+----------------+--------+---------+-----------+--------+
44-
Always catch LocalStorage security and quota exceeded errors
58+
**Always catch LocalStorage security and quota exceeded errors**
4559

4660
QuotaExceededError: When storage limits exceeds on this function window.sessionStorage.setItem(key, value);, it throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)
4761

@@ -59,5 +73,5 @@ Compared to others, there's no advantages of Cookies
5973
Stores data that has to be sent back to the server with subsequent requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side).
6074

6175
Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side.
62-
Size must be less than 4KB.
76+
Cookies Size must be less than 4KB.
6377
Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie

0 commit comments

Comments
 (0)