JavaScript series-----objects based on hash storage (<Key,Value> Key article) (1)

Source: Internet
Author: User



Structure of the 1.Hash table






First, allow us to take a moment to introduce the hash table briefly.



1. What is a hash table



Hash table is a two-dimensional structure, the management of a pair of <Key,Value> such key-value pairs, the structure of the hash table as shown:






As shown, the left part is an array of one-dimensional sequential storage, and the contents of the array cell are pointers to another chain array. The green part of the figure is <key,value> the white part on the right side of the green section is a pointer to a pair of key values down.



How the hash table works:



(1). The first step is to obtain a specific hash value based on the given key and hash algorithm, which is the corresponding array subscript.



(2). The second step, according to the array subscript to get the pointer stored in this subscript, if the pointer is empty, there is no such a key value pair, otherwise according to this pointer to get this chain array. (This chain array contains a pair of pairs of <Key,Value>).



(3). Traverse this chain array, remove key separately from the given key, if the key is found equal to the given key, that is, in this hash table to find the <Key,Value> key value pair, after which the key value pair can be related operations; if not found, that is not the existence of this Key-value pairs.



So the hash table is actually the management of a pair of <Key,Value> such structure.






2. The inevitable hash conflict



It is well known that the hash table is the management of a group of <key,Value> data structure, access to the key to take the hash algorithm evaluation, according to the value of the chain array, based on the chain array to obtain value. What about a given key?



Hashing is based on a hashing algorithm, ideally the same key hash is the same value, the different key hash column is different values. In reality, however, because of the limited storage space, this algorithm is impossible to implement, so when the different keys are scattered as the same value, there is a conflict. This is what we call the hash conflict, let's talk about why this algorithm is impossible to achieve?



Ideally one-dimensional hash table: for example:






(2) Ideal case of a one-dimensional hash table



Ideally, a one-dimensional hash table holds a pair of pairs of <key,Value> key values,



1. Requirements for hash hashing algorithms: Different keys must be hashed to different values, and the same key must be hashed as the same value.



2. The requirements of an array, in order to maintain efficient access, must be kept in order to store the arrays. (The access time of the chained array is Logn, and the structure of the balance tree must be maintained at all times to be costly and non-conforming).



Ok.... So the problem is, the ideal hash table can solve the following problems perfectly:



(1). In order to be efficient, you are an array of sequential storage, then do you know how much storage you need to open up to this array each time? If too big, will inevitably cause the waste of space, and this space must also be continuous, if too small, need to constantly adjust. So you can still stay productive?



(2). For each <Key,Value> how much space do you prepare to store this key-value pair? I'm sorry to tell you ... You don't know. Too larger than the small will face the question we asked just now?






When the problem arises, how many geniuses are there to emerge?






The perfect implementation of the 3.hash table (allows for the existence of conflicts)



Hash lookup is an efficient lookup, and with the compromise between storage space and lookup time, someone proposes a new idea that allows different key hashes to be the same value.



The specific implementation is as follows:



1. First open an array in space. We first get the subscript of the array based on the key and hash algorithm, which is the partial left array.



2. Each cell in the array maintains a chain array, and the chain array holds <key,value>



3. When accessing, according to the key one by one comparison, if found, obtained value, if not found, different programming languages return different values.



4. When storing, first find out if this Key exists, if present, modify this value, if not, then the end of this chain array plus a pair of <Key,Value>;



With the above we can see that the use of hash table storage objects, the time to query the speed is very fast. The left array can be accessed randomly, while the right chain array needs to traverse, but if the hashing algorithm is good enough, the number of key-value pairs stored in each chained array will be small. The search speed is also fast enough. Ideally, the search speed can reach almost O (1).









What is the storage format for objects in 2.JavaScript?



1. Two different types of objects to create



(1). creating an object with a constructor function


1 var object=new Object();
2 object.x=1;
3 object. 2 = 1; / / error, because variable cannot start with a number 


The above mistakes believe that we all know, then use the literal creation of objects?



(2). create an object with literal amounts


var object = {
X: 1,
2: 2
}; / / no error 





(3) Two ways of comparison



Do you think it's strange that we should compare the two? ------Why the first way of error, the second is not?



Of course, you can also understand that the JS engine is blocking syntax errors. So why does the JS engine bother to shield the offending variables so much? The answer is: there's a reason for---.






Suppose JS allows you to assign values in this way or access


object.2=2; Assuming there is no error
2==2;
Console.log (2);//How do you make the JS engine happen when this happens? is to understand 2 as a global variable or a number 2


When this happens, the interpreter can only be despair. Therefore, in order to unify the grammar, we define various requirements for the variable-type Access object variables.



In other words, the JS engine is reasonable for shielding against illegal variables.






So the question comes again----



Since there is a requirement for the form of a variable, why do you allow this kind of thing to exist? -----answer is: because it is also reasonable/
 
var object = { 
  x:1, 2: 2 };


Why is this way reasonable?



Because there is no such or such requirement for key in the <Key,Value> key pair of the hash table. So, if the JS object is based on a hash table to store variables, since this form is valid at the time of storage, then why don't you allow me to access my stored variables? This is not a contradiction!



Of course, there is a logical question, why do I agree that the storage of JS object is based on hash? (I'll explain this in part III)






The above lists two methods of defining object variables and the differences, and then explains why the two definition forms clearly conflict with each other but also exist!






Let's start with our discussion: What is the key in the hash table of JS object? ------Answer: String






First, let's take a look at his output.


 
var object = {
  x: 1, 2: 2 } for (var property in object) {
  console.log(‘(‘ + typeof property + ‘)‘ + property + ‘:‘ + object[property]);
}
Result (string) 2:2
 (String) x:1


The above code proves two points: object[2] is really there. And the Key in <Key,Value> is in the form of a string. That is, the object's variables and values are stored in the hash table as a <Key,Value> key value pair, and the key is in the form of a string, so what is the form of value? We'll discuss this later. It is also worth discussing.






Summarize:



When the object is initialized with literals, the variable name can make the number, or it can be a string, but it can not be an object (explained below), but later with [] access, the contents of [] can be an object, as long as the content in [] can be converted to a string.



Both forms of the initialization object are legal, but access to the variable with the. Number can only access our usual legal variables, to [] access is relatively free, there are not so many requirements, this is the JS design time exists shortcomings, but also is a bit of JS language.



Why objects cannot be used as variables when initializing an object, as follows:



Var object={
{X: 1}: 2 / / this form is incorrect because the interpreter does not recognize the syntax correctly and will report an error. -----Theoretically, it can be done. It may be a defect in design.
}
//The following methods will not report errors
var  object={};
object[{x:1}]=1;
Console. Log (object [{X: 1}]); / / 1 the interpreter will try its best to interpret the contents of [] into a string.














Know that key is a string, then we can now explain the problem: object[2] and object["2"] there is a difference? -----(No, of course, it depends on how you understand it)! To prove that there are no differences between the two forms, let's look at the following two examples






Consider: What does the interpreter do for us when we use [] to access the object variable?


 
var object = {
  x: 1, 2: 2 }

console.log(object[2]);//2
console.log(object["2"]);//2

object[2]=3;
console.log(object["2"]);//3


The reason object[2] and object["2" are equivalent, that's because the interpreter helped us do a little work, so what did the interpreter do for us?






When the interpreter accesses Object[2], it first converts the 2 inside the square brackets into a string. Then visit, in order to prove this, I wrote a little code to prove it.


 
var object = {
  x: 1, 2: 2 }
Object.prototype.toString = function () { return ‘2‘;
}
console.log(object[{x: 1}]);  //2
console.log(object["2"]);  //2
console.log(object[2]);  //2


Take a moment to analyze the execution of the above code:



1. First a object object is defined and initialized, and there are two variables in the object.



2. Override the ToString () method in the object prototype.



3. In line 7th, we used a temporary object {x:1} for []. When this object is initialized, the {x:1} in square brackets is parsed before object[] execution, at which point the interpreter converts the object to a string, and if the reference type invokes the ToString () function in the prototype object, the base data type is also converted to the corresponding string if it is a basic data type. The result is access to object["2"]. The result of the output is 2.






So we also indirectly prove that the JS object, all the key is a string, even if you access the time is not the form of a string, the interpreter will try to convert it to a string first.



So the following two ways to initialize an object are completely equivalent:


1 var object1 = {
2 x: 1,
3 1: 2
4} / / first
5 var object2 = {
6   ‘x‘: 1,
7   ‘1‘: 2
8} / / second 





Above, we are talking about the storage of JS object and how the data is stored.



Let's discuss why the variables stored in JS are based on hash.






objects in 3.JavaScript store variables based on the hash table.



(1). Proof:



We can add or remove variables to an object at any time (if this variable allows deletion)


1 var object={};
2 object. X = 1; / / add a variable
3 delete object. X; / / delete a variable success
4 console.log(Object.keys(object).length); //0 


(1) Since the object type and number of variables are variable, we cannot assign an object to a fixed space like java,c++. The space occupied by the object that the JS reference refers to must support adjustment at any time. Based on this, the sequentially stored array has been eliminated.



(2) The disadvantage of the slow-link array query is that it cannot be used as the storage structure of variables in the object.



(3) Of course you can say that I can use the tree storage structure, the more efficient may be the balance tree. The time complexity of the balance tree is log (n), not too high, but when the attribute is deleted, the balance tree is much more expensive to adjust than the hash table. Perhaps, you have other options, but I dare say that there is no such a hash table to store data so convenient and efficient.



(4). Only JavaScript objects are stored on a hash table, and all of the c++,java,c# that exist in the JavaScript language are reasonable.



In fact, white, physical storage is not a non-hash table, but there is no better than the hash table, so in the JS language design is as a hash table structure design.






To prove that the object is stored in key-value pairs based on the hash table, let's take a simple look at the array types and functions:



Function:


 
var person=function y(){};

person.x=1;
person.y=2; for(var property in person){
  console.log(property+" : "+ person[property])
}//x:1
// y:2


Array:


 
var array=[1,2,4] for(var property in array){
  console.log(property+" : "+ array[property])
}// 0: 1
 // 1: 2
 // 2:4 


(2). Understand what this is for:



1. You also can have the array number of properties, functions can also have properties strange? Because they manage their own hash tables, they can add or remove properties at any time.



2. We know that the following table in the array can be arbitrarily added, no matter how big you set, you can also cross-border access (strictly speaking, there is no so-called cross-border), but the result of the return is undefined ... Because the corresponding key was not found.



You understand it as subscript, rather than as a key, so, JS array is so mysterious? Plainly, it is a hash structure. If you want to, you can also think of object as an array, and then customize a whole set of functions, but it may not be so convenient.









Note: Of course, the function as an object must have its particularity. We don't have much discussion here.









4.JS object is a typical application based on hash table.






Array de-weight


1 var array=[‘true‘,true,false,‘1‘,1,‘‘,‘sss‘," ",1,34,,,{x:1},{x:2}]
Two
3 Array.prototype.unique=function(){
Four
5 / / de duplication by using the hash storage feature of the object
6   var object={},result=[]; 
Seven
8   for(var i=0,length=this.length;i<length;i++){
Nine
10     var temp=this[i],key;
Eleven
12     if((typeof temp)==‘object‘){
13 key = JSON. Stringify (Temp); / / if it is an object type, serialize the object as a string
14     }else{
15           key=typeof temp+temp;
16}
Seventeen
18     if(!object[key]){
19 object [key] = true; / / if the key value already exists in the object, it indicates that the element already exists in the array
20       result.push(temp);
Twenty-one
22}
Twenty-three
24}
Twenty-five
26   return result;
Twenty-seven
28}
Twenty-nine
30 console.log(array.unique()); 
//["True",True,False,"1",1,"" "SSS" 34< Span class= "Arraycomma", undefinedobject {x=1} object {x< Span class= "objectequal" >=2" ]





The disadvantage of this algorithm is that because of the addition of an object object and a result array, the comparison takes up space, but is very fast, at least faster than a tree structure. This is the improvement of some algorithms on the net, there are a lot of algorithms for object hash on the network is not perfect to go heavy.



For example, array [1, "1", {x:1},{x:2}].






The doubts that exist in the article: what is the method that the basic types of number,boolean are called when they are converted to strings? (Not the ToString () method in the prototype chain, which is not described, welcome to add);






JavaScript series-----objects based on hash storage (<Key,Value> Key article) (1)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.