Is es6 map a reference type?
Map is a reference type; map (collection) is a new reference data type in es6, which represents the mapping relationship of data. Data in the map collection data type is stored in a "key/value" manner. You can use the properties of the object as the key and use the properties to reference the value; the map can be created using new, for example "const myMap = new Map();" .
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
map is a reference type.
es6 map
Before ES6, to implement 'key'=>'value' in JavaScript, which is what we often call key-value pairs, is to use Object to complete. However, this implementation method has problems in special scenarios. ES6 has introduced a new collection type called Map, which brings a true key-value pair storage mechanism to the language.
map (collection) is a new reference data type in es6, which represents the mapping relationship of data; data in the map collection data type is stored in a "key/value" manner, and you can use the properties of the object As keys, use properties to reference values.
Use the new
keyword to instantiate a map
let m = new Map(); console.log(m); // Map(0) {}
Initialization when creating:
Pass in a two-dimensional array parameter (an iterable object, the key value is passed in as an array internally)
For each sub-array, the first element corresponds to map key
, the second element is the value
let m = new Map([[{}, 222], [{}, '123']]); console.log(m); // Map(2) { {} => 222, {} => '123' }
1-2-1 Add mapping element
Add through the set() method, passing in two parameters, the first The key of the map is passed in first, and the value of the map is passed in the second one. What is returned is the mapping set (meaning it can be added in a chain)let m = new Map(); m.set('prop', '值'); console.log(m); // Map(1) { 'prop' => '值' }
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); console.log(m); // Map(3) { 'prop' => '值', 'prop2' => false, 'num' => { id: 13 } }
##1-2-2 Mapping set length Use the
size attribute to get the number of elements in the current collection<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>let m = new Map();
m.set(&#39;prop&#39;, &#39;值&#39;).set(&#39;prop2&#39;, false).set(&#39;num&#39;, {id: 13});
console.log(m.size);</pre><div class="contentsignin">Copy after login</div></div>
1-2-3 Get elementsGet the element through the get() method and pass in the key of the target
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); console.log(m.get('prop2')); // false
##1-2-4 Delete the elementDelete an element in the mapping collection through the delete() method and return a Boolean value indicating whether the deletion is successful or failed let m = new Map();
m.set('prop', '值').set('prop2', false).set('num', {id: 13});
m.delete('prop2');
// true
console.log(m.get('prop2'), m.size);
// undefined 2
Copy after login
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); m.delete('prop2'); // true console.log(m.get('prop2'), m.size); // undefined 2
1-2-5 Detect whether the element is ExistenceUse the has() method to detect whether the target element exists and return the Boolean value of the detection resultlet m = new Map();
m.set('prop', '值').set('prop2', false).set('num', {id: 13});
m.delete('prop2');
// true
console.log(m.has('prop2'), m.has('num'));
// false true
Copy after login
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); m.delete('prop2'); // true console.log(m.has('prop2'), m.has('num')); // false true
1-2-6 Clear the element MethodUse the clear() method to clear all elements and return a Boolean value of successful clearinglet m = new Map();
m.set('prop', '值').set('prop2', false).set('num', {id: 13});
m.clear();
// true
console.log(m);
// Map(0) {}
Copy after login
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); m.clear(); // true console.log(m); // Map(0) {}
##1-3 Sequence With iterationmap can iterate elements according to the order of insertion The mapping instance will provide (iterator). It can generate an array in the form of [key, value] in the order of insertion, and can pass entries () method (or provided Symbol.iterator) iterator interface traversal.
let m = new Map(); m.set('prop', '值').set('prop2', false).set('num', {id: 13}); console.log(m.entries === m[Symbol.iterator]);// true for(let k1 of m.entries()){ console.log(k1); // [ 'prop', '值' ] // [ 'prop2', false ] // [ 'num', { id: 13 } ] // 遍历的属性即对应映射元素的键值对数组 } for(let k2 of m.keys()){ console.log(k2); // prop // prop2 // num // 遍历的属性对应映射元素的键 } for(let k3 of m.values()){ console.log(k3); // 值 // false // { id: 13 } // 遍历的属性对应映射元素的值 } for(let k4 of m[Symbol.iterator]()){ console.log(k4); // [ 'prop', '值' ] // [ 'prop2', false ] // [ 'num', { id: 13 } ] // 遍历的属性即对应映射元素的键值对数组 }
1-4 Comparison with ObjectMemory usage
Browser The difference will lead to different memory usage between the two storage methods. However, given the memory size, map stores about 50% more key-value pairs than Object.-
Insertion speed The performance of map and Object above are roughly the same, but if the code involves a large number of insertions, it is recommended to use mapInsert Performance
-
The difference is smaller Small, Object is better when it contains only a small number of key-value pairsFind speed
-
Object's delete() performance is poor, while map's delete() The performance is good. If the data involves a large number of deletion operations, it is recommended to use mapDelete performance
-
[Related recommendations:
javascript video tutorial ,
The above is the detailed content of Is es6 map a reference type?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

application.yml defines the list collection. The first way is to use the @ConfigurationProperties annotation to obtain all the values of the list collection type:code:status:-200-300-400-500. Write the entity class corresponding to the configuration file. What needs to be noted here is that defining the list Collection, first define a configuration class Bean, and then use the annotation @ConfigurationProperties annotation to obtain the list collection value. Here we will explain the role of the relevant annotations. @Component hands over the entity class to Spring management @ConfigurationPropertie

1. Technical background In actual project development, we often use caching middleware (such as redis, MemCache, etc.) to help us improve the availability and robustness of the system. But in many cases, if the project is relatively simple, there is no need to specifically introduce middleware such as Redis to increase the complexity of the system in order to use caching. So does Java itself have any useful lightweight caching components? The answer is of course yes, and there is more than one way. Common solutions include: ExpiringMap, LoadingCache and HashMap-based packaging. 2. Technical effects to realize common functions of cache, such as outdated deletion strategy, hotspot data warm-up 3. ExpiringMap3.

async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

Method 1. Use HashtableMapashtable=newHashtable(); This is the first thing everyone thinks of, so why is it thread-safe? Then take a look at its source code. We can see that our commonly used methods such as put, get, and containsKey are all synchronous, so it is thread-safe publicsynchronizedbooleancontainsKey(Objectkey){Entrytab[]=table;inthash=key.hashCode( );intindex=(hash&0x7FFFFFFF)%tab.leng

There are many ways to convert javabeans and maps, such as: 1. Convert beans to json through ObjectMapper, and then convert json to map. However, this method is complicated and inefficient. After testing, 10,000 beans were converted in a loop. , it takes 12 seconds! ! ! Not recommended. 2. Obtain the attributes and values of the bean class through Java reflection, and then convert them into the key-value pairs corresponding to the map. This method is the second best, but it is a little more troublesome. 3. Through net.sf.cglib.beans.BeanMap Method in the class, this method is extremely efficient. The difference between it and the second method is that because of the use of cache, the bean needs to be initialized when it is first created.

The map directive uses the ngx_http_map_module module. By default, nginx loads this module unless artificially --without-http_map_module. The ngx_http_map_module module can create variables whose values are associated with the values of other variables. Allows classification or simultaneous mapping of multiple values to multiple different values and storage in a variable. The map directive is used to create a variable, but only performs the view mapping operation when the variable is accepted. For processing requests that do not reference variables, this The module has no performance shortcomings. 1.ngx_http_map_module module instruction description map syntax

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

Optimizing the performance of Go language map In Go language, map is a very commonly used data structure, used to store a collection of key-value pairs. However, map performance may suffer when processing large amounts of data. In order to improve the performance of map, we can take some optimization measures to reduce the time complexity of map operations, thereby improving the execution efficiency of the program. 1. Pre-allocate map capacity. When creating a map, we can reduce the number of map expansions and improve program performance by pre-allocating capacity. Generally, we
