You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Javascript/OOP-Prototypal-Inheritence/Inheritence-OOP-Class-vs-Prototypes-Theory.md
+4-1
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,9 @@
1
1
#### Class Inheritance:
2
+
2
3
Instances inherit from classes (like a blueprint — a description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class inheritance may or may not use the class keyword from ES6.
3
4
4
5
#### Prototypal Inheritance:
6
+
5
7
Instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.
6
8
7
9
JavaScript objects use prototype-based inheritance. Its design is logically similar (but different in implementation) from class inheritance in strictly Object Oriented Programming languages.
@@ -14,9 +16,10 @@ This is why in many tutorials you will see **String.prototype.split** written in
14
16
15
17
Technically, just the light knowledge of the **class** and **extends** keywords is enough to write software. Trying to understand prototype is like venturing into the darker corners of language design. And sometimes that can be insightful.
16
18
17
-
Just know that at the very top of the Prototype hierarchy there is the Object object. That’s why its own prototype points to null. There’s nothing else above it.
19
+
Just know that at the very top of the Prototype hierarchy there is the Object object. That’s why its own prototype points to null. There’s nothing else above it.
Copy file name to clipboardExpand all lines: Javascript/call-apply-bind/bind-why-its-needed.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
1
+
The bind() method creates a new function that, when called, has its **this** keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
2
2
3
3
The bind() function creates a new bound function, which is an exotic function object (a term from ECMAScript 2015) that wraps the original function object. Calling the bound function generally results in the execution of its wrapped function.
According to MDN, the WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With the WebSocket API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
4
+
5
+
At the same time, WebSocket does not compromise with the security system of the web. All the WebSocket handshakes can be scrutinized by the browser using embedded developer tools in them.
6
+
7
+
WebSocket represents a single TCP socket connection, thus eliminating the problem of connection limitation.
8
+
9
+
### The differences between a WebSocket API and a FETCH API.
10
+
11
+
-1. WebSocket is a low-level protocol, based on the concept of socket and port, which are the underlying transport mechanism whereas REST is based on CRUD operation.
12
+
-2. WebSocket require the use of IP address and Port details, which are lower level details for any application whereas RESTful application needs to design operation based on verbs, and HTTP based.
13
+
-3. WebSocket is bi-directional in nature i.e. both way operation from client to server and vice versa is possible whereas REST follows a uni-directional approach.
14
+
-4. WebSocket approach is ideal for real-time scalable application, whereas REST is better suited for the scenario with lots of getting request.
15
+
-5. WebSocket is a stateful protocol whereas REST is based on stateless protocol i.e. client does not need to know about the server and same hold true for the server.
16
+
-6. WebSocket connection can scale vertically on a single server whereas REST, which is HTTP based can scale horizontally.
17
+
-7. WebSocket is ideal for a scenario where high loads are a part of game i.e. real-time scalable chat application whereas REST is better fitted for occasional communication, in a typical GET request scenario to call RESTful APIs.
18
+
-8. WebSocket works better, where client-server communicates over the same TCP connection for the life of web socket connection whereas, for HTTP request, a new TCP connection is initiated.
19
+
-9. WebSocket communication allows client and server to talk independently of each other whereas with the REST based approach, either client is talking to the client or server is talking to the client at any given time.
20
+
-10. WebSocket communication cost is lower whereas REST-based communication is comparatively higher end on the cost.
At first glance, each long URL and the corresponding alias form a key-value pair.
8
-
In a certain sense, a URL shortener is a giant hash table, which maps integer values to full URL strings. The integer values are represented in the "shortened" URL by a short sequence of characters, which may encode this integer in something like base-64 notation. An alphabet of 64 characters is used, each character representing 6 bits of an encoded number that is used as the key to the hash table.
8
+
In a certain sense, a URL shortener is a giant hash table, which maps integer values to full URL strings. The integer values are represented in the "shortened" URL by a short sequence of characters, which may encode this integer in something like base-64 notation. An alphabet of 64 characters is used, each character representing 6 bits of an encoded number that is used as the key to the hash table.
9
9
10
10
Therefore, the question can be simplified like this – given a URL, how can we find hash function F that maps URL to a short alias:
11
11
F(URL) = alias
12
12
and satisfies following condition:s
13
13
14
-
### Each URL can only be mapped to a unique alias
14
+
### Each URL can only be mapped to a unique alias
15
15
16
16
### Each alias can be mapped back to a unique URL easily
17
+
17
18
The second condition is the core as in the run time, the system should look up by alias and redirect to the corresponding URL quickly.
18
19
19
20
## Performance VS flexibility
@@ -30,11 +31,10 @@ On the flip side, using incremental IDs will make the mapping less flexible. For
30
31
31
32
I can hardly not ask about how to evaluate the cost of the system. For insert/query, we’ve already discussed above. So I’ll focus more on storage cost.
32
33
33
-
Each entry is stored as <ID, URL> where ID is a 7-character string. Assuming max URL length is 2083 characters, then each entry takes 7 * 4 bytes + 2083 * 4 bytes = 8.4 KB. If we store a million URL mappings, we need around 8.4G storage.
34
+
Each entry is stored as <ID, URL> where ID is a 7-character string. Assuming max URL length is 2083 characters, then each entry takes 7 _ 4 bytes + 2083 _ 4 bytes = 8.4 KB. If we store a million URL mappings, we need around 8.4G storage.
34
35
35
36
If we consider the size of database index and we may also store other information like user ID, date each entry is inserted etc., it definitely requires much more storage.
36
37
37
-
38
38
## How would you generate unique IDs at scale (thousands of URL shortening requests coming every second)?
39
39
40
40
## How would your service handle redirects?
@@ -46,14 +46,15 @@ If we consider the size of database index and we may also store other informatio
46
46
## How to track click stats?
47
47
48
48
## What characters can we allow in our randomly-generated slugs?
It turns out the answer is "only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL" (RFC 1738). "Reserved characters" with "reserved purposes" are characters like '?', which marks the beginning of a query string, and '#', which marks the beginning of a fragment/anchor. We definitely shouldn't use any of those. If we allowed '?' in the beginning of our slug, the characters after it would be interpreted as part of the query string and not part of the slug!
52
+
It turns out the answer is "only alphanumerics, the special characters "\$-\_.+!\*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL" (RFC 1738). "Reserved characters" with "reserved purposes" are characters like '?', which marks the beginning of a query string, and '#', which marks the beginning of a fragment/anchor. We definitely shouldn't use any of those. If we allowed '?' in the beginning of our slug, the characters after it would be interpreted as part of the query string and not part of the slug!
52
53
53
-
Okay, so it seems like the set of allowed characters is A-Z, a-z, 0-9, and "$-_.+!*'(),". The apostrophe character seems a little iffy, since sometimes URLs are surrounded by single quotes in HTML documents. So let's pull that one.
54
+
Okay, so it seems like the set of allowed characters is A-Z, a-z, 0-9, and "\$-\_.+!\*'(),". The apostrophe character seems a little iffy, since sometimes URLs are surrounded by single quotes in HTML documents. So let's pull that one.
Domains aren't case-sensitive (so google.com and Google.com will always go to the same place), but the path portion of a URL is case-sensitive. If I query parker.com/foo and parker.com/Foo, I'm requesting different documents (although, as a site owner, I may choose to return the same document in response to both requests). So yes, lowercase and capital versions of the same letter can be treated as different characters in our slugs.
60
+
Domains aren't case-sensitive (so google.com and Google.com will always go to the same place), but the path portion of a URL is case-sensitive. If I query parker.com/foo and parker.com/Foo, I'm requesting different documents (although, as a site owner, I may choose to return the same document in response to both requests). So yes, lowercase and capital versions of the same letter can be treated as different characters in our slugs.
0 commit comments