1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15function logMapElements(value, key, map) { 16 print(`m[${key}] = ${value}`); 17} 18 19new Map([ 20 ['foo', 3], 21 ['bar', {}], 22 ['baz', undefined], 23]).forEach(logMapElements); 24 25 26function logMapElements1(value, key, map) { 27 print(`m[${key}] = ${value}`); 28 map.delete(key); 29 print(map.size); 30} 31 32new Map([ 33 ['foo', 3], 34 ['bar', {}], 35 ['baz', undefined], 36]).forEach(logMapElements1); 37 38let myMap = new Map(); 39for (let i = 0; i < 1000; i++) { 40 myMap.set(i, i); 41 myMap.set("key" + i, i); 42 myMap.set(Symbol("foo"), i) 43} 44 45const hugeString = BigInt("9007199254740991"); 46for (let i = 0; i < 100; i++) { 47 myMap.set(hugeString + BigInt(i), hugeString); 48} 49 50var intSum = 0; 51var symbolSum = 0; 52var bigIntSum = BigInt(""); 53var stringSum = 0; 54myMap.forEach((value, key, map) => { 55 if (typeof key == "bigint") { 56 bigIntSum += value; 57 return; 58 } 59 if (typeof key == "number") { 60 intSum += value; 61 return; 62 } 63 if (typeof key == "symbol") { 64 symbolSum += value; 65 return; 66 } 67 if (typeof key == "string") { 68 stringSum += value; 69 return; 70 } 71}); 72 73print(intSum); 74print(bigIntSum); 75print(symbolSum); 76print(stringSum) 77